You are on page 1of 4

Interpolation/Extrapolation using Newton’s forward difference

formula

Example 1: Find 𝒚 𝟏. 𝟒 given that


x 1 2 3 4 5
Y 10 26 58 112 194

Code:
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");
disp(d)

%to find the value of y corresponding to x%


x0=input("Enter the value of x for which y is to be found :")
h=x(2)-x(1);
u=(x0-x(1))/h;
y0=y(1);

Lab Manual Department of Mathematics, NIE, Mysuru P a g e | 13


for i=1:n-1
fact=1; %to find factorial%
for k=1:i
fact=fact*k;
end
p(i)=1.0; %to compute r, r(r-1), r(r-1)(r-2)....%
for j=0:i-1
p(i)=p(i)*(u-j);
end
p(i)=p(i)/fact; %to compute r(r-1)/2!, r(r-1)(r-2)/3! ...%
y0=y0+p(i)*d(i+1,i+1);
end
fprintf("required interpolating value is %f ",y0);

Output:
Enter the values for x :
[1 2 3 4 5]
Enter the values for y :
[10 26 58 112 194]
Difference table
10 0 0 0 0
26 16 0 0 0
58 32 16 0 0
112 54 22 6 0
194 82 28 6 0

Enter the value of x for which y is to be found:


1.4
x0 =1.4000
required interpolating value is 14.864000

Lab Manual Department of Mathematics, NIE, Mysuru P a g e | 14


Exercise:
1. Find 𝑢0.5 from the data:
𝑢0 = 225, 𝑢1 = 238, 𝑢2 = 320, 𝑢3 = 340
Ans: 214.68
2. The area of a circle (A) corresponding to diameter (D) is given below:
D 80 85 90 95 100
A 5026 5674 6362 7088 7854
Find the area corresponding to diameter 105.
Ans: 8666

Interpolation/Extrapolation using Newton’s backward difference


formula

Example 1: If 𝑓 0 = 0, 𝑓 2 = 4, 𝑓 4 = 56, 𝑓 6 = 204, 𝑓 8 = 496,


𝑓 10 = 980. Find 𝑓(5) and 𝑓(7)

Code:
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");

Lab Manual Department of Mathematics, NIE, Mysuru P a g e | 15


disp(d)
%to find the value of y corresponding to x%
x0=input("Enter the value of x for which y is to be found :")
h=x(2)-x(1)
u=(x0-x(n))/h
y0=y(n)
for i=1:n-1
fact=1; %to find factorial%
for k=1:i
fact=fact*k;
end
p(i)=1.0; %to compute r, r(r+1), r(r+1)(r+2)....%
for j=0:i-1
p(i)=p(i)*(u+j);
end
p(i)=p(i)/fact; %to compute r(r+1)/2!, r(r+1)(r+2)/3! ...%
y0=y0+p(i)*d(n,i+1);
end
fprintf("required value is %f ",y0);

Output:

x0=5
required value is 115

x0=7
required value is 329

Exercise: Compute 𝑢14.2 from the following table


𝑢 10 12 14 16 18
𝑢𝑥 0.240 0.281 0.318 0.352 0.384
Ans: 0.3215

Lab Manual Department of Mathematics, NIE, Mysuru P a g e | 16

You might also like