Matlab Lecture Examples from Week 4
This script displays a spline and the local cubics that make it up.
close all
x = [ 1 3 4 7 9 10];
y = [ -1 1 -2 2 1 1];
z = linspace(1,10);
% The next two lines are synonomous with
% sVals = Spline(x,y,z)
S = Spline(x,y);
sVals = ppval(S,z);
plot(z,sVals,x(2:5),y(2:5),'*',x([1 6]),y([1 6]),'o',[0 11],[0 0])
axis([0 11 -4 6])
pause
[x,rho,L,k] = unmkpp(S)
for i=1:L
% Evaluate the polynomial
% rho(i,1)(v-x(i))^2 + rho(i,2)(v-x(i))^2 + rho(v,3)(v-x(i)) + rho(i,4)
% at v = z(1),...,z(100) and assemble the results in the vector qiVals
qiVals = polyval(rho(i,:),z-x(i));
% Plot the interpolant and highlight the knots.
plot(z,sVals,x(2:5),y(2:5),'*',x([1 6]),y([1 6]),'o',[0 11],[0 0])
axis([0 11 -4 6])
% Superimpose a plot of the ith local cubic.
hold on
qiVals = polyval(rho(i,:),z-x(i));
plot(z,qiVals,'r--',[x(i) x(i+1)],[0 0],'+k',[x(i) x(i+1)],[0 0],'m')
hold off
title(sprintf(' i = %1d',i))
pause
end