MATLAB: Can somebody explain me this code for interpolation with cubic spline

cubicspline

Can somebody explain me some things in this program. I need some help .
function [S C]=Spline3(u,x,y,dya,dyb)
% vectors x and y contain n+1 points and the corresponding function values
% vector u contains all discrete samples of the continuous argument of f(x)
% dya and dyb are the derivatives f'(x_0) and f'(x_n), respectively
n=length(x); % number of interpolating points
k=length(u); % number of discrete sample points
C=zeros(n,k); % the n-1 cubic interpolating polynomials
A=2*eye(n); % coefficient matrix on left-hand side
A(1,2)=1;
A(n,n-1)=1;
d=zeros(n,1); % vector on right-hand side
d(1)=((y(2)-y(1))/(x(2)-x(1))-dya)/h0; % first element of d
for i=2:n-1
h0=x(i)-x(i-1);
h1=x(i+1)-x(i);
h2=x(i+1)-x(i-1);
A(i,i-1)=h0/h2;
A(i,i+1)=h1/h2;
d(i)=((y(i+1)-y(i))/h1-(y(i)-y(i-1))/h0)/h2; % 2nd divided difference
end
d(n)=(dyb-(y(n)-y(n-1))/h1)/h1; % last element of d
M=6*inv(A)*d; % solving linear equation system for M's
for i=2:n
h=x(i)-x(i-1);
x0=u-x(i-1);
x1=x(i)-u;
C(i-1,:)=(x1.^3*M(i-1)+x0.^3*M(i))/6/h... % the ith cubic polynomial
-(M(i-1)*x1+M(i)*x0)*h/6+(y(i-1)*x1+y(i)*x0)/h;
idx=find(u>x(i-1) & u<=x(i)); % indices between x(i-1) and x(i)
S(idx)=C(i-1,idx); % constructing spline by cubic polynomials
end
end

Best Answer

In theory, I probably could, with some amount of effort. Reading moderately poorly written code that lacks useful comments can be quite difficult, even for someone who has been writing splines codes for 40 years. (And, sorry, but many of those comments are not really that useful.) I suppose I could tell the story of the spline code I received that had only one comment in 300 lines of Fortran. The comment was "build C matrix here". With no indication what the C matrix did.
If you really want to understand a simple interpolating spline code, you can surely find it, with explanations in many books, or online. Numerical Recipes, for example, will probably have a basic interpolating spline code. (Said without looking.) But, since MATLAB already provides basic splines code, using this mess is hardly worth it. In the end, a cubic spline is a cubic spline.
So, sorry, but even if I had the energy to write many pages of stuff, deriving the formulas in a spline, then showing how those formulas correspond to the code you see here, it would be a singular waste of time. Good luck, but I'd suggest you start from the other end. Pick up a copy of de Boor's absolutely classic text, "A Practical Guide to Splines". The read will be well worth your time. And you canget quite a lot out of even the first few chapters.
Or, if you are feeling too frugal to do that, just do some reading on Wikipedia. For example, here:
That page gives you all I would have done anyway, with just the exception I would have had to spend several hours explaining how those formulas relate to the equations in the code you have, and then more time answering followup questions.
As I said, you already have splines in MATLAB, and ones that are better written than that mess you are asking for help about.
Related Question