Azzera filtri
Azzera filtri

newton forward interpolation method by using less number of for loops

2 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

KSSV
KSSV il 28 Ago 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.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by