Forward interpolation formula
Example: Find u(0.5) if u(0)=225, u(1)=238, u(2)=320, u(3)=340
clear; clc;
x=input("Enter the values for x : ");
y=input("Enter the values for y : ");
n=length(x);
%to construct difference table we create a zero matrix of order n%
d=zeros(n,n);
for i=1:n
d(i,1)=y(i);
end
for i=2:n
for j=i:n
d(j,i)=d(j,i-1)-d(j-1,i-1);
end
end
fprintf("Difference table\n");
Difference table
disp(d)
225 0 0 0
238 13 0 0
320 82 69 0
340 20 -62 -131
%to find the value of y corresponding to x%
x0=input("Enter the value of x for which y is to be found :")
x0 = 0.5000
h=x(2)-x(1);
u=(x0-x(1))/h;
y0=y(1);
sum1=y0
sum1 = 225
for i=1:n-1
f=factorial(i)
p(i)=1;
for j=0:i-1
p(i)=p(i)*(u-j);
end
sum1=sum1+p(i)/f*d(i+1,i+1);
end
f = 1
f = 2
f = 6
1
fprintf("required interpolating value is %0.3f ",sum1);
required interpolating value is 214.688
Backward interpolation formula
Example: Compute f(0.26) if f(0.10)=0.1003, f(0.15)=0.1511, f(0.20)=0.2027, f(0.25)=0.2553, f(0.30)=0.3093
clear; clc;
x=input("Enter the values for x : ");
y=input("Enter the values for y : ");
n=length(x);
%to construct difference table we create a zero matrix of order n%
d=zeros(n,n);
for i=1:n
d(i,1)=y(i);
end
for i=2:n
for j=i:n
d(j,i)=d(j,i-1)-d(j-1,i-1);
end
end
fprintf("Difference table\n");
Difference table
disp(d)
225 0 0 0
238 13 0 0
320 82 69 0
340 20 -62 -131
%to find the value of y corresponding to x%
x0=input("Enter the value of x for which y is to be found :")
x0 = 0.5000
h=x(2)-x(1);
u=(x0-x(n))/h;
y0=y(n);
sum1=y0
sum1 = 340
for i=1:n-1
f=factorial(i)
p(i)=1;
for j=0:i-1
p(i)=p(i)*(u+j);
2
end
sum1=sum1+p(i)/f*d(n,i+1);
end
f = 1
f = 2
f = 6
fprintf("required interpolating value is %0.3f ",sum1);
required interpolating value is 214.688