Home > K25104 > Interpolation > Lagrangeeval.m

Lagrangeeval

PURPOSE ^

Evaluates the interpolating polynomial for the given data

SYNOPSIS ^

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

DESCRIPTION ^

 Evaluates the interpolating polynomial for the given data 
 from the Lagrange 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 ] = Lagrangeeval( nodes,values,x )
0002 % Evaluates the interpolating polynomial for the given data
0003 % from the Lagrange 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 y=zeros(p,1);   % initialise y
0021 for k=1:n;
0022     prod=ones(p,1); % initalise vector to hold product
0023     for l=1:n
0024         if l==k;    % do nothing
0025         else        % update product
0026             prod=prod.*(x-nodes(l)*ones(p,1))/(nodes(k)-nodes(l));
0027         end
0028     end
0029     y=y+values(k)*prod;   % update sum
0030 end
0031 
0032 end
0033

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