0% found this document useful (0 votes)
168 views

Matlab Code Interpollation

The document contains Matlab code for implementing two interpolation methods: Newton's Gregory backward interpolation and Sterling or central difference formula. The code takes user input for the number of data points and their x and y values, calculates interpolation coefficients, and outputs the interpolation polynomial function for the given method.

Uploaded by

fake7083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views

Matlab Code Interpollation

The document contains Matlab code for implementing two interpolation methods: Newton's Gregory backward interpolation and Sterling or central difference formula. The code takes user input for the number of data points and their x and y values, calculates interpolation coefficients, and outputs the interpolation polynomial function for the given method.

Uploaded by

fake7083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Remaining Codes are also compiled on the similar guidelines, and only Matlab Script

files are provided below: -

Newtons Gregory Backward Interpolation Method

clear all
clc
s = "This programme is valid for 'n' elements of array, where n = 1,2,3,..,10";
disp(s)
syms x v f f(x)
n = input('Enter Array Length = ');
M = zeros(n,n+1);
for i=1:n
M(i,1) = input((['Enter X' int2str(i) ' Value of Array = ']));
M(i,2) = input((['Enter Y' int2str(i) ' Value of Array = ']));
end
for j=3:n+1
for i=j-1:n
M(i,j) = M(i,j-1)-M(i-1,j-1);
end
end
disp(M)
h = M(n,1)-M(n-1,1);
v = (x-M(n,1))/h;
N = zeros(n,1);
for i=1:n
% j=i-1;
N(i,1) = M(n,i+1);
end
disp(N)
if n==1
f = N(1,1);
elseif n==2
f = N(1,1) + N(2,1)*v;
elseif n==3
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2));
elseif n==4
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3));
elseif n==5
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3)) +
N(5,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(1/factorial(4));
elseif n==6
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3)) +
N(5,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(1/factorial(4)) +
N(6,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(1/factorial(5));
elseif n==7
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3)) +
N(5,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(1/factorial(4)) +
N(6,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(1/factorial(5)) +
N(7,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(1/factorial(6));
elseif n==8
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3)) +
N(5,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(1/factorial(4)) +
N(6,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(1/factorial(5)) +
N(7,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(1/factorial(6)) +
N(8,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(v+7)*(1/factorial(7));
elseif n==9
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3)) +
N(5,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(1/factorial(4)) +
N(6,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(1/factorial(5)) +
N(7,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(1/factorial(6)) +
N(8,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(v+7)*(1/factorial(7)) +
N(9,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(v+7)*(v+8)*(1/factorial(8));
elseif n==10
f = N(1,1) + N(2,1)*v + N(3,1)*v*(v+1)*(1/factorial(2)) +
N(4,1)*v*(v+1)*(v+2)*(1/factorial(3)) +
N(5,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(1/factorial(4)) +
N(6,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(1/factorial(5)) +
N(7,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(1/factorial(6)) +
N(8,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(v+7)*(1/factorial(7)) +
N(9,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(v+7)*(v+8)*(1/factorial(8)) +
N(10,1)*v*(v+1)*(v+2)*(v+3)*(v+4)*(v+5)*(v+6)*(v+7)*(v+8)*(v+9)*(1/factorial(9));
end
f(x) = expand(f); disp(f(x))
w = input((['Enter the Value of "X" for interpolation = '])); x = subs(w); f(x)
Sterling or Central Difference Formula

clear all
clc
s = "This programme is valid for 'n' elements of array, where n = 1,2,3,..,5";
disp(s)
syms x u f f(x)
n = input('Enter Array Length = ');
M = zeros(n,n+1);
for i=1:n
M(i,1) = input((['Enter X' int2str(i) ' Value of Array = ']));
M(i,2) = input((['Enter Y' int2str(i) ' Value of Array = ']));
end
for j=3:n+1
for i=j-1:n
M(i,j) = M(i,j-1)-M(i-1,j-1);
end
end
disp(M)
h = M(2,1)-M(1,1);
y = round(n/2);
if mod(y,2)==0
m = n-1;
else
m=n;
end
p = (x-M(y,1))/h;
N = zeros(n,2);

for i=1:m
j=i+1;
if j == 2
N(i,1) = M(y,j);
elseif j==3
N(i,1) = M(y,j);
N(i,2) = M(y+1,j);
elseif j==4
N(i,1) = M(y+1,j);
elseif j==5
N(i,1) = M(y+1,j);
N(i,2) = M(y+2,j);
else
N(i,1) = M(y+2,j);
end
end
disp(N);
if n==1
f = N(1,1);
elseif n==2
f = N(1,1) + (1/2)*p*(N(2,1)+N(2,2));
elseif n==3
f = N(1,1) + (1/2)*p*(N(2,1)+N(2,2)) + (1/factorial(2))*(p^2)*(N(3,1));
elseif n==4
f = N(1,1) + (1/2)*p*(N(2,1)+N(2,2)) + (1/factorial(2))*(p^2)*(N(3,1)) + (1/
(factorial(3)*2))*p*((p^2)-1)*(N(4,1)+N(4,2));
elseif n==5
f = N(1,1) + (1/2)*p*(N(2,1)+N(2,2)) + (1/factorial(2))*(p^2)*(N(3,1)) + (1/
(factorial(3)*2))*p*((p^2)-1)*(N(4,1)+N(4,2)) + (1/factorial(4))*(p^2)*((p^2)-
1)*(N(5,1));
end
f(x) = expand(f); disp(f(x))
w = input((['Enter the Value of "X" for interpolation = '])); x = subs(w); f(x)

You might also like