Professional Documents
Culture Documents
Course No.: EEE 212 Group No.:06 Course Title: Numerical Technique Laboratory
Course No.: EEE 212 Group No.:06 Course Title: Numerical Technique Laboratory
: EEE 212
Group No.:06
Objective:
where
CODE:
clc;
clear all;
close all;
x = [-1 0 1 2 3];
y = [2 1 -2 3 6];
n = length(x);
%calculating the divided difference table. The first column is
done separately to maintain the next loop's initializations.
for i=1:n
d(i,1) = y(i);
end
for j=2:n
for k=j:n
d(k,j)=(d(k,j-1)-d(k-1,j-1))/(x(k)-x(k-j+1));
%calculating values of d
end
end
%creating a 2D array to keep track of the product of divided
difference and polynomial before that difference.
s=zeros(n,n);
for i=1:n
p=1;
if i~=1
for k=1:i-1
a = [1 -x(k)]; %Here 1 is the co-effecient of the
variable x.
p = conv(a,p); %polynomials which will be multiplied
by co-efficients d.
end
end
p=p*d(i,i);
f = fliplr(p); % p has been flipped left to right to keep
the constants in the leftmost column.
for j =1:length(p)
p*d
end
end
A = fliplr(s); % flipped again to keep the higher degree of xs
co-efficients in the leftmost coloumn.
for j = 1:n
res(j) = sum(A(:,j)); %adding the co-efficients of same
column
end
disp(res);
x1 = x(1):.1:x(n); % value of x has been incremented from first
element of x to last with .1 increment.
plot(x,y,'o',x1,polyval(res,x1)); %plotting values
grid on
RESULT:
The following data was shown in command window after running the code:
And the graph for the following data passes through given points is shown below:
DISCUSSION:
MATLAB has successfully been used in this assignment to interpolate data by
using Newton polynomial .