Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Numerical Analysis Lab

Lab No.5

Submitted By:
Muhammad Hassan (2020-MC-274)
Submitted to:
Dr. Arshi Khalid
--------------------------------------------------------------------------------------------------------
-

Department of Mechatronics & Control Engineering


University of Engineering & Technology Lahore,
Faisalabad Campus.
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

Date: 21-10-2022

Newton’s Forward Difference Table:-


Matlab Code:
% Newton's Forward Differnce Table
clearvars;
clc;
close all;
x = input('Enter the values of x: ');
y = input('Enter the values of F: ');
y_len = length(y);
x_len = length(x);
if (x_len~=y_len)
fprintf('Invalid Input')
end
xp = input('Enter the number whose value needs to be interpolate: ');
h = x(2) - x(1)
% for value of p
for a=1:x_len
if xp>x(a)
xo = x(a);
p = (xp - xo)/h;
if p>1 || p<0
xo = x(a+1);
elseif p<=1 || p>=0
continue
end
end
end
xo
% for Finite Difference Table
table=zeros(2*y_len-1,y_len+1);
j=1;
% to create a matrix having 1st column of values of x and 2nd column of f
% and others are zero
for a=1:2:2*y_len-1
table(a,1) = x(j);
table(a,2) = y(j);
j = j+1;
end
% to calculate foraward difference table
for col=3:1:y_len+1
for row=col-2:2:2*y_len-col
table(row+1,col) = table(row+2,col-1) - table(row,col-1);
end
end
fprintf("Finite Difference Table")
table
% to locate the position of fo
for a=1:2:2*y_len-1
if table(a,1) == xo
xo_position = a;
end
end
xo_position = xo_position + 1;
% Formula for Newton's Forward Differnce Table
A=y(1); B=p;
for k=1:y_len-1
A=A+B*table(xo_position+1,k+1);
B=(p-k)/(k+1)*B;
end
fprintf('Value of Y(%f)=%f \n',xp,A)

2
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

Output:
If we input
X = [0 0.25 0.5 0.75 1]
Y = [0 0.2763 0.5205 0.7112 0.8427]
Xp = 0.125
Then
Value of Y(0.125000)=0.136144

Drawbacks of above program:


Above program calculates the value of p, determines xo on the basis of p and determines the position of fo.
But there is error while applying the Interpolation formula.

You might also like