Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

Computational Methods

Chapter Four
Interpolation and Approximation

School of Electrical and Computer Engineering

Instructor: Haftom G.
23 April, 2024

1
Chapter 4| Interpolation and Approximation Year III, Sem I
Outline
❖ Introduction
❖ Interpolation

▪ Linear interpolation
▪ N-degree polynomial interpolation
▪ Piecewise (Spline) interpolation
❖ Approximation
▪ List square fit approximation

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 2
1. Introduction
▪ Experimental observations or computational results are usually
represented by discrete data sets
▪ These data are usually tabulated as:

▪ Interpolation and curve fitting are the two ways to relate these discrete
data sets as a function.
▪ In interpolation, we construct a curve through the data points.
▪ Curve fitting is used to find a smooth curve that approximates the
data in some sense
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 3
Cont…

Data points interpolation curve fitting

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 4
Interpolation
◼ Used estimate intermediate values between precise data points.
◼ The simplest form of an interpolant is a polynomial.
◼ For 𝒏 + 𝟏 distinct data points, there is only one polynomial function of
order 𝒏 that passes through all points.
𝑷𝒏 𝒙 = 𝒂𝟎 + 𝒂𝟏 𝒙 + 𝒂𝟐 𝒙𝟐 + ⋯ + 𝒂𝒏 𝒙𝒏
◼ Thus, polynomial interpolation is consists of determining the unique
𝑛𝑡ℎ order polynomial that fits 𝑛 + 1 data points
◼ If 𝑓(𝑥) is continuous on a finite interval [𝑎, 𝑏], there exists polynomial
𝑃𝑛 (𝑥) of degree 𝑛, such that:

where 𝑒 is a very small error.


________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 5
Cont…

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 6
Cont…

◼ Some polynomial interpolation methods

a. Linear interpolation

b. N-degree interpolation

c. Interpolation by Lagrange method

d. Interpolation by Newton’s divided difference

e. Piecewise (Spline) interpolation

_______________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 7
a. Linear Interpolation
◼ It is applicable when two data points are given

◼ Consider: x0 x1
f(x0) f(x1)

◼ Using a similar triangle rule:

◼ By rearranging, yields a function

f ( x0 ) − f ( x1 )
f1 ( x) = ( x − x0 ) + f ( x0 )
x0 − x1

Which is used to relate two data points


_________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 8
Cont…
◼ Example 1: Find the linear approximation of 𝑒 𝑥 in the interval [0, 1]

◼ Solution: first find 𝑓 𝑥0 &𝑓 𝑥1


𝑓 𝑥0 = 𝑒 𝑥0 = 𝑒 0 = 1
𝑓 𝑥1 = 𝑒 𝑥1 = 𝑒 1 = 𝑒

Then using the equation for linear interpolation we can relate the data
points as
f ( x0 ) − f ( x1 )
f1 ( x) = ( x − x0 ) + f ( x0 )
x0 − x1

1− e
f 1 ( x) = ( x − 0) + 1 = (e − 1) x + 1
0 −1
Which is linear equation
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 9
b. N-degree polynomial interpolation
◼ If we have 𝑛 + 1 data points, the 𝑛𝑡ℎ degree polynomial is given as:

Pn ( x) = a0 + a1 x + a 2 x 2 +  + a n x n

◼ When the above equation is evaluated at the different 𝒏 + 𝟏 data


points, we have the linear system of equations as:

a 0 + a1 x1 + a 2 x1 2 +  + a n x1 n = y1

a 0 + a1 x 2 + a 2 x 2 +  + a n x 2 = y 2
2 n


    
a + a x + a x 2 +  + a x n = y
 0 1 n 2 n n n n

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 10
Cont…
◼ The linear system of equations can be expressed in matrix form as:

1 x1 x12 x13 x1n   a0   y1 


 2 3

n   y 
 2 2 2
1 x x x x2   1   2 
a
   =  
    
1 xn xn2 xn3 xnn   an   yn 
◼ Then we can finally solve the above equation using the methods
discussed in the previous chapter
Example 2: Find an approximate function that is used to relate the data
point given below:
X 0 1 2
Y 2 3 6
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 11
Cont…
Solution
▪ Since we have 3 data points we can interpolate quadratic function as:
𝑷𝟐 𝒙 = 𝒂𝟎 + 𝒂𝟏 𝒙 + 𝒂𝟐 𝒙𝟐
▪ Evaluate the quadratic function at each data points and obtain:

1 0 0  a 0  2
1 1 1  a  = 3 
  1   
1 2 4 a 2  6 
▪ If we use gaussian elimination we have 𝑎0 = 2, 𝑎1 = 0 & 𝑎2 = 1
▪ Therefore, the quadratic equation which relates the data points is:
P2 ( x ) = x 2 + 2
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 12
c. Lagrange’s Method

▪ It is another alternative interpolating method by taking on certain

values at arbitrary points

▪ An n-degree polynomial is constructed by using Lagrange's formula as:


n
p n ( x) =  yi Li ( x)
i =0

▪ The 𝐿′ 𝑠 are known as cardinal functions (Lagrange’s function)

n x − xj
Li ( x) = 
j =0 xi − x j
j i
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 13
Cont…
◼ Example 3: Given the data’s (3, 1), (4, 2) and (5, 4) find the
interpolating function using Lagrange’s method
Solution
◼ Since we have 3 tabulated data points we can construct quadratic
polynomial function
𝑝2 𝑥 = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2
◼ Then find the constants 𝑎0 , 𝑎1 , and 𝑎2 using the Lagrange's formula as:
2
p2 ( x) =  yi Li ( x) = y0 L0 + y1 L1 + y2 L2
i =0

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 14
Cont…
◼ But first find the cardinal functions by using the product formula as:
2 x − xj
x − x1 x − x2 x −4 x −5 x 2 − 9 x + 20
L0 ( x) =  =( )( )=( )( )=
j =1 x0 − x j x0 − x1 x0 − x2 3 − 4 3 − 5 2
j 0

2 x − xj x − x0 x − x2 x −3 x −5
L1 ( x) =  =( )( )=( )( ) = − x 2 + 8 x − 15
j =0 x1 − x j x1 − x0 x1 − x2 4−3 4−5
j 1

2 x − xj
x − x0 x − x1 x −3 x −4 − x 2 + 7 x − 12
L2 ( x) =  =( )( )=( )( )=
x
j =0 2 − x j x2 − x0 x2 − x1 5 − 3 5 − 4 2
j2

◼ Therefore, the quadratic polynomial function is

𝑥 2 − 9𝑥 + 20 2
−𝑥 2 + 7𝑥 − 12
𝑝2 𝑥 = 1 + 2 −𝑥 + 8𝑥 − 15 + 4( )
2 2
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 15
Cont…
◼ Solving the above equation
𝑝2 𝑥 = −29 + 25.5𝑥 − 3.5𝑥 2
Therefore, the value of the constants become
𝑎0 = −29, 𝑎1 = 25.5, and 𝑎2 = −3.5

◼ Exercise: Find an approximate polynomial function for the following


data using the Lagrange’s method.

X 0 1 2 3 4
Y 2 3 6 8 12

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 16
Cont…

 Important points on Lagrange’s polynomial

▪ The spacing between the data points does not need to be equal

▪ For a given set of points, the whole expression of the interpolation

polynomial has to be calculated for every value of x.

▪ If an interpolated value is calculated for a given set of data points,

and the data set is enlarged to include additional points,

▪ All the terms of Lagrange’s polynomial have to be calculated

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 17
d. Newton's Divided Difference (NDD)

◼ Lagrange’s method has two main disadvantages.

✓ It engages more arithmetic operations

✓ All arithmetic operations have to be repeated for any extra added point in

the data.

◼ Newton’s divided difference avoids these kinds of difficulties.

◼ In Newton’s divided difference method, an 𝑛𝑡ℎ degree polynomial is

given as:

Pn ( x) = a0 + ( x − x0 )a1 + ( x − x0 )( x − x1 )a2 + + ( x − x0 )( x − x1 ) ( x − xn −1 )an

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 18
Cont…
◼ The polynomial coefficients are computed using divided difference

a 0 = f ( x0 )

a = f ( x1 ) − f ( x0 )
f [ x1 , x0 ] =
 1 x1 − x0

 f [ x 2 , x1 ] − f [ x1 , x0 ]
a 2 = f [ x 2 , x1 , x0 ] =
 x 2 − x0


a = f [ x n , x n −1 ,  , x1 ] − f [ x n −1 , x n − 2 ,  , x0 ]
 n f [ x n , x n −1 ,  , x0 ] =
 x n − x0
Where,
f ( xi ) − f ( x j )
f [ xi , x j ] =
xi − x j
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 19
Cont…
◼ For n = 4 the graphical depiction of the recursive nature of finite
divided differences is expressed as follows

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 20
Cont…

◼ Example 4: Find an approximate quadratic function for the given data

using Newton’s divided difference method.

X 0 1 2
Y 2 3 6

◼ Solution: The Newton’s divided difference polynomial is expressed as:

P2 ( x) = a 0 + ( x − x0 )a1 + ( x − x0 )( x − x1 )a 2

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 21
Cont…
◼ The coefficients are determine using divided difference as:



a 0 = f ( x 0 ) = 2
 f ( x1 ) − f ( x0 )
a1 = f [ x1 , x0 ] = =1
 x1 − x0
 f [ x 2 , x1 ] − f [ x1 , x0 ] 3 − 1
a 2 = f [ x 2 , x1 , x0 ] = = =1
 x 2 − x0 2

◼ Then the polynomial function becomes

P2 ( x) = 2 + ( x − 0) + ( x − 0)( x − 1) = x 2 + 2
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 22
Cont…

 Important points on Newton’s polynomial

▪ The spacing between the data points does not to be equal

▪ For a given set of points, once the coefficients 𝑎1 through 𝑎𝑛 are

determined, they can be used for interpolation at any point

between the data points

▪ Additional data points can be added and only the additional

coefficients have to be determined

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 23
Cont…
Exercise
◼ The set of the following five data points is given:

X 1 2 4 5 7
y 52 5 -5 -40 10

a) Determine the polynomial function using the NDD method that


passes through the points.
b) Use the polynomial obtained in part (a) to determine the interpolated
value for 𝑥 = 3
c) Write a MATLAB user-defined function that interpolates using
Newton’s polynomial.
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 24
e. Piecewise (Spline) Interpolation

◼ As the order of the polynomial increases, a large error might occur.

◼ But if the number of points is small the interpolated values are

reasonably accurate

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 25
Cont…

◼ When a large number of points is involved:

► a better interpolation can be obtained by applying multiple low-order

polynomials to the sub set of data points.

◼ This type of interpolation is called piecewise or spline interpolation

and the connecting polynomials are called spline function

◼ All of the polynomials are of the same order, but the coefficients are

different in each interval.

◼ Linear, quadratic, and cubic are some types of spline interpolation

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 26
Linear spline interpolation

◼ Is carried out by using first-order polynomials between the points

◼ The points are connected using straight line

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 27
Cont…
◼ For example to connect the first two intervals (𝑥1 , 𝑦1 ) & (𝑥2 , 𝑦2 ) we can
use Lagrange's method as:

◼ For 𝑛 data points there are 𝑛 − 1 intervals, and the interpolation in the
𝑖 𝑡ℎ interval of data points (𝑥𝑖 , 𝑦𝑖 ) & (𝑥𝑖+1 , 𝑦𝑖+1 ) is connected using the
equation.

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 28
Cont…
◼ Example 5: determine the linear spline for the following data points
and evaluate the interpolated value at 𝑥 = 12.7

X 8 11 15 18

Y 5 9 10 8

◼ Solution: since we have four data points there are three possible
spline intervals;
𝑓1 𝑥 𝑖𝑠 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 8, 5 & (11, 9)

𝑓2 𝑥 𝑖𝑠 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 11, 9 & (15, 10)

𝑓2 𝑥 𝑖𝑠 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 15, 10 & (18, 8)

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 29
Cont…
◼ Then using the equation for spline interpolation in each interval

◼ The interpolated value of 𝑦 at 𝑥 = 12.7 is found in 𝑓2 (𝑥) and by


substituting the value of x in the equation we get

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 30
3. Approximation (Curve fitting)
◼ It is a procedure in which a mathematical
formula (equation) is used to best fit a given
set of data points.
◼ The objective is to find a function that fits the
data points overall.
◼ It is used to reduce the random noise that
occurred due to measurement errors of the
data.
◼ Finds a smooth curve that fits the data point
on average
◼ The most commonly used approximation is
the Least-square fit method.

_______________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 31
Least-Square Fit
◼ A procedure for finding the best-fitting curve to a given set of points.
◼ It can be done by minimizing the sum of the squares of the offsets
(called residuals) of the points from the curve.
◼ The sum of the squares of the offsets is used because:
◼ Simply adding the residuals of all the points does not provide a good
measure of the overall fit.
◼ Making the overall error E equal to the sum of the absolute values of the
residuals can not have a unique solution.

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 32
a. Least-Square Fit of straight line

◼ It is fitting a straight line to a set of paired observations:

(𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), . . . , (𝑥𝑛 , 𝑦𝑛 )

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 33
Cont…

◼ The mathematical expression for the straight line is

𝑦 = 𝑎0 + 𝑎1 𝑥 + 𝑒

◼ Where, 𝑎0 and 𝑎1 are coefficients and 𝑒 is the error (residual) during fitting

◼ Therefore, the aim is determining 𝑎0 & 𝑎1 that gives smaller error

when the data points are substitute

◼ The error is defined as the difference between the true value of (y) and

approximate value (𝑎0 + 𝑎1 𝑥) at each data point.

𝑒 = 𝑦 − 𝑎0 − 𝑎1 𝑥

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 34
Cont…

◼ One strategy for fitting a “best” line through the data would be to

minimize the sum of the residual errors for all the available data.

◼ This is done by determining a unique linear function that has smallest

total error which is obtained by:

◼ Making sum of the offsets equals to the sum of the squares of the offsets

between the measured ‘y’ and the ‘y’ calculated with the linear model

Where, 𝑆𝑟 is sum of the residuals at each data points


________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 35
Cont…
◼ Since all the values 𝑥𝑖 and 𝑦𝑖 are known, 𝑆𝑟 is a nonlinear function of the
two variables 𝑎1 and 𝑎0 .
◼ The function 𝑆𝑟 has a minimum at the values of 𝑎1 and 𝑎0 ,
◼ where the partial derivatives of 𝑆𝑟 with respect to each variable is equal to
zero.

◼ Setting these derivatives equal to zero and gives:

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 36
Cont…
◼ Now, realizing that σ 𝑎0 = 𝑛𝑎𝑜 , we can express the equations as a set of
two simultaneous linear equations as:

◼ Solving simultaneously for 𝑎0 and 𝑎1 gives

Where, 𝑦ത and 𝑥ҧ are the means of 𝑥 and 𝑦 respectively


◼ The above equation is called linear least square regression of straight line
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 37
Cont…
◼ Example 6: Determine the best straight line that fits to the following data
points.
𝒙𝒊 1 2 3 4 5 6 7
𝑦𝑖 0.5 2.5 2 4 3.5 6 5.5

◼ Solution: first compute the following quantities

Then ,

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 38
b. Least square fit of n-degree polynomial
◼ A given set of 𝑛 data points can be curve-fit with polynomials of
different order up to an order of (𝑛 − 1).

◼ The plots in the figure show that as the order of the polynomial
increases the curve passes closer to the points.

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 39
Cont…
◼ The figure shows that the same set of data points can be curve fit with
polynomials of different order.
“Which of the polynomials gives the best fit?”
◼ It depends on the type and source of data, the engineering or science
application associated with the data, and the purpose of the curve
fitting.
◼ For example consider the two scenario:
i. When the data points lack accuracy, it isn't very logical to employ a
higher-order polynomial that tries to closely mimic these points.
ii. However, if the data points are highly precise and the goal is to accurately
represent the data, using a higher-order polynomial for curve fitting might
be more suitable
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 40
Cont…
◼ Suppose that we need to fit second order polynomial

◼ In our case the sum of the squares of the residuals is:

◼ By differentiating 𝑆𝑟 w.r.t each coefficients we get:

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 41
Cont…
◼ These equations can be set equal to zero and rearranged to develop
the following set of linear equations:

◼ Therefore, we can solve the coefficients 𝑎0 , 𝑎1 and 𝑎2 using the


methods used to solve systems of linear equations.

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 42
Cont…
◼ Consider the error that occurred due to measurement for n data points is
n
Sr =  ( yi − a0 − a1 xi − a2 xi2 − − an xin ) 2
i =0

◼ The optimal value of the parameters are given by differentiating 𝑆𝑟 w.r.t


each coefficient and set to zero gives:
 S n

 a =  − 2( y i − a 0 − a1 xi − a 2 xi2 −  − a n xin ) = 0
 0 i =0

 S n

 =  − 2( y − a 0 − a1 xi − a 2 xi2 −  − a n xin )xi = 0


 a1
i
i =0


 S n

 =  − 2( y − a 0 − a1 xi − a 2 xi2 −  − a n xin )xi2 = 0


 a 2
i
i =0
 

 S
n

 a =  − 2( y
i =0
i − a 0 − a1 xi − a 2 xi2 −  − a n xin )xin = 0
 n


________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 43
Cont…
◼ We can express the above equation in terms of matrix as

 n n n
  n 
n x x i
2
i x n
i  a   i  y
 i =0 i =0 i =0
  0   i =1 
 n n n n
  a1   n

  xi x x i   a  =   xi yi 
2 3 n +1
i i x
 i =0 i =0 i =0 i =0
  2   i =1 
    
 n n n n    n 
 x n x n +1
x n+2
i
x 2n   n   x n y 
a
 i =0 i i =0
i
i =0
i
i =0   i =1 i i 

◼ Since, this is systems of linear equation we can solve using the methods
we studied in the previous chapters.

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 44
Cont…
◼ Example 7: Fit quadratic equation from the following data points.

𝒙𝒊 0 1 2 3 4 5
𝑦𝑖 2.1 7.7 13.6 27.2 40.9 61.1

◼ Solution: from the given data we can compute

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 45
Cont…
◼ Then, the simultaneous linear equations are given as:

◼ Solving for the coefficients we get


𝑎0 = 2.47857, 𝑎1 = 2.35929, & 𝑎2 = 1.86071
◼ Therefore, the least square quadratic equation is
𝑦 = 2.47857 + 2.35929𝑥 + 1.86071𝑥 2

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 46
Cont…
Exercise:
1. Given the data points. Find the linear and second order least-square
fit polynomial and evaluate at x = 1.5

x 0 2 3
y 7 11 28

2. In an electrophoretic fiber-making process, the diameter of the fiber,


𝑑, is related to the current flow, 𝐼. The following are measured during
production:

◼ The relationship between the current and the diameter can be modeled with an
equation of the for 𝑑 = 𝑎 + 𝑏 𝐼 . Use the data to determine the constants a and b
that best fit the data.
________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 47
End

Any question?

________________________________________________________________________________________________________________________________
Chapter 4| Interpolation and Approximation Year III, Sem I 48

You might also like