You are on page 1of 2

%Program for Newton's Forward Difference Interpolation.

%Academic Year:
%Roll No:
%Batch:
clc;
clear all;
n=input('Enter the number of data points: ');
x=zeros(10);
y=zeros(10,10);
for i=1:n
fprintf('Enter the value of x(%d):',i);
x(i)=input('');
fprintf('Enter the corresponding value of y(%d):',i);
y(i)=input('');
end
disp(y);
xg=input('Enter the xg term: ');
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
yg=yg+((u/factorial(m))*y(1,k));
m=m+1;
end
yg=Ans+yg;
fprintf('\nthe final value of yg=%f',yg);
output:
Enter the number of data points: 5
Enter the value of x(1):0
Enter the corresponding value of y(1):1
Enter the value of x(2):1
Enter the corresponding value of y(2):5
Enter the value of x(3):2
Enter the corresponding value of y(3):25
Enter the value of x(4):3
Enter the corresponding value of y(4):100
Enter the value of x(5):4
Enter the corresponding value of y(5):250
Enter the xg term: .5

4.000000 16.000000 39.000000 -19.000000


20.000000 55.000000 20.000000
75.000000 75.000000
150.000000
the final value of yg=4.179688>>
solver;
>> x=[0 1 2 3 4];
>> y=[1 5 25 100 250];
>> interp1(x,y,.5,'spline')
ans =
3.7344

You might also like