0% found this document useful (0 votes)
60 views10 pages

King Ooo 11111

The document contains MATLAB code for polynomial interpolation using Lagrange, Newton divided difference, Newton forward divided difference, and Newton backward divided difference methods. It includes examples of each method applied to different data sets of x and y values. The code calculates the interpolating polynomial, plots the polynomial and error values for each example.

Uploaded by

Muhammad Maaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views10 pages

King Ooo 11111

The document contains MATLAB code for polynomial interpolation using Lagrange, Newton divided difference, Newton forward divided difference, and Newton backward divided difference methods. It includes examples of each method applied to different data sets of x and y values. The code calculates the interpolating polynomial, plots the polynomial and error values for each example.

Uploaded by

Muhammad Maaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

numerical matlab code

M:MAAZ
MT120202037
MATLAB"CODING"
EXAMPLE NO.1
X=[1.0;1.3;1.6;1.9;2.2]
Y=[0.7651977;0.6200860;0.4554022;0.2818186;0.1103623]
LAGRANGE INTERPOLATION:
clc
clear all
X=[1.0;1.3;1.6;1.9;2.2];
Y=[0.7651977;0.6200860;0.4554022;0.2818186;0.1103623];
n=length(X);
F=zeros(n,n);
for i=1:n
C=1;
for j=1:n
if i~=j
C=conv(C,poly(X(j)))/(X(i)-X(j));
end
end
F(i,:)=C*Y(i);
end
p=sum(F);
A=polyval(p,1.1)
subplot(2,1,1)
x=1.0:0.5:2.2;
y=polyval(p,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(p, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON DIVIDED DIFFERENCE:


clc
clear all
X=[1.0;1.3;1.6;1.9;2.2];
Y=[0.7651977;0.6200860;0.4554022;0.2818186;0.1103623];
n=length(X);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
F
c=F(n,n);
for k=n-1:-1:1
c=conv(c,poly(X(k)));
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,1.1)
subplot(2,1,1)
x=1.0:0.5:2.2;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON FORWARD DIVIDED DIFFERENCE:


clc
clear all
X=[1.0;1.3;1.6;1.9;2.2];
Y=[0.7651977;0.6200860;0.4554022;0.2818186;0.1103623];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
c=F(n,n);
for k=n-1:-1:1
p=poly(X(1))/h;
p(2)=p(2)-(k-1);
c=conv(c,p)*h^k;
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,1.1)
subplot(2,1,1)
x=1.0:0.5:2.2;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON BACKWARD DIVIDED DIFFERENCE:


clc
clear all
X=[1.0;1.3;1.6;1.9;2.2];
Y=[0.7651977;0.6200860;0.4554022;0.2818186;0.1103623];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=1:n-j+1
F(i,j)=(F(i+1,j-1)-F(i,j-1))/(X(i+j-1)-X(i));
end
end
c=F(1:n)
for k=n-1:-1:1
p=poly(X(n))/h;
p(2)=p(2)+(k-1);
c=conv(c,p)*h^k;
m=length(c);
c(m)=c(m)+F(n-k+1,k);
end
A=polyval(c,2.0)
subplot(2,1,1)
x=1.0:0.5:2.2;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

EXAMPLE NO.2
X=[0.0 0.2 0.4 0.6 0.8]
Y=[1.00000 1.22140 1.49182 1.82212 2.52544]

LAGRANGE INTERPOLATION:
clc
clear all
X=[0.0 0.2 0.4 0.6 0.8];
Y=[1.00000 1.22140 1.49182 1.82212 2.52544];
n=length(X);
F=zeros(n,n);
for i=1:n
C=1;
for j=1:n
if i~=j
C=conv(C,poly(X(j)))/(X(i)-X(j));
end
end
F(i,:)=C*Y(i);
end
p=sum(F);
A=polyval(p,0.1);
subplot(2,1,1)
x=0.0:0.1:0.8;
y=polyval(p,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(p, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON DIVIDED DIFFERENCE:


clc
clear all
X=[0.0 0.2 0.4 0.6 0.8];
Y=[1.00000 1.22140 1.49182 1.82212 2.52544];
n=length(X);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
c=F(n,n);
for k=n-1:-1:1
c=conv(c,poly(X(k)));
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,0.1)
subplot(2,1,1)
x=0.0:0.1:0.8;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON FORWARD DIVIDED DIFFERENCE:


clc
clear all
X=[0.0 0.2 0.4 0.6 0.8];
Y=[1.00000 1.22140 1.49182 1.82212 2.52544];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
F
c=F(n,n);
for k=n-1:-1:1
p=poly(X(1))/h;
p(2)=p(2)-(k-1);
c=conv(c,p)*h^k;
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,0.1)
subplot(2,1,1)
x=0.0:0.1:0.8;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON BACKWARD DIVIDED DIFFERENCE:


clc
clear all
X=[0.0 0.2 0.4 0.6 0.8];
Y=[1.00000 1.22140 1.49182 1.82212 2.52544];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=1:n-j+1
F(i,j)=(F(i+1,j-1)-F(i,j-1))/(X(i+j-1)-X(i));
end
end
c=F(1:n)
for k=n-1:-1:1
p=poly(X(n))/h;
p(2)=p(2)+(k-1);
c=conv(c,p)*h^k;
m=length(c);
c(m)=c(m)+F(n-k+1,k);
end
A=polyval(c,0.7)
subplot(2,1,1)
x=0.0:0.1:0.8;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Absolute Error');
xlabel('X');
ylabel('error_values');
grid on

EXAMPLE NO.3
X=[-1.2;-0.9;-0.6;-0.3;-0.0]
Y=[0.18232;-0.105083;-0.51036;-1.20397;-3.12145]

LAGRANGE INTERPOLATION:
clc
clear all
X=[-1.2;-0.9;-0.6;-0.3;0.0];
Y=[0.18232;-0.105083;-0.51036;-1.20397;-3.12145];
n=length(X);
F=zeros(n,n);
for i=1:n
C=1;
for j=1:n
if i~=j
C=conv(C,poly(X(j)))/(X(i)-X(j));
end
end
F(i,:)=C*Y(i);
end
p=sum(F);
A=polyval(p,0.1);
subplot(2,1,1)
x=-1.0:0.1:0.0;
y=polyval(p,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(p, X) - Y);
plot(X, error_values, 'r-o');
title('Asolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON DIVIDED DIFFERENCE:


clc
clear all
X=[-1.2;-0.9;-0.6;-0.3;0.0];
Y=[0.18232;-0.105083;-0.51036;-1.20397;-3.12145];
n=length(X);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
c=F(n,n);
for k=n-1:-1:1
c=conv(c,poly(X(k)));
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,-0.1)
subplot(2,1,1)
x=-1.0:0.1:0.0;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Asolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON FORWARD DIVIDED DIFFERENCE:


clc
clear all
X=[-1.2;-0.9;-0.6;-0.3;0.0];
Y=[0.18232;-0.105083;-0.51036;-1.20397;-3.12145];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
c=F(n,n);
for k=n-1:-1:1
p=poly(X(1))/h;
p(2)=p(2)-(k-1);
c=conv(c,p)*h^k;
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,-0.1)
subplot(2,1,1)
x=-1.0:0.1:0.0;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Asolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON BACKWARD DIVIDED DIFFERENCE:


clc
clear all
X=[-1.2;-0.9;-0.6;-0.3;0.0];
Y=[0.18232;-0.105083;-0.51036;-1.20397;-3.12145];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=1:n-j+1
F(i,j)=(F(i+1,j-1)-F(i,j-1))/(X(i+j-1)-X(i));
end
end
c=F(1:n)
for k=n-1:-1:1
p=poly(X(n))/h;
p(2)=p(2)+(k-1);
c=conv(c,p)*h^k;
m=length(c);
c(m)=c(m)+F(n-k+1,k);
end
A=polyval(c,-0.2)
subplot(2,1,1)
x=-1.0:0.1:0.0;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Asolute Error');
xlabel('X');
ylabel('error_values');
grid on

EXAMPLE NO.3
X=[0.0;0.2;0.4;0.6];
Y=[15.0;21.0;30.0;51.0];

LAGRANGE INTERPOLATION:
clc
clear all
X=[0.0;0.2;0.4;0.6];
Y=[15.0;21.0;30.0;51.0];
n=length(X);
F=zeros(n,n);
for i=1:n
C=1;
for j=1:n
if i~=j
C=conv(C,poly(X(j)))/(X(i)-X(j));
end
end
F(i,:)=C*Y(i);
end
p=sum(F);
A=polyval(p,0.1);
subplot(2,1,1)
x=0.0:0.1:0.6;
y=polyval(p,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(p, X) - Y);
plot(X, error_values, 'r-o');
title('Asolute Error');
xlabel('X');
ylabel('error_values');
grid on

NEWTON DIVIDED DIFFERENCE:


clc
clear all
X=[0.0;0.2;0.4;0.6];
Y=[15.0;21.0;30.0;51.0];
n=length(X);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
c=F(n,n);
for k=n-1:-1:1
c=conv(c,poly(X(k)));
m=length(c);
c(m)=c(m)+F(k,k);
end
A=polyval(c,0.1)
subplot(2,1,1)
x=0.0:0.1:0.6;
y=polyval(c,x);
plot(x,y,'b-o');
xlabel('X');
ylabel('polynomial');
title('polynomial interpolation');
grid on
subplot(2,1,2)
error_values = abs(polyval(c, X) - Y);
plot(X, error_values, 'r-o');
title('Asolute Error');
xlabel('X');
ylabel('error_values');
grid on

NWETON FORWARD DIVIDED DIFFERENCE:


clc
clear all
X=[0.0;0.2;0.4;0.6];
Y=[15.0;21.0;30.0;51.0];
n=length(X);
h=X(2)-X(1);
F=zeros(n,n);
F(:,1)=Y;
for j=2:n
for i=j:n
F(i,j)=(F(i,j-1)-F(i-1,j-1))/(X(i)-X(i-j+1));
end
end
c=F(n,n);
for k=n-1:-1:1
p=poly(X(1))/h;
p(2

You might also like