function newtondd(x,y) % Newton divided difference disp(\' Newton divided differ
ID: 3833034 • Letter: F
Question
function newtondd(x,y)
% Newton divided difference
disp(' Newton divided difference')
disp('______________________________________________________________________')
disp(' x y f[,] f[,,] f[,,,] ... ')
disp('______________________________________________________________________')
n=length(x);
for k=1:n-1
% Compute first divided difference
d(k,1)=(y(k+1)-y(k))/(x(k+1)-x(k));
end;
for i=2:n-1
for k=1:n-i
% Compute ith divided difference
d(k,i)=(d(k+1,i-1)-d(k,i-1))/(x(k+i)-x(k));
end;
end;
% print results
if (rem(n,2)==0)
p=n/2;
m=n/2;
else
p=fix(n/2);
m=fix(n/2)+1;
end;
for i=1:p
fprintf('%8.2f %8.2f',x(i),y(i));
for k=1:i-1
fprintf(' %8.5f',d(i-k,2*k));
end;
fprintf(' ')
for k=1:i
fprintf(' %8.5f',d(i-k+1,2*k-1));
end;
fprintf(' ')
end;
j=p;
for i=m:-1:1
j=j+1;
fprintf('%8.2f %8.2f',x(j),y(j));
for k=1:i-1
fprintf(' %8.5f',d(j-k,2*k));
end;
fprintf(' ')
for k=1:i-1
fprintf(' %8.5f',d(j-k+1,2*k-1));
end;
fprintf(' ')
end;
Explanation / Answer
I am providing you with the generic solution to use newtondd.m function in matlab i.e. as follows:
function [coefficients]= newtonDD(x, F)
fLength=size(F);
fLength=fLength(2);
chart=zeros(fLength);
chart(:,1)=F;
coefficients=zeros(1, 4);
coefficients(1,1)=F(1,1);
for i=2:fLength,
for j=2:i,
chart(i,j)=(chart(i,j-1)-chart(i-1, j-1))/(x(i)-x(i-(j-1)));
if i==j,
coefficients(1,i) = chart(i,j);
end
end
end
disp(chart);
end
Inputs:
x=array of x values {x_0,...,x_n}
F=array of f(x_n) values {f(x_0),...,f(x_n)}
Outputs:
coefficients=array of coefficients for the terms of the
interpolating polynomial
*Note*
Before the program returns the coefficents of the resulting
polynmial, it prints out the entire divided difference chart
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.