Home > K25104 > Interpolation > cubic_spline.m

cubic_spline

PURPOSE ^

Fits a cubic spline interpolant to the function at given nodes, and evaluates it at the specified points in x.

SYNOPSIS ^

function [ y ] = cubic_spline( nodes,values,a,b,x )

DESCRIPTION ^

 Fits a cubic spline interpolant to the function at given nodes, and evaluates it at the specified points in x.
 Input arguments:
   nodes, the interpolation nodes
   values, function values at the nodes
   a,b, values of the the derivatives at the endpoints
   x, points at which the interpolating spline should be evaluated
 Output arguments:
   y, value of spline at x

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [ y ] = cubic_spline( nodes,values,a,b,x )
0002 % Fits a cubic spline interpolant to the function at given nodes, and evaluates it at the specified points in x.
0003 % Input arguments:
0004 %   nodes, the interpolation nodes
0005 %   values, function values at the nodes
0006 %   a,b, values of the the derivatives at the endpoints
0007 %   x, points at which the interpolating spline should be evaluated
0008 % Output arguments:
0009 %   y, value of spline at x
0010 
0011 [n,m]=size(nodes);   % finding the size of nodes
0012 [p,q]=size(values);  % finding the size of values
0013 if m~=1 || q~=1
0014     error('inputs must be column vectors');
0015 elseif n~=p
0016     error('input vectors must be the same length');
0017 elseif max(x)>max(nodes) || min(x)<min(nodes)
0018     error('the interpolant will not be defined at all of the points in x');
0019 end
0020 m=length(x);    % finding the length of x
0021 
0022 
0023 d=spline_deriv(nodes,values,a,b); % this subroutine calculates the
0024                                   % derivatives at the internal nodes.
0025 derivs=[a;d;b];                   % create vector which holds all the
0026                                   % derivatives (including endpoints).
0027 
0028 coeffs=spline_pieces(nodes,values,derivs); % this subroutine calculates the
0029                                   % coefficients of the cubic pieces on
0030                                   % each subinterval
0031                                    
0032 y=zeros(m,1);   % intialise vector y
0033 k=1;      
0034 for j=1:m;
0035     point=x(j);
0036     if point>nodes(k+1) % then the point lies in the next subinterval
0037         k=k+1;  % increment k
0038     end 
0039     temp=point-nodes(k);
0040     y(j)=coeffs(k,1)+coeffs(k,2)*temp+coeffs(k,3)*temp^2+coeffs(k,4)*temp^3;
0041 end
0042 
0043 end
0044

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