function slide=histodem
%HISTODEM illustrate the use of spline interpolation to smooth a histogram
%
% This is a slideshow file for use with playshow.m and makeshow.m
% To see it run, type 'playshow histodem', 

% cb 9oct99
%   Copyright 1987-2000 by C. de Boor and The MathWorks, Inc. 
%   $Revision: 1.2 $  $Date: 2000/04/25 14:14:01 $

if nargout<1
  playshow histodem
else
  %========== Slide 1 ==========

  slide(1).code={
   'y = randn(1,5001); hist(y); [heights,centers] = hist(y);';
   '' };
  slide(1).text={
   '';
   'Here is a histogram.';
   '';
   ['We would like to derive from it a smoother approximation to the',...
   ' underlying distribution.'];
   ['We do this by constructing a spline function  f  whose average',...
   ' value over each bar interval equals the height of that bar.']};

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

  slide(2).code={
   'hist(y), hold on,  set(gca,''Xticklabel'',[])';
   'n = length(centers); w = centers(2)-centers(1);';
   't = linspace(centers(1)-w/2,centers(end)+w/2,n+1);';
   'p = fix(n/2);  fill(t([p p p+1 p+1]),[0 heights([p p]),0],''w'')';
   'plot(centers([p p]),[0 heights(p)],''r:''), h = text(centers(p)-.2,heights(p)/2,''   h'');';
   'dep = -70;tl = text(t(p),dep,''l''); tr = text(t(p+1),dep,''r''); hold off';
   '' };
  slide(2).text={
   ['If   h  is the height of one of these bars,  and its left and right',...
   ' edge are at  l  and  r , then we want our spline  f   to satisfy'];
   '';
   '                             integral { f(x) : l < x < r }/(r - l)  =  h ,';
   '';
   'or, with  F  the indefinite integral of   f , i.e.,  DF = f , ';
   '';
   '                                               F(r) - F(l)  =  h*(r - l) .';
   ''};

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

  slide(3).code={
   'set(h,''String'',''h(i)'')';
   'set(tl,''String'',''t(i)'')';
   'set(tr,''String'',''t(i+1)'')';
   'dt = diff(t);';
   'F = spline(t,[0,cumsum([0,heights.*dt]),0]);';
   'hold on, fnplt(fnder(F), ''r'',2), hold off';
   '' };
  slide(3).text={
   ['So, with  t(i)  the left edge of the i-th bar,  dt(i)  its width,',...
   ' and  h(i)  its height, we want '];
   '                F(t(i+1)) - F(t(i))  =  h(i) * dt(i) ,    i=1:n ,';
   'or, setting arbitrarily  F(t(1)) = 0 ,';
   '               F(t(i))  =  sum {h(j)*dt(j) : j=1:i-1},  i=1:n+1.';
   'Add to this the two end conditions  DF(t(1)) = 0 = DF(t(n+1)) , and we';
   ['have all the data we need to get  F  as a complete cubic spline',...
   ' interpolant, and its derivative,  f = DF , is what we want and',...
   ' plot, all in one statement:'];
   '>> hold on, fnplt(fnder(spline(t,[0,cumsum([0,h.*dt]),0])),''r'',2),hold off';
   ''};
  %========== Slide 4 ==========

  slide(4).code={};
  slide(4).text={
   'Here is an explanation of the one-liner used:';
   '>>  fnplt(fnder(spline(t,[0,cumsum([0,h.*dt]),0])),''r'',2)';
   '';
   ' Fvals = cumsum([0,h.*dt]);      % provides the values of  F  at  t ';
  ' F = spline( t , [0, Fvals, 0]); % constructs the cubic spline interpolant,';
   '                                   % with zero endslopes, to these values';
   ' DF = fnder(spline);             % computes its first derivative';
   ' fnplt(DF, ''r'', 2)             % plots  DF , in red with linewidth 2';
   ''};
end
