5 Newtons Method Matlab 6 points a Create a function newtonc
Solution
function [f, a, d] = newtninter(x, y, p)
% Newton interpolation
%
% [f a d] = newtoninter(x, y, p)
%
% Input arguments ([]s are optional):
% x (vector) of size 1xN which contains the interpolation nodes.
% y (vector) of size 1xN which contains the function values at x
% p (vector) of size 1xP which contains points to be interpolated.
%
% Output arguments ([]s are optional):
% f (vector) of size 1xP. The result of interpolation respect to p.
% [a] (vector) of size 1xN which is leading coefficients genereated by
% divided difference method.
% [d] (matrix) of size NxN (triangular) which is the result of the
% divided difference method
%
% Example
% >> x=[0,1/2,1,2,3]
% >> y=[1,2,5, -1,-3 ];
% >> [f a d]= newtninter(x, y, 5)
n = length(x);
d(:,1)=y\';
for j=2:n
for i=j:n
d(i,j)= ( d(i-1,j-1)-d(i,j-1)) / (x(i-j+1)-x(i));
end
end
a = diag(d)\';
Df(1,:) = repmat(1, size(p));
c(1,:) = repmat(a(1), size(p));
for j = 2 : n
Df(j,:)=(p - x(j-1)) .* Df(j-1,:);
c(j,:) = a(j) .* Df(j,:);
end
f=sum(c);
solution
1.0000 0 0 0 0
2.0000 2.0000 0 0 0
5.0000 6.0000 4.0000 0 0
-1.0000 -6.0000 -8.0000 -6.0000 0
-3.0000 -2.0000 2.0000 4.0000 3.3333
f =
461
