Sunteți pe pagina 1din 10

PROGRAM FOR LAGRANGE’S INTERPOLATION

n=input('enter the no. of elements n');


for i=1:n
x(i)=input('enter the value of x:');
y(i)=input('enter the value of y:');
end
xg=input('enter the value of xg:');
yg=0;
for j=1:n
nu=1;
de=1;
for i=1:n
if(i~=j)
nu=nu*(xg-x(i));
de=de*(x(j)-x(i));
end
end
l(j)=nu/de;
fprintf('\n L%f=%f',j,l(j));
yg=yg+l(j)*y(j);
end
fprintf('The final value of yg:%f',yg);

enter the no. of elements n4

enter the value of x:0

enter the value of y:2

enter the value of x:1

enter the value of y:3

enter the value of x:2

enter the value of y:12

enter the value of x:5

enter the value of y:147

enter the value of xg:1.5

The final value of yg:6.125000>>


PROGRAM FOR NEWTON FORWARD DIFFERENCE METHOD
n=input('enter the no. of elements n');
for i=1:n
x(i)=input('enter the value of x:');
y(i,1)=input('enter the value of y:');
end
xg=input('enter the value of xg:');
h=x(2)-x(1);
c=n-1;
for j=2:n
c=c-1;
for i=1:c+1
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
end
c=n;
for i=1:n-1
fprintf('\n\n');
c=c-1;
for j=2:c+1
fprintf ('\t%f',y(i,j));
end
end
u=(xg-x(1))/h;
ans=y(1,1)+u*y(1,2);
yg=0;
m=2;
u1=u;
for k=3:n
if(m<k)
u=u*(u1-(m-1));
end
fact=1;
for i=1:k-1
fact=fact*i;
end
yg=yg+((u/fact)*y(1,k));
m=m+1;
end
yg=ans+yg;
fprintf('\nThe final value of yg:%f',yg);

enter the no. of elements n8


enter the value of x:2
enter the value of y:19
enter the value of x:3
enter the value of y:48
enter the value of x:4
enter the value of y:99
enter the value of x:5
enter the value of y:178
enter the value of x:6
enter the value of y:291
enter the value of x:7
enter the value of y:444
enter the value of x:8
enter the value of y:643
enter the value of x:9
enter the value of y:894
enter the value of xg:3.5

29.000000 22.000000 6.000000 0.000000 0.000000 0.000000


0.000000

51.000000 28.000000 6.000000 0.000000 0.000000 0.000000

79.000000 34.000000 6.000000 0.000000 0.000000

113.000000 40.000000 6.000000 0.000000

153.000000 46.000000 6.000000

199.000000 52.000000

251.000000
The final value of yg:70.375000>>
PROGRAM FOR NEWTON BACKWARD DIFFERENCE METHOD
n=input('enter the no. of elements n');
for i=1:n
x(i)=input('enter the value of x:');
y(i,1)=input('enter the value of y:');
end
xg=input('enter the value of xg:');
h=x(2)-x(1);
c=1;
for j=2:n
c=c+1;
for i=n:-1:c
y(i,j)=y(i,j-1)-y(i-1,j-1);
end
end
c=1;
for i=2:n
fprintf('\n\n');
c=c+1;
for j=2:c
fprintf ('\t%f',y(i,j));
end
end
u=(x(n)-xg)/h;
ans=y(n,1)-u*y(n,2);
yg=0;
m=2;
u1=u;
for k=3:n
if(m<k)
u=u*(u1-(m-1));
end
fact=1;
for i=1:k-1
fact=fact*i;
end
if (mod (k,3)==0)
yg=yg+((u/fact)*y(n,k));
else
yg=yg-((u/fact)*y(n,k));
end
m=m+1;
end
yg=ans+yg;
fprintf('\nThe final value of yg:%f',yg);

enter the no. of elements n5


enter the value of x:0.1
enter the value of y:1.4
enter the value of x:0.2
enter the value of y:1.56
enter the value of x:0.3
enter the value of y:1.76
enter the value of x:0.4
enter the value of y:2
enter the value of x:0.5
enter the value of y:2.28
enter the value of xg:0.25
0.160000

0.200000 0.040000

0.240000 0.040000 0.000000

0.280000 0.040000 -0.000000 -0.000000


The final value of yg:1.655000>>
PROGRAM FOR NEWTON FORWARD DIFFERENCE (DIFFERENTIATION) METHOD

n=input('enter the no. of elements n');


for i=1:n
x(i)=input('enter the value of x:');
y(i,1)=input('enter the value of y:');
end
xg=input('enter the value of xg:');
h=x(2)-x(1);
c=n-1;
for j=2:n
c=c-1;
for i=1:c+1
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
end
c=n;
for i=1:n-1
fprintf('\n\n');
c=c-1;
for j=2:c+1
fprintf ('\t%f',y(i,j));
end
end
for i=1:n
if (xg==x(i))
m=i;
end
end
ans=y(m,2);
dydx=0;
for k=3:n
if(mod(k,2)~=0)
dydx=dydx-((y(m,k))/(k-1));
else
dydx=dydx+((y(m,k))/(k-1));
end
end
dydx=ans+dydx;
dydx=dydx/h;
fprintf('\nThe value dydx=%f',dydx);

enter the no. of elements n6


enter the value of x:1.5
enter the value of y:3.375
enter the value of x:2
enter the value of y:7
enter the value of x:2.5
enter the value of y:13.625
enter the value of x:3
enter the value of y:24
enter the value of x:3.5
enter the value of y:38.875
enter the value of x:4
enter the value of y:59
enter the value of xg:1.5
3.625000 3.000000 0.750000 0.000000 0.000000

6.625000 3.750000 0.750000 0.000000

10.375000 4.500000 0.750000

14.875000 5.250000

20.125000
The value dydx=4.750000>>
PROGRAM FOR NEWTON BACKWARD DIFFERENCE (DIFFERENTIATION) METHOD
n=input('enter the no. of elements n');
for i=1:n
x(i)=input('enter the value of x:');
y(i,1)=input('enter the value of y:');
end
xg=input('enter the value of xg:');
h=x(2)-x(1);
c=1;
for j=2:n
c=c+1;
for i=n:-1:c
y(i,j)=y(i,j-1)-y(i-1,j-1);
end
end
c=1;
for i=2:n
fprintf('\n\n');
c=c+1;
for j=2:c
fprintf ('\t%f',y(i,j));
end
end
for i=n:-1:1
if (xg==x(i))
m=i;
end
end
ans=y(m,2);
dydx=0;
for k=3:n
dydx=dydx+((y(m,k))/(k-1));
end
dydx=ans+dydx;
dydx=dydx/h;
fprintf('\nThe value dydx=%f',dydx);
enter the no. of elements n5
enter the value of x:0
enter the value of y:1
enter the value of x:10
enter the value of y:0.984
enter the value of x:20
enter the value of y:.939
enter the value of x:30
enter the value of y:0.866
enter the value of x:40
enter the value of y:0.766
enter the value of xg:40

-0.016000

-0.045000 -0.029000

-0.073000 -0.028000 0.001000

-0.100000 -0.027000 0.001000 -0.000000


The value dydx=-0.011317>>
PROGRAM FOR CUBIC SPLINE INTERPOLATION

n=input('enter the no. of points n');


for i=1:n
x(i)=input('enter the value of x:');
y(i,1)=input('enter the value of y:');
end
ar(1)=6*(y(3)-2*y(2)+y(1));
ar(1)=6*(y(4)-2*y(3)+y(2));
xg=input('enter the value of xg:');
m(1)=0;
m(4)=0;
m(2)=((ar(1)*4)-ar(2))/15;
m(3)=ar(1)-4*m(2);
if (xg<x(2))
range=1;
else
range=3;
end
i=range;
fprintf('i=%f',i);
h=x(2)-x(1);
yg=(1/(6*h))*((power((x(i+1)-xg),3))*m(i)+power((xg-x(i)),3)*m(i+1)
+(x(i+1)-xg)*(6*y(i)-h*h*m(i))+((xg-x(i))*(6*y(i+1)-h*h*m(i+1))));
fprintf('\nThe final value of yg:%f',yg);

enter the no. of points n4


enter the value of x:1
enter the value of y:0
enter the value of x:2
enter the value of y:0.3
enter the value of x:3
enter the value of y:0.48
enter the value of x:4
enter the value of y:0.6
enter the value of xg:2.3
i=2.000000 yg=0.366180>>
PROGRAM FOR INVERSE INTERPOLATION
n=input('enter the no. of elements n');
for i=1:n
y(i)=input('enter the value of y:');
x(i)=input('enter the value of x:');
end
yg=input('enter the value of yg:');
xg=0;
for j=1:n
nu=1;
de=1;
for i=1:n
if(i~=j)
nu=nu*(yg-y(i));
de=de*(y(j)-y(i));
end
end
l(j)=nu/de;
fprintf('\n L%f=%f',j,l(j));
xg=xg+l(j)*x(j);
end
fprintf('The final value of xg:%f',xg);

enter the no. of elements n4


enter the value of y:0
enter the value of x:0
enter the value of y:1
enter the value of x:1
enter the value of y:7
enter the value of x:2
enter the value of y:25
enter the value of x:3
enter the value of yg:2

L1.000000=-0.657143
L2.000000=1.597222
L3.000000=0.060847
L4.000000=-0.000926
The final value of xg:1.716138>>

S-ar putea să vă placă și