You are on page 1of 8

University of Engineering and Technology Peshawar

Jalozai Campus
Department of Industrial Engineering
Numerical Analysis & Computer Applications Lab
Lab No. : 06
Submitted to: Dr. Iltaf
Submitted by: Umair Ali Shah
Semester: 5th Semester
Registration ID: 18JZIND0105
Submission Date: Dec 26, 2020
Lab 06
To Find the Interpolating Polynomial from the Tabulated Data Using
Newton’s Divided Difference and Lagrange’s Interpolation

Objectives
 To write MATLAB codes for finding the interpolating polynomial from the tabulated
data using Newton’s Divided Difference and Lagrange’s Interpolation.
 Implementation of these methods on engineering case studies using solution of
nonlinear models.

Introduction
In this lab we will find the interpolating polynomial from the data which may or may not be equi-
spaced. Newton’s Divided Difference and Lagrange’s Interpolation applies on both equally and
unequally spaced data. We will then solve some engineering problems using these methods.

Theory
Newton’s Divided Difference Interpolation
Divided differences is a recursive division process. The method can be used to calculate the
coefficients in the interpolation polynomial in the Newton form. Newton’s Divided Difference
Interpolation formula is a technique used to find the polynomial when interval is not equal.

Table 5.1: Divided Difference Table


Let us consider

𝛥𝑦0 = 𝑓[𝑥0 , 𝑥1 ]

𝛥2 𝑦0 = 𝑓[𝑥0 , 𝑥1 , 𝑥2 ]

To make formula simple

𝑓(𝑥) = 𝑦0 + (𝑥 − 𝑥0 )𝛥𝑦0 + (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )𝛥2 𝑦0 + (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )(𝑥 − 𝑥2 )𝛥3 𝑦0 + ⋯

Lagrange’s Interpolation
In numerical analysis, Lagrange polynomials are used for polynomial interpolation. For a given
set of points with no two values equal, the Lagrange polynomial is the polynomial of lowest
degree that assumes at each value the corresponding value, so that the functions coincide at each
point. The best thing in Lagrange’s Interpolation is that we don’t need to find any difference
table.

For four values of x formula can be written as

(𝑥 − 𝑥1 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 )


𝑓(𝑥) = 𝑦0 + 𝑦
(𝑥0 − 𝑥1 )(𝑥0 − 𝑥2 )(𝑥0 − 𝑥3 ) (𝑥1 − 𝑥0 )(𝑥1 − 𝑥2 )(𝑥1 − 𝑥3 ) 1

(𝑥 − 𝑥0 )(𝑥 − 𝑥1 )(𝑥 − 𝑥3 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )(𝑥 − 𝑥2 )


+ 𝑦2 + 𝑦
(𝑥2 − 𝑥0 )(𝑥2 − 𝑥1 )(𝑥2 − 𝑥3 ) (𝑥3 − 𝑥0 )(𝑥3 − 𝑥1 )(𝑥3 − 𝑥2 ) 3

In simple for each value of x which is not listed in nominator, all other values of x will be
subtracted from that. Number of terms will be equal to the number of values in x and y. Factors
in each term will be 1 less than the number of values in x and y.

MATLAB Programs
MATLAB Program for Newton’s Divided Difference Interpolation
% Newton's Divided Difference Interpolation by Umair Ali Shah
clearvars;
clc;
format short;
sympref('FloatingPointOutput',true); % Getting all answers in decimal
syms x;
v=[1 2 3 5]; % Write x here
y=[0 7 26 124]; % Write y here
z=y;
b=zeros(length(y),2+length(y)-1); %Creating difference table
b(:,1)=v'; % Assigning first column to x
b(:,2)=y'; % Assigning second column to y
for i=1:length(y)-1
a=diff(z);
e=zeros(1,length(a));
for c=1:length(a)%finds denominator of each difference value
e(c)=v(i+c)-v(c);
end
dl=a./e;
d(i)=dl(1); % Obtaining only 1st difference in each order
b(1:length(dl),2+i)=dl'; % Putting difference values in table
z=dl;
end
fprintf(' X Y Difference\n')
disp(b)
ip=0;
for c=1:length(y)-1
s=1;
for z=1:c % Single term
s=s*(x-v(z));
end
ip=ip+d(c)*s;% adding all terms
end
f(x)=simplify(y(1)+ip); % adding Yn
disp('Interpolating Polynomial')
disp(f(x))
fplot(f,[-10 10],'-g')
title('Function Plot')
xlabel('X')
ylabel('F(X)')
In this program we just have to enter x and y and program will find the polynomial and its plot.

MATLAB Program for Lagrange’s Interpolation


% Lagrange Interpolavtion by Umair Ali Shah
clearvars;
clc;
format short;
sympref('FloatingPointOutput',true); % Getting all answers in decimal
syms x;
v=[1 2 -4]; % Write x here
y=[3 -5 -4]; % Write y here
ip=0;
for i=1:length(y)
n=prod(x-v(v~=v(i))); %nominator
d=prod(v(i)-v(v~=v(i))); % denominator
ip=ip+double(y(i)/d)*n; % Adding all terms
end
f(x)=simplify(ip); % simplifying polynomial
disp('Interpolating Polynomial')
disp(f(x))
fplot(f,[-10 10],'-g')
title('Function Plot')
xlabel('X')
ylabel('F(X)')
In this program we just have to enter x and y and program will find the polynomial and its plot.

Problems
Problem 1
Using polynomial interpolation, with the aid of Newton's Divided Difference Formula, find a
value for y (– 1) and the polynomial f (x) from the following data:
x –2 0 1 2

y 4 10 10 16

Solution
Enter the values of x in v, y in y and run the file.

Results
X Y Difference

-2 4 3 -1 1

0 10 0 3 0

1 10 6 0 0

2 16 0 0 0

Interpolating Polynomial

x^3 - x + 10

𝒇(𝒙) = 𝒙𝟑 − 𝒙 + 𝟏𝟎

To find the y (– 1) enter the f (– 1) in command window.

>> f(-1)

ans =

10

𝒀(−𝟏) = 𝟏𝟎
Figure 6.1: Function Plot for Problem 1

Problem 2
Interpolate the function value at x = 2 from the following data using the Lagrange polynomial.

x 0 1 3 4

y 8 9 35 72

Solution
Enter the values of x in v, y in y and run the file.

Results
Interpolating Polynomial

x^3 + 8

𝒇(𝒙) = 𝒙𝟑 + 𝟖

To find the y (2) enter the f (2) in command window.

>> f(2)

ans =

16

𝒀(𝟐) = 𝟏𝟔
Figure 6.2: Function Plot for Problem 2

Engineering Problem
Use a Lagrange interpolating polynomial to evaluate the density of unused motor oil at T = 15 °C
based on the following data:

Temperature (x) Density, F(x)

0 3.850

20 0.800

40 0.212

Solution
Enter the temperature as x, density as y and run the file.

Results
Interpolating Polynomial

0.0031*x^2 - 0.2140*x + 3.8500

𝒇(𝒙) = 𝟎. 𝟎𝟎𝟑𝟏𝒙𝟐 − 𝟎. 𝟐𝟏𝟒𝒙 + 𝟑. 𝟖𝟓

To find the y (15) enter the f (15)) in command window.


>> f(15)

ans =

1.3317

𝒀(𝟐𝟎) = 𝟏. 𝟑𝟑𝟏𝟕

At temperature T=15 oC density is 1.3317 units.

Figure 6.3: Temperature vs Density Plot

Conclusion
From this lab we concluded that a polynomial can be created from the set of data using
interpolation which can be further used to find the unknown values using interpolating
polynomial. MATLA makes it more easy by removing lengthy calculation time and gives
accurate results.

You might also like