function interp ( )

clf
x = linspace ( 0, 10, 101 );
y = f ( x );
plot ( x, y, 'b-', 'LineWidth', 4.0 );
axis ( [ 0, 10, 0, 45 ] )
grid on
title ( 'A function to be interpolated', 'fontsize', 20 )
hold off
print ( '-dpng', 'interp_f.png' );
pause
%
%  Constant interpolant.
%
clf
hold on
xc = [ x(51) ];
yc = f ( xc );
ycc = zeros ( size ( x ) );
ycc(1,1:101) = yc;
plot ( x, y, 'b-', 'LineWidth', 4.0 );
plot ( xc, yc, 'k.', 'MarkerSize', 40.0 )
plot ( x, ycc, 'r-', 'LineWidth', 4.0 )
axis ( [ 0, 10, 0, 45 ] )
grid on
title ( 'Interpolation by a constant', 'fontsize', 20 )
print ( '-dpng', 'interp_c1.png' );
pause
%
%  Suggest integral of f versus integral of interpolant
%
ymin = min ( y, ycc );
ymax = max ( y, ycc );
fill ( [ x, 10, 0], [ymax,0,0], 'r' )
fill ( [x,10,0], [ymin,0,0], 'b' );
plot ( xc, yc, 'k.', 'MarkerSize', 40.0 )
title ( 'Integral of f(x) versus integral of constant', 'fontsize', 20 )
hold off
print ( '-dpng', 'interp_c2.png' );
pause
clf
%
%  Linear interpolant.
%
clf
hold on
xl = [ x(26), x(76) ];
yl = f ( xl );
slope = ( yl(2) - yl(1) ) / ( xl(2) - xl(1) );
yll(1,1:101) = yl(1) + slope * ( x - xl(1) );
plot ( x, y, 'b-', 'LineWidth', 4.0 );
plot ( xl, yl, 'k.', 'MarkerSize', 40.0 )
plot ( x, yll, 'r-', 'LineWidth', 4.0 )
axis ( [ 0, 10, 0, 45 ] )
grid on
title ( 'Interpolation by a linear', 'fontsize', 20 )
print ( '-dpng', 'interp_l1.png' );
pause
%
%  Suggest integral of f versus integral of interpolant
%
ymin = min ( y, yll );
ymax = max ( y, yll );
fill ( [ x, 10, 0], [ymax,0,0], 'r' )
fill ( [x,10,0], [ymin,0,0], 'b' );
plot ( xl, yl, 'k.', 'MarkerSize', 40.0 )
title ( 'Integral of f(x) versus integral of linear', 'fontsize', 20 )
print ( '-dpng', 'interp_l2.png' );
hold off
pause
clf
%
%  Quadratic interpolant
%
clf
hold on
yq = 0.49 * x .* x - 3.1780 * x + 16.7650;
x3 = [ x(17), x(51), x(84) ];
y3 = [ yq(17), yq(51), yq(84) ];
plot ( x, y, 'b-', 'LineWidth', 4.0 );
plot ( x, yq, 'r-', 'Linewidth', 3.0 );
plot ( x3, y3, 'k.', 'MarkerSize', 40.0 )
axis ( [ 0, 10, 0, 45 ] )
grid on
title ( 'Interpolation by a quadratic', 'fontsize', 20 )
print ( '-dpng', 'interp_q1.png' );
pause
%
%  Suggest integral of f versus integral of interpolant
%
ymin = min ( y, yq );
ymax = max ( y, yq );
fill ( [ x, 10, 0], [ymax,0,0], 'r' )
fill ( [x,10,0], [ymin,0,0], 'b' );
plot ( x3, y3, 'k.', 'MarkerSize', 40.0 )
title ( 'Integral of f(x) versus integral of quadratic', 'fontsize', 20 )
print ( '-dpng', 'interp_q2.png' );
hold off
pause

  return
end
function y = f ( x )
  y = 0.5 * ( x - 3.5 ).^2 + 12 + 0.1 * ( x - 2 ) .* ( x - 5 ) .* ( x - 8 );
  return
end
