MATLAB: Interpolating a table of data by Newton’s forward and backward difference interpolation formula


Problem :
            Write a program to find out y(10) and y(1) for the following tabular data:
x
x0 = 3
x1 = 4
x2 = 5
x3 = 6
x4 = 7
x5 = 8
x6 =9
y
y0 = 2.7
y1 = 6.4
y2 = 12.5
y3 = 21.6
y4 = 34.3
y5 = 51.2
y6 = 72.9


Solution:
Input: for y(1)
clc
clear all
close all

x = [3 4 5 6 7 8 9];
y = [2.7 6.4 12.5 21.6 34.3 51.2 72.9];

n = length(x);
p=1;

d(:,1)=y’;

for j = 2:n
    for k = j:n
        d(k,j)=(d(k-1,j-1) – d(k,j-1))/(x(k-j+1)-x(k));
    end
end

a = diag(d)’;
df(1)=1;
c(1) = a(1);

for j = 2:n
    df(j) = (p-x(j-1))*df(j-1);
    c(j) = a(j) * df(j);
end
fp = sum(c)
Output:
fp = 0.1000



Input: for y(10)
clc
clear all
close all

x = [3 4 5 6 7 8 9];
y = [2.7 6.4 12.5 21.6 34.3 51.2 72.9];

n = length(x);
p=10;

d(:,1)=y’;

for j = 2:n
    for k = j:n
        d(k,j)=(d(k-1,j-1) – d(k,j-1))/(x(k-j+1)-x(k));
    end
end

a = diag(d)’;
df(1)=1;
c(1) = a(1);

for j = 2:n
    df(j) = (p-x(j-1))*df(j-1);
    c(j) = a(j) * df(j);
end
fp = sum(c)

Output:
fp = 100.0000


Previous Post Next Post