% CLASS27OCT  Applied equally-spaced polynomial interpolation,
%   equally-spaced piecewise-linear interpolation, and
%   Chebyshev-spaced  polynomial interpolation to approximate
%   a very irregular function.  That function is computed by
%   another script; see
%     http://www.dms.uaf.edu/~bueler/nodiffM.htm
%   Download it from:
%     http://www.dms.uaf.edu/~bueler/jagged.m
%   THIS SCRIPT ONLY RUNS IF jagged.m IS AVAILABLE TO MATLAB/OCTAVE.

% draw basic plot of y=f(x); note that  f(x) = jagged(x,10,4,1.2)
x = 0:2.e-5:1 ; y = jagged(x,10,4,1.2) ; plot(x,y) % blue

% EQUALLY-SPACED POLYNOMIAL INTERPOLATION in red
% plot the interpolation points
n = 20;
xj = 0:1/n:1; % n+1 = 21 points
yj = jagged(xj,10,4,1.2);   % y_j = f(x_j)
hold on, plot(xj,yj,'or','markersize',12)   % red
P = polyfit(xj,yj,20);  % compute the polynomial
plot(x,polyval(P,x),'r')  % plot the polynomial; red also

% EQUALLY-SPACED PIECEWISE-LINEAR INTERPOLATION in black
% because piecewise-linear is the default plotting style:
plot(xj,yj,'k') % black

% CHEBYSHEV-SPACED POLYNOMIAL INTERPOLATION in green
xc = 0.5*(cos((pi/n)*(0:n)) + 1);  % x-values of n+1 equally-spaced
                                   % points *on the semicircle* 
plot(xc,jagged(xc,10,4,1.2),'go','markersize',12)  % cyan
PC = polyfit(xc,jagged(xc,10,4,1.2),n);
plot(x,polyval(PC,x),'g')    % cyan also
axis([0 1 -0.5 1.5]), hold off, xlabel x

