You are on page 1of 2

Batch- T2

PROGRAM FOR NEWTON’S FORWARD DIFFERENCE METHOD

n=input('\n Enter the number of elements=');


for i=1:n
x(i)=input('\n Enter the value of x=');
y(i,1)=input('\n Enter the value of y=');
end
xg=input('\n 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(i,k));
m=m+1;
end
yg=ans+yg;
fprintf('\n yg=%f',yg);

PROGRAM OUTPUT

Enter the number of elements=8 Enter the value of y=48


Enter the value of x=2 Enter the value of x=4
Enter the value of y=19 Enter the value of y=99
Enter the value of x=3 Enter the value of x=5
Enter the value of y=178 Enter the value of x=8
Enter the value of x=6 Enter the value of y=643
Enter the value of y=291 Enter the value of x=9
Enter the value of x=7 Enter the value of y=894
Enter the value of y=444 Enter the value of xg=3.5
c=
7
29.000000 22.000000 6.000000 0.000000 0.000000 0.000000
0.000000
c=
6
51.000000 28.000000 6.000000 0.000000 0.000000 0.000000
c=
5
79.000000 34.000000 6.000000 0.000000 0.000000
c=
4
113.000000 40.000000 6.000000 0.000000
c=
3
153.000000 46.000000 6.000000
c=
2
199.000000 52.000000
c=
1
251.000000

yg=72.625000>>

SOLVER

x=[2 3 4 5 6 7 8 9]
x=
2 3 4 5 6 7 8 9
>> y=[19 48 99 178 291 444 643 894]
y=
19 48 99 178 291 444 643 894
>> interp1(x,y,3.5,'spline')
ans =70.3750

You might also like