MATH-5 LECTURE NOTE 6 Summer 20-21
MATH-5 LECTURE NOTE 6 Summer 20-21
MATH-5 LECTURE NOTE 6 Summer 20-21
6.1 Introduction
In many occasions we are given only a few discrete set of values. To study the behavior of the
function through those points a technique known as interpolation is introduced. Polynomial is a
function which is easy to handle. The method of finding a polynomial that fits a selected set of
points ( x, f ( x)) which behaves nearly the same way as the true function will be considered.
Example 6.1 : Find the polynomial of least degree which takes the values
x −1 1 2 5
f(x) 9 3 6 39
Solution: There are four set of values given. Let the approximated polynomial be
f ( x) a0 + a1 ( x + 1) + a2 ( x + 1)( x − 1) + a3 ( x + 1)( x − 1)( x − 2)
Using the values of x and f(x) in turn, we get
From x = −1, f (−1) = 9 , we get 9 = a0
From x = 1, f (2) = 6 , we get 3 = 9 + a1 (2) or a1 = 12 93 − 9) = −3
From x = 2, f (2) = 3 , we get 3 = 9 − 3(3) +a 2 (3)(1) or a2 = 13 (6) = 2
From x = 5, f (2) = 39 , we get 39 = 9 − 3(6) + 2(6)(4) + a3 (6)(4)(3) or
a3 = 1
72
(39 − 39) = 0
Thus the polynomial is f ( x) = 9 − 3( x + 1) + 2(( x + 1)( x − 1)
= 9 − 3x − 3 + 2 x 2 − 2
= 2 x 2 − 3x + 4
Summer 20-21
If f (x) is a polynomial through (n+1) points x0 , x1 , x2 , , xn , then the polynomial g (x) through
those points with an extra point x = xn +1 is g ( x) = f ( x) + b( x − x0 )( x − x1 ) ( x − xn )
The constant b can be calculated by substituting x = xn +1.
Example 6.2
The table below gives the values of x and f(x):
x: −1 1 2 3 4
f(x) : −7 −1 8 29 68
(ii) The needed differences are enclosed by the double lined box.
By Newton’s divided difference formula, we get
f ( x) = −7 + 3( x + 1) + 2( x + 1)( x − 1) + 1( x + 1)( x − 1)( x − 2)
and f (5) = −7 + 3(6) + 2(6)(4) + 1(6)(4)(3)
= −7 + 18 + 48 + 72 = 131
(iii) Here f (1) f (2) = (−1)(8) = −8 0
Thus a root is in (1, 2).
From the table, we have
x f(x) 1DD
1 −1
2 8 9
Thus the root is the solution of
f ( x) = −1 + 9( x − 1) = 0
or x = 1 + 19 1.111
(iv) The polynomial g (x) can be written as
g ( x) = f ( x) + b( x + 1)( x − 1)( x − 2)( x − 3)( x − 4)
where b is a constant.
Summer 20-21
Taking x = 5 , we have
g (5) = f (5) + b(6)(4)(3)(2)(1)
or 203 = 131 + 144b
203 − 131 72 1
Hence b= = =
144 144 2
The required polynomial is
g ( x) = f ( x) + 12 ( x + 1)( x − 1)( x − 2)( x − 3)( x − 4)
If the nodes are reordered as xn , xn−1 , , x0 , the divided differences interpolating polynomial can
be written as
p( x) = f ( xn ) + f [ xn , xn −1 ]( x − xn ) + f [ xn , xn −1 , xn − 2 ]( x − xn )( x − xn −1 ) +
+ f [ xn , xn−1 , xn−2 , , x0 ]( x − xn )( x − xn−1 ) ( x − x0 )
and is called the Newton Backward Divided Difference formula.
Lagrange polynomial of degree one passing through two points ( x0 , y0 ) and ( x1 , y1 ) is written as
x − x1 x − x0
L1 ( x) = y0 + y1
x0 − x1 x1 − x0
Lagrange polynomial of degree two passing through three points ( x0 , y0 ) , ( x1 , y1 ) and ( x2 , y2 )
is written as
( x − x1 )( x − x2 ) ( x − x0 )( x − x2 ) ( x − x0 )( x − x1 )
L2 ( x) = y0 + y1 + y2
( x0 − x1 )( x0 − x2 ) ( x1 − x0 )( x1 − x2 ) ( x2 − x0 )( x2 − x1 )
Lagrange polynomial of degree three passing through four points ( x0 , y0 ) , ( x1 , y1 ) , ( x2 , y2 ) and
( x3 , y3 ) is written as
( x − x1 )( x − x2 )( x − x3 ) ( x − x0 )( x − x2 )( x − x3 )
L3 ( x) = y0 + y1
( x0 − x1 )( x0 − x2 )( x0 − x3 ) ( x1 − x0 )( x1 − x2 )( x1 − x3 )
( x − x0 )( x − x1 )( x − x3 ) ( x − x0 )( x − x1 )( x − x2 )
+ y2 + y3
( x2 − x0 )( x2 − x1 )( x2 − x3 ) ( x3 − x0 )( x3 − x1 )( x3 − x2 )
In general, the Lagrange polynomial of degree n passing through (n + 1) points ( x0 , y0 ) , ( x1 , y1 )
, , ( xn , yn ) is written as
( x − x1 )( x − x2 ) ( x − xn ) ( x − x0 )( x − x2 ) ( x − xn )
Ln ( x) = y0 + y1
( x0 − x1 )( x0 − x2 ) ( x0 − xn ) ( x1 − x0 )( x1 − x2 ) ( x1 − xn )
( x − x0 )( x − x1 ) ( x − xn −1 )
+ + yn
( xn − x0 )( xn − x1 ) ( xn − xn −1 )
Summer 20-21
Example 6.3
The following table gives the values of an empirical function f(x) for certain values of x.
x 0 1 2 3
f(x) −4 −1 8 29
y −4 −1 8 29
x 0 1 2 3
Then
( y + 4)( y − 8)( y − 29) ( y + 4)( y + 1)( y − 29) ( y + 4)( y + 1)( y − 8)
x = 0 +1 +2 +3
(−1 + 4)(−1 − 8)(−1 − 29) (8 + 4)(8 + 1)(8 − 29) (29 + 4)(29 + 1)(29 − 8)
When y = 0 , then
(4)(−8)(−29) (4)(1)(−29) (4)(1)(−8)
x = 0 +1 +2 +3
(3)(−9)(−30) (12)(9)(−21) (33)(30)(21)
= 1.1457 + 0.1023 − 0.0046
= 1.2434
Summer 20-21
Exercise 6.4 The upward velocity of a rocket is given as a function of time below:
t (s) 10 15 20 22.5 30
v(t) (m/s) 227 363 517 603 903
i. Construct a divided-difference table for the above data.
ii. Determine the value of the velocity at 𝑡 = 17 seconds using two suitable points.
iii. Determine the value of the velocity at 𝑡 = 17 seconds using three suitable points.
iv. Find the polynomial which passes through all the points and find 𝑣(35).
v. Use Lagrange interpolating polynomial to estimate
a. the value of t for 𝑣(𝑡) = 400 using two suitable points.
b. the value of t for 𝑣(𝑡) = 400 using three suitable points.
vi. Write down MATLAB codes using “polyfit(x, y, n)” and “polyval(p, x)” for the
following.
a. Find the polynomial of least degree that incorporates all the values in the table. and
estimate the velocities corresponding to .𝑡 = 17, 25 and 35 seconds.
b. Draw the figure showing fitted polynomial and the given points.
Solution:
i.
t v(t) v1[ ] v2[ ] v3[ ] v4[ ]
10 227
15 363 27.2
20 517 30.8 0.36
22.5 603 34.4 0.48 0.0096
30 903 40.0 0.56 0/0043 0.0002
ii. Note that 15 < 17 < 20 and using the relevant part of the table
t v(t)
15 363
20 517 30.8
we have the linear polynomial 𝑣(𝑡) = 363 + 30.8 (𝑡 − 15).
And 𝑣(17) ≈ 363 + 30.8(2) = 424.6.
iii. Note that 17 − 10 = 7 and 22.5 − 17 = 5.5. Thus we may choose points corresponding to
𝑡 = 15, 20 and 22.5. Collecting the relevant part of the table
v. For a given v we need to calculate the value of t, so consider the Lagrange polynomial in
reverse order.
a. With two points consider
𝑣 363 517
𝑡 15 20
(𝑣−517) (𝑣−363)
and the Lagrange polynomial is 𝑡 = 15 (363−517) + 20 (517−363)
For 𝑣 = 400,
15(−117) 20(37) 2495
𝑡= + = = 16.2013.
−154 154 154
25.0000 695.8000
30.0000 903.0000 1000
Velocity v(t)
>> pt=polyfit(t,v,4);
600
>> t1=linspace(5,35,500); %
generates 500 values
400
>> v1=polyval(pt,t1); %
calculates values of v
200
>> plot(t, v,'o',t1,v1);
>> title('Graph of v against t');
0
>> xlabel('Time (t)'); 5 10 15 20 25 30 35
Time (t)
>> ylabel('Velocity v(t)');
Summer 20-21
Exercise 6.1
t(s) 1 3 4 7
v(m/s) 3 5 21 201
i. Construct a divided-difference table for the above data.
ii. Find the polynomial of least degree that incorporates the values in the table.
iii. Find the acceleration at time 𝑡 = 6𝑠.
iv. Find the distance function when 𝑆(0) = 2.
𝑥 4 5 7 9 11
𝑓(𝑥) 62 95 185 307 461
x -2 -1 0 3
f(x) 12 14 10 22
Summer 20-21
i.
Construct a divided-difference table for the above data.
ii. Find the polynomial of least degree that incorporates the values in the table and find 𝑓(8).
iii. Given 𝑔(8) = 1202, find the polynomial g (x) that also takes the values of the above
table.
iv. Use Lagrange interpolation formula to find
a. a real root of 𝑓(𝑥) = 0 using linear approximation.
b. a real root of 𝑓(𝑥) = 0 using all the points.
v. Write down MATLAB codes using “polyfit(x, y, n)” and “polyval(p, x)” to plot the
figure showing fitted polynomial and the given points.
Summer 20-21
Curve Fitting
6.6 Introduction
The purpose of curve fitting is to find the parameters values of the model function that closely
match the data’s. The fitted curves can be used to estimate the values of one variable corresponding
to the specified values of the other variable. The method of least squares may be one of the most
systematic procedure to fit a curve through the given data points. In polynomial interpolation we
have considered the problem of finding polynomial of least degree which agree with the tabulated
data’s. Spline interpolation is a form of polynomial interpolation where the interpolant is a
piecewise polynomial called spline. This means that between two points there is a piecewise
polynomial curve which joined smoothly to the neighboring curves. Cubic spline has different
important applications. One of the important applications is in Computer graphics.
The method of least squares may be one of the most systematic procedures to fit a curve through
given data points.
Consider the problem of fitting a set of n data points
( xr , yr ), r = 1.2.3, , n
to a curve Y = f (x) whose values depends on m parameters c1, c2 , c3 , , cm . The values of the
function at a point depends on the values of the parameter involved. In least square method we
determine a set of values of the parameter c1, c2 , c3 , , cm such that the sum of the squares of the
error
n
E (c1 , c 2 , , c m ) = f ( xi , c1 , c 2 , , c m ) − yi 2
ii =1
is minimum.
The necessary conditions for E to have a minimum is that
E
= 0, r = 1,2,3, , m
cr
This condition gives a system of m equations, called normal equations, in m unknowns
c1, c2 , c3 , , cm .
If the parameters appear in the function in non-linear form, the normal equations become non-
linear and are difficult to solve. This difficulty may be avoided if f (x) is transformed to a form
which is linear in parameters.
n
Note that 1 = n .
i =1
Summer 20-21
Example 6.5
Given the following set of values of x and y:
X 1 2 3 4 5 6
Y 1.553 1.638 0.685 −0.428 −0.679 0.164
Solution
t (yrs) 3 6 9 12 15
H (ft ) 2.87 3.60 4.28 4.88 5.35
Solution
6.45
i. The curve 𝐻 = 1+𝑎 is to be fitted to the given data.
2 exp(−𝑎3 𝑡)
6.45
The equation of the curve can be rewritten as 𝐻
− 1 = 𝑎2 𝑒 −𝑎3𝑡
6.45
Taking logarithm of both sides, we get ln ( 𝐻 − 1) = ln 𝑎2 − 𝑎3t,
which can be written in the form
Y = A + BX
6.45
where 𝑌 = ln ( − 1) , 𝐻 = 𝑡, 𝐴 = ln 𝑎2 and 𝐵 = −𝑎3 .
𝐻
N T H X Y XY X2
1 3 2.87 3 0.221 0.663 9
2 6 3.60 6 -0.234 -1.402 36
3 9 4.28 9 -0.679 -6.113 81
4 12 4.88 12 -1.134 -13.609 144
5 15 5.35 15 -1.582 -23.727 225
Sum 45 -3.408 -44.187 495
Normal Equations
5 A + 45 B = -3.408
45 A + 495 B = -44.187
B = -0.150 a3 = 0.15
A = 0.668 a2 = 1.95
6.45
iv. The fitting curve is 𝐻 = 1+1.45 exp(−0.15𝑡) .
From the equation of the curve, we get
when 𝑡 = 20 then 𝐻 = 5.88.
v. >> xd=[3 6 9 12 15]; % state x-values
>> yd=[2.87 3.60 4.28 4.88 5.35]; % staet y-values
Define fitting curve in terms of parameters as vector a
>> fun=@(a,xd) a(1)./(1+a(2).*exp(-a(3).*xd));
>> a0=[6,2,0.2]; % guess parameter values
% To fit the curve use MATLAB function lsqcurvefit with following syntax
>> a=lsqcurvefit(Fd,a0,xd,yd)
Summer 20-21
Exercise 6.2
1. Find the least square line 𝑣 = 𝑏 + 2𝑎𝑡 to the following data (where 𝑏, 𝑎 are constants)
t 0 2 4 5 7
v 6.7 9.2 11.5 15.6 19.2
7
2. Average price, P, of a certain type of second-hand car is believed to be related to its age,t
years, by an equation of the form
50
𝑃= 𝑡
𝑎+𝑏𝑒 4
wherea and b are constants. Data from a recent newspaper give the following average price
(in Taka) for used car of this type,
t (yrs) 2 4 6 8
P (lac) 20.50 17.25 14.50 11.75
3. A bowl of hot water is kept in a room of constant temperature 250C. At 5 minutes interval
temperature of the water is recorded and listed as given below.
t in minute 5 10 15 20 25
T in 0C 76.8 70.4 64.2 58.8 54.1
The law of cooling can be assumed to be of the form 𝑇 = 27 + 𝑎𝑒 −𝑘𝑡 .
(i) Find, to 2 significant figures, the best values of a and k.
(ii) Estimate the initial temperature.
(iii) Estimate the time, to the nearest minute, when the temperature of the water in the bowl
will be 500C.
4. The equation 𝑣 = 70 − 𝑐𝑒 −𝑘𝑡 can be used for calculating the speed of a moving car, where
c and k are constants.
The table below shows the speed of the car at various times
t 4 8 12 16 20
v 23.21 28.52 33.07 36.96 40.29