newton forward interpolation method by using less number of for loops

7 views (last 30 days)
I wrote the following program for newton forward interpolation method. Can it be written with reduced code using single for loop
function [ yval ] = new_fint( xd,yd,x )
%NEWTON FORWARD Summary of this function goes here
% Detailed explanation goes here
n=length(xd);
if(length(yd)==n)
l=zeros(1,n);
diff=yd';
for j=2:n
for i=1:n-j+1
diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
end
end
l=diff(1,:);
h=xd(2)-xd(1);
u=(x-xd(1))/h;
t(1)=1;
for i=2:n
t(i)=t(i-1)*(u-(i-2))/(i-1);
end
yval=sum(l.*t);
else
error('xd and yd must be of same size');
end
end

Answers (1)

KSSV
KSSV on 28 Aug 2018
This loop can be replaced:
for j=2:n
for i=1:n-j+1
diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
end
end
with:
j=2:n
i=1:n-j+1
diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
Try tit out...and follow the others.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!