% computer science matlab has available the splinetool command for
% experimenting with spline approximation:

>> splinetool

% I clicked on `provide own data', then used the default, to end up
% interpolating to cos on its basic interval  [0 .. 2*pi].
% I showed the effect of various end conditions, 
% derided the `natural' end condition (except when the function actually has
% a zero second derivative),
% showed that matlab's not-a-knot condition is not as good near the end as is
% complete or clamped end condition, and, in this case, the
% periodic end condition was also very good.

% matlab's spline command gives the not-a-knot cubic spline interpolant,
% but can also be made to give the complete cubic spline interpolant, by 
% supplying also the end slopes, as the first and last entry, respectively,
% of the vector y, thus making that vector have two more entries than x has.

% matlab's spline command can even work with vector-valued functions. 
% E.g.,
>> y =
     0     1     0    -1     0     1     0
     1     0     1     0    -1     0     1];
>> x = 0:4;
     
>> sp = spline(x,y)
sp = 
      form: 'pp'
    breaks: [0 1 2 3 4]
     coefs: [8x4 double]
    pieces: 4
     order: 4
       dim: 2
>> val = ppval(sp,linspace(0,4,100));
>> plot(val(1,:),val(2,:))

>> axis equal % to make sure that both axes are scaled the same.

% this looks like a good approximation to a circle, -- except at the point
% (1,0). The reason is that I took the wrong slope there:
% As the independent variable runs from 0 to 4, we run once around
approximately the unit circle, which has length 2*pi. Assuming constant speed,
this means that our speed should be 2*pi/4 = pi/2, rather than the 1 I used.
>> y(2,[1 end]) = pi/2
y =
         0    1.0000         0   -1.0000         0    1.0000         0
    1.5708         0    1.0000         0   -1.0000         0    1.5708
>> sp = spline(x,y)
sp = 
      form: 'pp'
    breaks: [0 1 2 3 4]
     coefs: [8x4 double]
    pieces: 4
     order: 4
       dim: 2
>> val = ppval(sp,linspace(0,4,100));
>> plot(val(1,:),val(2,:))
>> axis equal

% Not so bad, particularly considering that we are doing this with just four
% cubic pieces!
diary off
