function slide=spapidem
%SPAPIDEM This is a slideshow file for use with playshow.m and makeshow.m
%   To see it run, type 'playshow spapidem',

%   Copyright 1987-2000 by C. de Boor and The MathWorks, Inc.
%   $Revision: 5.15 $

if nargout<1,
  playshow spapidem
else
  %========== Slide 1 ==========

  slide(1).code={
   'hold off',
   '[xx,yy]=titanium;',
   '% The plot of the data shows a rather sharp peak.',
   'frame=[-1 1 -.1 .1]+[min(xx),max(xx),min(yy),max(yy)];',
   'plot(xx,yy,''x'');',
   'axis(frame);' };
  slide(1).text={
   ' Press the "Start" button to see a demonstration of a spline',
   ' curve being used to fit real world data.'};

  %========== Slide 2 ==========

  slide(2).code={
   'hold off',
   'plot(xx,yy,''x'');',
   'axis(frame);' };
  slide(2).text={
   ' Here are some sample data which record a property of',
   ' titanium measured as a function of temperature. We''ll use it to',
   ' illustrate the use of spline interpolation.',
   ' >> [xx, yy] = titanium;',
   ' >> frame = [-1 1 -.1 .1]+[min(xx), max(xx), min(yy), max(yy)];',
   ' >> plot(xx, yy, ''x'');',
   ' >> axis(frame);',
   ' >> drawnow;'};

  %========== Slide 3 ==========

  slide(3).code={
   'plot(xx,yy,''x'');',
   'axis(frame);',
   'hold on',
   'pick=[1 5 11 21 27 29 31 33 35 40 45 49];',
   'tau=xx(pick);y=yy(pick);',
   'plot(tau,y,''ro'');' };
  slide(3).text={
   ' We will pick a few data points from these somewhat',
   ' rough data, since we want to interpolate it. Here is a',
   ' picture of the data, with the selected data points marked.',
   '',
   ' >> pick = [1 5 11 21 27 29 31 33 35 40 45 49];',
   ' >> tau = xx(pick);',
   ' >> y = yy(pick);',
   ' >> hold on, plot(tau, y, ''ro'');'};

  %========== Slide 4 ==========

  slide(4).code={
   'plot(xx,yy,''x'',tau,y,''ro'');',
   'n=length(tau);dl=tau(2)-tau(1);dr=tau(n)-tau(n-1);',
   't=[tau(1)-dl*[2 1] tau tau(n)+dr*[1 2]];        % construct the knot sequence',
   'hold on, axis(frame+[-2*dl 2*dr 0 0])',
   'plot(t,frame(3)*ones(size(t)),''kx''), hold off',
   'sp=spapi(t,tau,y);                      % This constructs the spline.' };
  slide(4).text={
   ' Since a spline of order k with n+k knots has n degrees of',
   ' freedom, and we have 12 data points, a fit with a fourth order',
   ' spline requires 12+4 = 16 knots. Moreover, this knot sequence',
   ' t must be such that the i-th data site lies in the support',
   ' of the i-th B-spline. We achieve this by using the data',
   ' sites as knots, but adding two knots at either end.',
   ' >> n = length(tau);  dl = tau(2)-tau(1);  dr = tau(n)-tau(n-1);',
   ' >> t = [tau(1)-dl*[2 1] tau tau(n)+dr*[1 2]];',
   ' We use this knot sequence to construct an interpolating cubic spline:',
   ' >> sp = spapi(t, tau, y);'};

  %========== Slide 5 ==========

  slide(5).code={
   'plot(xx,yy,''x'',tau,y,''ro''), axis(frame), hold on;',
   '% Now, for the plot:',
   'pp=fn2fm(sp,''pp'');                        % converts to pp form',
   '% We don''t care about the part of the curve outside the data interval,',
   '% so we cut the  pp-rep. down to that interval....',
   'pc=fnbrk(pp,[tau(1) tau(n)]);',
   '%       ....  and only then plot it,',
   'values=fnplt(pc);plot(values(1,:),values(2,:));',
   'drawnow',
   'hold off' };
  slide(5).text={
   ' Now, for the plot. We first convert the spline from B-form to',
   ' ppform. Since we do not care about the part of the curve',
   ' outside the data, we cut the ppform down to that interval ...',
   ' >> pp = fn2fm(sp,''pp'');',
   ' >> pc = fnbrk(pp, [tau(1) tau(n)]);',
   ' ... then obtain plotting values of "pc" for that interval and plot them',
   ' >> values = fnplt(pc); plot(values(1,:),values(2,:));'};

  %========== Slide 6 ==========

  slide(6).code={
   '  xxx = linspace(tau(1),tau(5),41);',
   '  plot(xxx, fnval(pc, xxx), tau, y, ''ro'');',
   '  axis([tau(1) tau(5) 0.6 1.2]);' };
  slide(6).text={
   ' A closer look at the left part of the spline fit shows some',
   ' undulations.',
   '',
   ' >> xxx = linspace(tau(1),tau(5),41);',
   ' >> plot(xxx, fnval(pc, xxx), tau, y, ''ro'');',
   ' >> axis([tau(1) tau(5) 0.6 1.2]);'};

  %========== Slide 7 ==========

  slide(7).code={
   '  plot(xxx, fnval(pc, xxx), tau, y, ''ro'');',
   '  axis([tau(1) tau(5) 0.6 1.2]);',
   '  fnplt(pp);' };
  slide(7).text={
   ' The unreasonable bump in the first interval stems from the fact',
   ' that our spline goes smoothly to zero at its first knot.',
   '',
   ' Here is a picture of the entire spline.',
   '',
   ' >> fnplt(pp);'};

  %========== Slide 8 ==========

  slide(8).code={
   'fnplt(pp);',
   'hold on',
   'plot(tau, y, ''ro'');',
   'hold off' };
  slide(8).text={
   ' Here are again the data points as well.',
   '',
   ' >> hold on, plot(tau, y, ''ro''), hold off;'};

  %========== Slide 9 ==========

  slide(9).code={
   'tt=[tau(1)-[4 3 2 1]*dl tau tau(n)+[1 2 3 4]*dr];',
   'xx=[tau(1)-[2 1]*dl tau tau(n)+[1 2]*dr];',
   'yy=[y(1)-[2 1]*(y(2)-y(1)) y y(n)+[1 2]*(y(n)-y(n-1))];',
   'pp2=fn2fm(spapi(tt,xx,yy),''pp''); mr=[1 2 n+3 n+4];',
   'plot(values(1,:),fnval(pp2,values(1,:)),''-'',tau,y,''ro'',xx(mr),yy(mr),''ko'');',
   'axis(frame+[-2*dl 2*dr 0 0]);' };
  slide(9).text={
   ' Here is a simple way to enforce a more reasonable boundary',
   ' behavior. We add two more data points outside the given data',
   ' interval and choose as our data there the values of the straight',
   ' line through the first two data points.',
   ' >> tt = [tau(1)-[4 3 2 1]*dl tau tau(n)+[1 2 3 4]*dr];',
   ' >> xx = [tau(1)-[2 1]*dl tau tau(n)+[1 2]*dr];',
   ' >> yy = [y(1)-[2 1]*(y(2)-y(1)) y y(n)+[1 2]*(y(n)-y(n-1))];',
   ' >> pp2 = fn2fm(spapi(tt, xx, yy),''p'');',
   ' >> mr = [1 2 n+3 n+4];',
   ' >> plot(values(1,:), fnval(pp2, values(1,:)), ''-'', tau, y, ''ro'',xx(mr),yy(mr),''ok'');'};

  %========== Slide 10 ==========

  slide(10).code={
   'hold on',
   'plot(values(1,:),fnval(pp2,values(1,:)),''-'',tau,y,''ro'',xx(mr),yy(mr),''ko'');',
   'axis(frame+[-2*dl 2*dr 0 0]);',
   'fnplt(pp,''k'')',
   'hold off' };
  slide(10).text={
   ' Here is also the original spline fit, shown in black, to show the',
   ' reduction of the undulation in the first and last interval.',
   '',
   ' >> hold on, fnplt(pp,''k''), hold off'};

  %========== Slide 11 ==========

  slide(11).code={
   'plot(xxx,fnval(pp2,xxx),''b'',tau,y,''ro'',xxx,fnval(pp,xxx),''k'');',
   'axis([tau(1) tau(5) .6 2.2]);',
   'hold off' };
  slide(11).text={
   ' Finally, here is a closer look at the first four data intervals',
   ' which shows more clearly the reduction of the undulation near',
   ' the left end.',
   ' >> plot(xxx, fnval(pp2,xxx), ''b'', tau, y, ''ro'');',
   ' >> hold on',
   ' >> plot( xxx, fnval(pp,xxx), ''k'');',
   ' >> axis([tau(1) tau(5) .6 2.2]), hold off;'};
end
