%when reading this demo file, you  should ignore all lines that begin
%with disp. copy the file into your account, and remove all such lines.

disp('We introduce in this demo spline interpolation and apply it to')
disp('the functions cosine, |x|, and the example of Runge.')
 
disp('clear all')
disp('hold off')
 clear all
 hold off

%++++++++++++++++++++
%demo with cos(x)
 x=[-5:5]';
disp('x=[-5:5]'';')
 y=cos(x);
disp('y=cos(x);')
 pp=spline(x,y);
disp('pp=spline(x,y);')
disp('spline.m produces a cubic spline interpolant to the data specified')
disp(' by (x,y). The output can only be used as an input for another')
disp('spline file.')
null=input(' ');

disp('++++++++++++++++++++++++++++++++++')
 xi=linspace(-5,5)';
disp('xi=linspace(-5,5)'';')
disp('now, we evalute the spline at xi:')
disp('yi=ppval(pp,xi);')
 yi=ppval(pp,xi);
disp('hold off')
hold off
disp('plot(x,y,''x'',xi,yi), ')
plot(x,y,'x',xi,yi), 
disp('hold on, pause')
hold on
null=input(' ');

disp('++++++++++++++++++++++++++++++++++')
plot(xi,cos(xi))
disp('plot(xi,cos(xi))')
disp('max(abs(yi-cos(xi)))')
max(abs(yi-cos(xi)))

%ans =
%
   %0.01901056356831
%
disp('hold off, pause')
 hold off
 null=input(' ');
disp('plot(xi,yi-cos(xi)), pause')
 plot(xi,yi-cos(xi))
 null=input(' ');

disp('++++++++++++++++++++++++++++++++++')
disp('++++++++++++++++++++++++++++++++++')
%++++++++++++++++
%demo with |x|
disp('Now, we''ll use |x|')
disp('x=[-5:5]'';')
 x=[-5:5]';


%we now generate a spline interpolant
%and plot its graph

disp('pp=spline(x,abs(x));')
 pp=spline(x,abs(x));

disp('xi=linspace(-5,5);')
 xi=linspace(-5,5);

disp('yi=ppval(pp,xi);')
 yi=ppval(pp,xi);

disp('plot(x,abs(x),''x'',xi,yi),')
 plot(x,abs(x),'x',xi,yi)
 null=input(' ');
disp('++++++++++++++++++++++++++++++++++')


disp('hold on, pause')
 hold on
 null=input(' ');
disp('++++++++++++++++++++++++++++++++++')

%we now generate two polynomial interpolants
disp('n=length(x);')
 n=length(x);
disp('chebx=5*cos(linspace(-pi,0,n))''')
 chebx=5*cos(linspace(-pi,0,n))'
%chebx are the 11 Chebychev points on the interval [-5,5]
null=input(' ');
disp('++++++++++++++++++++++++++++++++++')

%
%chebx =
%
  %-5.00000000000000
  %-4.75528258147577
  %-4.04508497187474
  %-2.93892626146237
  %-1.54508497187474
   %0.00000000000000
   %1.54508497187474
   %2.93892626146237
   %4.04508497187474
   %4.75528258147577
   %5.00000000000000
%

%inter.m computes the polynomial interpolant to the data (x,y), plots it
%and returns xvalues and corresponding yvalues of that polynomial of a fine
%mesh
disp('[xpol,ypol]=inter1(x,abs(x));')
 [xpol,ypol]=inter1(x,abs(x));

disp('pause')
null=input(' ');
disp('++++++++++++++++++++++++++++++++++')


disp('[xcpol,ycpol]=inter1(chebx, abs(chebx),2); pause')
 [xcpol,ycpol]=inter1(chebx, abs(chebx),2); 
 null=input(' ');

disp('serror=max(abs(yi-abs(xi)))')
disp('pcerror=max((abs(ycpol-abs(xcpol))))')
 serror=max(abs(yi-abs(xi)))

%serror =
%
   %0.16940595938952
%
 pcerror=max((abs(ycpol-abs(xcpol))))

%pcerror =
%
   %0.29536070150205

disp('hold off, pause')
hold off
null=input(' ');
disp('++++++++++++++++++++++++++++++++++')


%++++++++++++++++++++++++
%now, Runge's example
%demo with 1/(1+|x|^2)
disp('Now, we''ll use Runge''s example')
disp('clear all')
 clear all

disp('x=[-5:5]'';')
 x=[-5:5]';


%we now generate a spline interpolant
%and plot its graph
disp('xi=linspace(-5,5);')
 xi=linspace(-5,5);

disp('pp=spline(x,(1./(1+x.^2)));')
 pp=spline(x,(1./(1+x.^2)));

disp('yi=ppval(pp,xi);')
 yi=ppval(pp,xi);

disp('plot(x,(1./(1+x.^2)),''x'',xi,yi),')
 plot(x,(1./(1+x.^2)),'x',xi,yi)
 null=input(' ');

disp('hold on, pause')
 hold on
 null=input(' ');
disp('++++++++++++++++++++++++++++++++++')

%we now generate two polynomial interpolants
disp('n=length(x);')
 n=length(x);
disp('chebx=5*cos(linspace(-pi,0,n))'';')
 chebx=5*cos(linspace(-pi,0,n))';
%chebx are the 11 Chebychev points on the interval [-5,5]


%inter.m computes the polynomial interpolant to the data (x,y), plots it
%and returns xvalues and corresponding yvalues of that polynomial of a fine
%mesh
disp('[xpol,ypol]=inter1(x,(1./(1+x.^2)));')
 [xpol,ypol]=inter1(x,(1./(1+x.^2)));

disp('pause')
null=input(' ');
disp('++++++++++++++++++++++++++++++++++')


disp('[xcpol,ycpol]=inter1(chebx, (1./(1+chebx.^2)),2); pause')
 [xcpol,ycpol]=inter1(chebx, (1./(1+chebx.^2)),2); 
 null=input(' ');

disp('ser=yi-1./(1+xi.^2);')
disp('pcer=ycpol-1./(1+xcpol.^2);')
disp('serror=max(abs(ser))')
disp('pcerror=max((abs(pcer)))')

 ser=yi-1./(1+xi.^2);
 pcer=ycpol-1./(1+xcpol.^2);
 serror=max(abs(ser))

%serror =
%
   %0.16940595938952
%
 pcerror=max((abs(pcer)))

%pcerror =
%
   %0.29536070150205

hold off
disp('hold off')
