# MATLAB: Interpolation Using Lagrange’s Formula

Assignment 01:

Write a program to find out y(1) for the following tabular data:
 x 3 4 5 6 7 8 9 y 2.7 6.4 12.5 21.6 34.3 51.2 72.9

Solution:

MATLAB script for solving this problem:

clc
close all
clear all
x=[3 4 5 6 7 8 9];
y=[2.7 6.4 12.5 21.6 34.3 51.2 72.9];
n=length(x);
x1=1;
p=0;
for j=1:n
p1=1;
for k=1:n
if (k~=j)
p1= p1*((x1-x(k))/(x(j)-x(k)));
end
end
p=p+p1*y(j);
end
fprintf('The result is %.3f \n',p);

Output:
The result is 0.100

Recheck: y(7)
clc
close all
clear all
x=[1 3 4 5 6 8 9];
y=[0.1 2.7 6.4 12.5 21.6 51.2 72.9];
n=length(x);
x1=7;
p=0;
for j=1:n
p1=1;
for k=1:n
if (k~=j)
p1= p1*((x1-x(k))/(x(j)-x(k)));
end
end
p=p+p1*y(j);
end
fprintf('The result is %.3f \n',p);

Output:
The result is 34.300

Assignment 02:

Write a program to find out y(3.5) for the following tabular data
 X 0 1 2 3 4 5 6 7 y 2 3 6 11 18 27 38 51

Solution:
MATLAB script for solving this problem:
clc
close all
clear all
x=[0 1 2 3 4 5 6 7];
y=[2 3 6 11 18 27 38 51];
n=length(x);
x1=3.5;
P=0;
for j=1:n
P1=1;
for k=1:n
if(k~=j)
P1=P1*((x1-x(k))/(x(j)-x(k)));
end
end
P=P+P1*y(j);
end
fprintf('The result is %.3f \n',P);

Output:
The result is 14.250
Recheck: y(2)
clc
close all
clear all
x=[0 1 3 3.5 4 5 6 7];
y=[2 3 11 14.25 18 27 38 51];
n=length(x);
x1=2;
P=0;
for j=1:n
P1=1;
for k=1:n
if(k~=j)
P1=P1*((x1-x(k))/(x(j)-x(k)));
end
end
P=P+P1*y(j);
end
fprintf('The result is %.3f \n',P);
Output:
The result is 6.000

Assignment 03:
Write a program to find out y(1.3) for the following tabular data
 X 0 1 2 3 4 5 6 y 0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.27

Solution:
MATLAB script for solving this problem:
clc
close all
clear all
x=[0 1 2 3 4 5 6 ];
y=[0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.27];
n=length(x);
x1=1.3;
P=0;
for j=1:n
P1=1;
for k=1:n
if(k~=j)
P1=P1*((x1-x(k))/(x(j)-x(k)));
end
end
P=P+P1*y(j);
end
fprintf('The result is %.4f \n',P);

Output:
The result is 0.9669

Recheck: y (4)
clc
close all
clear all
x=[0 1 1.3 2 3 5 6 ];
y=[0 0.8415 0.9669 0.9093 0.1411 -0.9589 -0.27];
n=length(x);
x1=4;
P=0;
for j=1:n
P1=1;
for k=1:n
if(k~=j)
P1=P1*((x1-x(k))/(x(j)-x(k)));
end
end
P=P+P1*y(j);
end
fprintf('The result is %.4f \n',P);

Output:
The result is -0.7569