I wrote a cubic spline package in Mathematica a long time ago. Here is my translation of this package in Matlab. Note. I did not consider cubic splines after about 7 years, so I base this on my own documentation. You must check everything that I say.
The main problem is to give us n
data points (x(1), y(1)) , ... , (x(n), y(n))
, and we want to calculate piecewise cubic interpolation. Interpolation is defined as
S(x) = { Sk(x) when x(k) <= x <= x(k+1) { 0 otherwise
Here Sk (x) is a cubic polynomial of the form
Sk(x) = sk0 + sk1*(xx(k)) + sk2*(xx(k))^2 + sk3*(xx(k))^3
Spline Properties:
- The spline passes through the data point
Sk(x(k)) = y(k)
- The spline is continuous at the end points and, therefore, continuous everywhere in the interpolation interval
Sk(x(k+1)) = Sk+1(x(k+1))
- The spline has a continuous first derivative
Sk'(x(k+1)) = Sk+1'(x(k+1))
- The spline has a continuous second derivative
Sk''(x(k+1)) = Sk+1''(x(k+1))
To construct a cubic spline from a set of data points, we need to solve for the coefficients sk0
, sk1
, sk2
and sk3
for each of the cubic polynomials n-1
. This is only 4*(n-1) = 4*n - 4
unknowns. Property 1 provides the restrictions n
, and properties 2,3,4 each provide additional restrictions n-2
. Thus, we have n + 3*(n-2) = 4*n - 6
restrictions and 4*n - 4
unknowns. This leaves two degrees of freedom. We fix these degrees of freedom, setting the second derivative equal to zero at the initial and final nodes.
Let m(k) = Sk''(x(k))
, h(k) = x(k+1) - x(k)
and d(k) = (y(k+1) - y(k))/h(k)
. The following is a three-term recurrence relation
h(k-1)*m(k-1) + 2*(h(k-1) + h(k))*m(k) + h(k)*m(k+1) = 6*(d(k) - d(k-1))
m (k) are unknown for which we want to solve. The parameters h(k)
and d(k)
determined by the input data. This three-term recurrence relation defines a three-diagonal linear system. After determining m(k)
coefficients of Sk
determined by the expression
sk0 = y(k) sk1 = d(k) - h(k)*(2*m(k) + m(k-1))/6 sk2 = m(k)/2 sk3 = m(k+1) - m(k)/(6*h(k))
Well, this is all the math you need to know in order to fully define the cubic spline calculation algorithm. Here it is in Matlab:
function [s0,s1,s2,s3]=cubic_spline(x,y) if any(size(x) ~= size(y)) || size(x,2) ~= 1 error('inputs x and y must be column vectors of equal length'); end n = length(x) h = x(2:n) - x(1:n-1); d = (y(2:n) - y(1:n-1))./h; lower = h(1:end-1); main = 2*(h(1:end-1) + h(2:end)); upper = h(2:end); T = spdiags([lower main upper], [-1 0 1], n-2, n-2); rhs = 6*(d(2:end)-d(1:end-1)); m = T\rhs; % Use natural boundary conditions where second derivative % is zero at the endpoints m = [ 0; m; 0]; s0 = y; s1 = d - h.*(2*m(1:end-1) + m(2:end))/6; s2 = m/2; s3 =(m(2:end)-m(1:end-1))./(6*h);
Here is some code to build a cubic spline:
function plot_cubic_spline(x,s0,s1,s2,s3) n = length(x); inner_points = 20; for i=1:n-1 xx = linspace(x(i),x(i+1),inner_points); xi = repmat(x(i),1,inner_points); yy = s0(i) + s1(i)*(xx-xi) + ... s2(i)*(xx-xi).^2 + s3(i)*(xx - xi).^3; plot(xx,yy,'b') plot(x(i),0,'r'); end
Here is a function that builds a cubic spline and enters the well-known Runge function:
function cubic_driver(num_points) runge = @(x) 1./(1+ 25*x.^2); x = linspace(-1,1,num_points); y = runge(x); [s0,s1,s2,s3] = cubic_spline(x',y'); plot_points = 1000; xx = linspace(-1,1,plot_points); yy = runge(xx); plot(xx,yy,'g'); hold on; plot_cubic_spline(x,s0,s1,s2,s3);
You can see it in action by doing the following at the Matlab prompt
>> cubic_driver(5) >> clf >> cubic_driver(10) >> clf >> cubic_driver(20)
By the time you have twenty nodes, your interpolator is visually indistinguishable from the Runge function.
Some comments on Matlab code: I do not use any for or while loops. I can vectorize all operations. I quickly form a sparse tridiagonal matrix with spdiags
. I solve it with the backslash operator. I count on Tim Davis UMFPACK to deal with decomposition, and solve it back and forth.
Hope this helps. The code is available as gistub https://gist.github.com/1269709