Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Method-I

Program - 2
Aim: To implement Newton’s Forward Interpolation formula.
Problem: To find the value of f(x) corresponding to the given value of x in the predefined
interval.
x 0 2 4 7 10 12
f(x) 20 20 12 7 6 6
Principle:
Consider a function, y=f(x)
Evaluating the function value for x=x0+ph

where
x0 - initial value of x
y0 - initial value of function f(x)

, x - value at which the function value is to be evaluated


Program:
%Newton’s Forward Difference Formula MATLAB Program
function [dt,fx]=newforinter(x)
clc;
clear;
x=[0 2 4 7 10 12]; % inputting values of x
fx=[20 20 12 7 6 6]; % inputting values of y
dt=zeros(6,10); % function
for i=1:6
dt(i,1)=x(i);% for loop
dt(i,2)=fx(i); % calling function
end
n=5; % number of iterations
for j=3:10
for i=1:n
dt(i,j)=dt(i+1,j-1)-dt(i,j-1);
end
n=n-1;
end
h=x(2)-x(1) % finding the value of h
% Enter the value of xp other that defined initial value of 'x'
xp=input('Enter the value of x:');
for i=1:5
q=(xp-x(i))/h; % calculating number of intervals
if (q>0&&q<1)
p=q;
end
end
l=xp-(p*h)
for i=1:5
if(l==x(i))
r=i;
end
end % calculating different value of y
f0=fx(r);
f01=dt(r,3);
f02=dt(r,(3+1));
f03=dt((r),(3+2));
f04=dt((r),(3+3));
% using the forward interpolation formula
fp=(f0)+((p*f01)+(p*(p-1)*f02)/(2)) + ((p*(p-1)*(p-2)*f03)/(6)) +
((p*(p-1)*(p-2)*(p-3)*f04)/(24))

Output:

h=

Enter the value of x:11

l=

10

fp =

ans =

0 20 0 -8 11 -10 6 0 0 0
2 20 -8 3 1 - 4 0 0 0 0
4 12 -5 4 -3 0 0 0 0 0
7 7 -1 1 0 0 0 0 0 0
10 6 0 0 0 0 0 0 0 0
12 6 0 0 0 0 0 0 0 0

Mapping:
Programming in MATLAB needs the conceptual knowledge and logical thinking for obtaining
the solution for a given problem. Hence the program maps with CO-1, PO-1, PO-5, PO-9, and
PO-10, PO-12.
Method -II
Program - 2
Aim: To implement Newton’s Forward and Backward Interpolation formula.

x 0 2 4 6
f(x) 5 29 125 341

find f(x) value at x=1.5


close all
n=input(' \n enter number of data points =')
xg=input(' \n enter x for which value of y is calculation =')
for i=1:n
fprintf('\n x %d = %f ', i)
x(i)=input(' ');
fprintf('\n y %d = %f ', i);
y(i,1)=input(' ');
end
h=x(2)-x(1);
u=(xg-x(1))/h
for j=2:n
for i= 1:n-j+1
y(i,j)=y(i+1,j-1)-y(i,j-1)
end
end
yg=y(1);
u1=1;
for j=1:n-1
u1=u1*(u-j+1)/j;
yg=yg+u1*y(1,j+1);
end
fprintf('\n value of yg= % f at xg=%f',yg,xg)
Output
enter number of data points 4
enter x for which value of y is calucation1.5

enter number of data points =4

n=

enter x for which value of y is calculation =1.5

xg =

1.5000

x1= 0

y1= 5

x2= 2

y 2 = 29

x3= 4
y 3 = 125

x4= 6

y 4 = 341

u=

0.7500

y=

5 24
29 0
125 0
341 0

y=

5 24
29 96
125 0
341 0

y=

5 24
29 96
125 216
341 0

y=

5 24 72
29 96 0
125 216 0
341 0 0

y=

5 24 72
29 96 120
125 216 0
341 0 0

y=

5 24 72 48
29 96 120 0
125 216 0 0
341 0 0 0

value of yg= 18.125000 at xg=1.500000


or
check the answer interpolation method
x=[0 2 4 6];
y=[5 29 125 341];
xg=1.5;
yg=interp1(x,y,xg,'spline')
yg = 18.1250

You might also like