% when matlab comes across a <name> it will, in effect, carry out the command
%  which <name>
which x
x is a variable.
which z
z not found.
which sin
sin is a built-in function.
which page18
C:\courses\412\00\page18.m
which mkpp
C:\MATLABR11\toolbox\matlab\polyfun\mkpp.m

% If <name> is not a variable nor a built-in function, matlab will look for
% a file called <name>.m in the current directory, and then in all the
% directories in the matlab path.
% The command PATH manages the matlab path (do a help path)
path

		MATLABPATH

	C:\MATLABR11\toolbox\matlab\general
	C:\MATLABR11\toolbox\matlab\ops
	C:\MATLABR11\toolbox\matlab\lang
	C:\MATLABR11\toolbox\matlab\elmat
	C:\MATLABR11\toolbox\matlab\elfun
	C:\MATLABR11\toolbox\matlab\specfun
	C:\MATLABR11\toolbox\matlab\matfun
	C:\MATLABR11\toolbox\matlab\datafun
	C:\MATLABR11\toolbox\matlab\polyfun
	C:\MATLABR11\toolbox\matlab\funfun
	C:\MATLABR11\toolbox\matlab\sparfun
	C:\MATLABR11\toolbox\matlab\graph2d
	C:\MATLABR11\toolbox\matlab\graph3d
	C:\MATLABR11\toolbox\matlab\specgraph
	C:\MATLABR11\toolbox\matlab\graphics
	C:\MATLABR11\toolbox\matlab\uitools
	C:\MATLABR11\toolbox\matlab\strfun
	C:\MATLABR11\toolbox\matlab\iofun
	C:\MATLABR11\toolbox\matlab\timefun
	C:\MATLABR11\toolbox\matlab\datatypes
	C:\MATLABR11\toolbox\matlab\winfun
	C:\MATLABR11\toolbox\matlab\demos
	C:\MATLABR11\toolbox\symbolic
	C:\MATLABR11\toolbox\wavelet\wavelet
	C:\MATLABR11\toolbox\wavelet\wavedemo
	C:\MATLABR11\toolbox\stats
	C:\MATLABR11\toolbox\splines
	C:\MATLABR11\toolbox\tour
	C:\MATLABR11\work
	C:\MATLABR11\toolbox\local


% To find out the current directory, use
pwd
ans =
c:\courses\412\00

% Continuing with the text, here is the script file ExpPlot as copied from the
% book's website -> m-files -> chapter 1, ...
type ExpPlot

% Script File: ExpPlot
% Examines the function
%
%      f(x) = ((1 + x/24)/(1 - x/24 + x^2/384))^8
%
% as an approximation to exp(z) across [0,1].

close all
x  = linspace(0,1,200);

y = ((1 + x/24)./(1 - x/24 + x.^2/384)).^8;

plot(x,y,x,exp(x))

% ... but with the several statements there replaced by the one-liner
% vectorized version of the function.

% I showed the resulting plot, complained about lack of distinction, so added
% distinguishing symbols to the two graphs, then complained about the lack
% of accuracy and proposed my better version, ...
% and then a student pointed out that, in the book, the second term in the
% numerator was -x/12 rather -x/24. 
%   MORAL:  you can never be too careful; always check for yourself!
% After that correction, the book's corrected
% approximation to exp(x) was visually perfect, and my correction not quite so.
%
% Here is the final version, which includes use of 'linew', to control the
% linewidth:

type ExpPlot

% Script File: ExpPlot
% Examines the function
%
%      f(x) = ((1 + x/24)/(1 - x/12 + x^2/384))^8
%
% as an approximation to exp(z) across [0,1].

close all
x  = linspace(0,1,200);

y = ((1 + x/24)./(1 - x/12 + x.^2/384)).^8;

yy = ((1 + x/24)./(1 - x.*(1/12 - x/96))).^8;
plot(x,y,'-r',x,exp(x),'--g')
hold on
plot(x,yy,'linewidth',2)
hold off
ExpPlot

%  on to page 18 and the plotting of many pictures in one:

type page18

% from page 18
x = linspace(-pi/2,9*pi/2,200);
y = tan(x);
plot(x,y)
pause
axis([x(1) x(end) -10 10])

page18
% contrary to the book's claim, the command
axis
ans =
   -1.5708   14.1372  -10.0000   10.0000
% ... does NOT go back to auto-scaling. For that, you must say
axis auto

% Next, we went into the plotting of polygons, given their vertices.
% I objected to the book's redefining of the vectors  x  and  y , 
% proposing to use instead matlab ability to make new vectors from the entries
% of given vectors, but the command  a = x(b) which gives the vector, of the
% same length as  b , with  a(i) = x(b(i)), i=1:length(b), PROVIDED the entries
% of b are natural numbers no bigger than  length(x).
%
% I also introduced the key word   end , as in  x(end) , to indicate the last
% entry of the vector  x . Thus  x(end:-1:1) is the vector that contains the
% entries of x but in reverse order.

x = [1,3,5,3,2]; y = [-1,-2,-3,2,1];
plot(x,y)
hold on
plot(x,y,'o')
hold off
plot(x([1:end,1]),y([1:end,1]))

%  Then we looked at the book's script for plotting various regular polygons.
%
type Polygons

% Script File: Polygons
% Plots selected regular polygons.

close all
theta = linspace(0,2*pi,361);
c = cos(theta);
s = sin(theta);
k=0;
for sides = [3 4 5 6 8 10 12 18 24]
   stride = 360/sides;
   k=k+1;
   subplot(3,3,k)
   plot(c(1:stride:361),s(1:stride:361))
   axis([-1.2 1.2 -1.2 1.2])
   axis equal
end

% why would  stride=7 not work??

Polygons

% Make sure to finish such a script with the command  close  since else the
% next plot will end up on one of those subplots.

% Final topic: a sum of sines
%
% To plot  f(x) = 2*sin(x) + 3*sin(2*x) + 7*sin(3*x) + 5*sin(4*x), use
% the full vectorizing power of matlab. With
n = 200; x = linspace(-10,10,n)';
% can compute  y = f(x) by

y = sin(x*(1:4))*[2;3;7;5];

% making use of the fact that the product of the column vector x of length n 
% with the row vector 1:4 produces the matrix of size n-by-4, having x as its
% first column, 2*x as its second column, 3*x as its third column, etc.;
% and using, further, that, in matlab,  sin(A) produces a matrix of the same
% size as A, with A(i,j) replaced by sin(A(i,j)), all i, j.

% So, the short version of the bottom script on page 23 is
type page23

% modified version of the script on page 23
n = 200; x = linspace(-10,10,n)';
plot(x,sin(x*(1:4))*[2;3;7;5])
title('f(x) = 2sin(x) + 3sin(2x) + 7sin(3x) + 5 sin(4x)')

% ... and this works as advertised:
page23
