%more manipulations with matrices
%suppose that we want to duplicate a column vector
disp('x=[2;6;7]')
x=[2;6;7]
pause

%we generate a matrix X 3x5, all of its columns equal the vector x:
disp('X=repmat(x,1,5)')
X=repmat(x,1,5)
pause

%of course the `5' in the command can be changed to any number
%suppose now that we would like to generate a vandermonde matrix
% 3x3 based on the vector x, i.e.,
%V(i,j)=x(i)^{j-1}

clear V; clear X;
disp('X=repmat(x,1,3)')
X=repmat(x,1,3)
pause

%this is V, save the fact the powers are still missing
%let's generate  a matrix J that contains the powers:
disp('j=[2:-1:0]')
j=[2:-1:0]
pause

disp('J=repmat(j,3,1)')
J=repmat(j,3,1)
pause

disp('V=X.^J')
V=X.^J
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('      POLYNOMIAL INTERPOLATION          ')
disp('++++++++++++++++++++++++++++++++++++++++')
%----------------------
%---------------------
%below this point is the experiment with polynomial interpolation
i=0;% ignore the parameter i. it is used only to control the color of the graph
disp('z=[0:.01:3.16];')
z=[0:.01:3.16];
disp('fsin=sin(z);')
fsin=sin(z); hold off
disp('plot(z,fsin)')
plot(z,fsin)
pause
disp('++++++++++++++++++++++++++++++++++++++++')
%now we interpolate sin(z) at the interpolation points x
disp('x=[.5;1.5;2.5];')
x=[.5;1.5;2.5];
disp('F=sin(x);')
F=sin(x);
disp('hold on')
hold on
pause
disp('++++++++++++++++++++++++++++++++++++++++')
%from now until we type `hold off', the plots are made one on top of the other

disp('inter')
inter
disp('plot(z,p,colr(i))')
plot(z,p,colr(i))
disp('e=max(abs(fsin-p))')
e=max(abs(fsin-p))
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('x=[.5;.7;.9];')
x=[.5;.7;.9];
disp('F=sin(x);')
F=sin(x);
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('inter')
inter
disp('plot(z,p,colr(i))')
plot(z,p,colr(i))
disp('e=max(abs(fsin-p))')
e=max(abs(fsin-p))
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('x(4)=2;')
x(4)=2;
disp('F(4)=sin(x(4));')
F(4)=sin(x(4));
pause

disp('inter')
inter
disp('plot(z,p,colr(i))')
plot(z,p,colr(i))
disp('e=max(abs(fsin-p))')
e=max(abs(fsin-p))
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('x(5)=2.8;')
x(5)=2.8;
disp('F(5)=sin(x(5));')
F(5)=sin(x(5));
disp('inter')
inter
disp('plot(z,p,colr(i))')
plot(z,p,colr(i))
disp('e=max(abs(fsin-p))')
e=max(abs(fsin-p))

disp('hold off')
hold off
pause


disp('++++++++++++++++++++++++++++++++++++++++')
disp('        ANOTHER EXPERIMENT              ')
disp('++++++++++++++++++++++++++++++++++++++++')
disp('%%%we try to interpolate 1/(1+t^2) on [0,3]')
disp('clear x F')
clear x F
disp('z=linspace(0,3)')
z=linspace(0,3); i=1;
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('fbell=1./(1+z.^2);')
fbell=1./(1+z.^2);
disp('plot(z,fbell)')
plot(z,fbell,colr(i))
pause

disp('++++++++++++++++++++++++++++++++++++++++')
disp('x=[0:.5:3]'';')
x=[0:.5:3]';
disp('F=1./(1+x.^2);')
F=1./(1+x.^2);
disp('hold on')
hold on
disp('inter')
inter
disp('plot(z,p)')
plot(z,p)
disp('e=max(abs(p-fbell))')
e=max(abs(p-fbell))
pause


disp('hold off')
hold off
disp('plot(z,p-fbell)')
plot(z,p-fbell)
pause
disp('++++++++++++++++++++++++++++++++++++++++')
disp('             end of demo                ')
disp('++++++++++++++++++++++++++++++++++++++++')

