Calculates the derivatives of the cubic spline at the interior nodes. Input arguments: nodes, column vector holding the positions of the nodes values, column vector holding function values at the nodes a,b, values of the the derivatives at the endpoints Output arguments: d, column vector holding the derivatives of the spline at the nodes
0001 function [ d ] = spline_deriv( nodes, values, a,b ) 0002 % Calculates the derivatives of the cubic spline at the interior nodes. 0003 % Input arguments: 0004 % nodes, column vector holding the positions of the nodes 0005 % values, column vector holding function values at the nodes 0006 % a,b, values of the the derivatives at the endpoints 0007 % Output arguments: 0008 % d, column vector holding the derivatives of the spline at the nodes 0009 0010 [n,m]=size(nodes); % finding the size of nodes 0011 [p,q]=size(values); % finding the size of values 0012 if m~=1 || q~=1 0013 error('inputs must be column vectors'); 0014 elseif n~=p 0015 error('input vectors must be the same length'); 0016 end 0017 0018 diagonal=zeros(n-2,1); % initialise vector to hold diagonal 0019 for k=1:n-2; 0020 diagonal(k)=2/(nodes(k+1)-nodes(k))+2/(nodes(k+2)-nodes(k+1)); 0021 end 0022 0023 sup_diag=zeros(n-3,1); % initialise vector to hold off diagonal 0024 for k=1:n-3 0025 sup_diag(k)=1/(nodes(k+1)-nodes(k)); 0026 end 0027 0028 S=diag(diagonal)+diag(sup_diag,1)+diag(sup_diag,-1); % form S 0029 0030 V=zeros(n-2,1); % initialise vector for right hand side 0031 for k=1:n-2; 0032 V(k)=3*(values(k+1)-values(k))/((nodes(k+1)-nodes(k))^2)+... 0033 3*(values(k+2)-values(k+1))/((nodes(k+2)-nodes(k+1))^2); 0034 end 0035 V(1)=V(1)-a/(nodes(2)-nodes(1)); 0036 V(n-2)=V(n-2)-b/(nodes(n-1)-nodes(n-2)); 0037 0038 d=linsolve(S,V); % solve linear system 0039 0040 end 0041