Home > K25104 > Interpolation > Newtoneval.m

Newtoneval

PURPOSE ^

Evaluates the interpolating polynomial for the given data

SYNOPSIS ^

function [y] = Newtoneval(nodes, values, x )

DESCRIPTION ^

 Evaluates the interpolating polynomial for the given data
 from the Newton form.
 Input arguments:
   nodes, the interpolation nodes
   values, function values at the nodes
   x, values at which the interpolating polynomial should be evaluated
 Output arguments:
   y, value of interpolating polynomial at x

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [y] = Newtoneval(nodes, values, x )
0002 % Evaluates the interpolating polynomial for the given data
0003 % from the Newton form.
0004 % Input arguments:
0005 %   nodes, the interpolation nodes
0006 %   values, function values at the nodes
0007 %   x, values at which the interpolating polynomial should be evaluated
0008 % Output arguments:
0009 %   y, value of interpolating polynomial at x
0010 
0011 [n,m]=size(nodes);  % finding the size of nodes
0012 [k,l]=size(values); % finding the size of values
0013 [p,q]= size(x);     % finding the size of x
0014 if m~=1 || l~=1 || q~=1;
0015     error('input need to be column vectors');
0016 elseif n~=k
0017     error('input dimensions do not agree');
0018 end
0019 
0020 d=divdifftable(nodes,values);   % this subroutine calculates the divided
0021                                 % difference table
0022 d=diag(d);  % the required divided differences lie along the diagonal
0023 
0024 y = d(1) * ones(p,1); % first term of the sum in the Newton form
0025 temp = x - nodes(1) * ones(p,1); % temp holds the product
0026 % Note that y and temp are vectors
0027 
0028 % add the terms of the sum in the Newton form one after the other
0029 for i=2:n
0030     y = y + d(i) * temp;
0031     temp = temp .* (x - nodes(i)); % Note: .* is element-wise multiplication
0032 end
0033 end
0034

Generated on Mon 18-Jan-2016 10:25:49 by m2html © 2005