% Bare essentials of MATLAB for first Math 53 X-hr -  Alex Barnett 9/27/07
%
% In windows or Mac, click on the Matlab icon.
% In linux I recommend starting it from command line without
% a command window, since less buggy, ie:  `matlab -nodesktop'

% Anything after a % is a comment (read by humans, ignored by matlab!)
% You can type (paste) each following line at the command prompt:

a = 5                                 % a symbol can be a number (scalar)
a = 5;                                % end command with ";" if want silent
sa = sin(a)                           % sa now contains sin of a, ie sin(5)
b = [1 2 3]                           % a row vector (row vec)
c = [3; -2; 4.6]                      % ; separators = column vector (col vec)
c(3)                                  % look up the 3rd element of c
b*c                                   % dot product
% NOTE: generally * computes matrix product using standard rule for matrices.
% So here b*c computes dot product (giving a 1-by-1 scalar answer),
% but c*b would compute `outer product' (a 3-by-3 matrix) - watch out!
dot(b,c)                              % = dot(c,b). Better way to do dot prod
d = c'                                % d is now transpose of c, so a row vec
b.*d                                  % element-by-element multiply, a 1-by-3
% NOTE this means b and d must be the same size (shape), here 1-by-3.
sin(b)                                % sin of all elements at once (fast!)
% Elegant: we don't have to write a loop to compute sin of many numbers at once

plot(b, c, '+-');                     % plot c vs b using + symbols and lines
% Note: this means plot the 3 points (b(1),c(1)), (b(2),c(2)) and (b(3),c(3))
% b is interpreted as `x values' (horizontal), c as `y values' (vertical).
% Number of elements in b and c must match up (unless want multiple lines) 
axis([0 4 -3 5]);                     % manually choose view domain

help                                  % list of help menus
help plot                             % get help on a command
doc                                   % if you prefer graphical help window
whos                                  % show all currently-defined variables
save mydata                           % save all variables as file mydata.mat
quit                                  % need I say?
load mydata                           % load vars (can overwrite variables)
clear                                 % clear all variables (careful!)
% Use up and down arrows to scroll through recent commands, enter to execute


% Plotting a graph of a function ------------------
x = 0:0.001:1;                       % first you need a list of ordinates
size(x)                              % tells you what 'shape' (size) array x is
y = sin(1./x);                       % apply sin(1/x) to each x value in list
% NOTE  ./ rather than just / is needed since you want elementwise divide

figure; plot(x, y, 'r-');            % new fig, plot with red solid line.
hold on;                             % get ready to add rather than overwrite

x(1:5)                               % this is the first 5 elements of the list
% NOTE since x is 1-by-something (or something-by-1) we can treat as a 1-d list
x(990:end)                           % this is the last 12 elements
x(40:43)                             % chunk from middle of list
plot(x(1:100), y(1:100), 'ko');      % show first 100 entries with black o's
xlabel('x'); ylabel('y'); title('sin(1/x)'); % always label your axes!


% WRITING TO INDIVIDUAL ROWS or COLUMNS of ARRAY (matrix), and LOOPS ----------
x = zeros(2,10)                       % make a 2-by-10 matrix of zeros
x(2,1) = 1                            % change the 2,1 element of x
x(:,1)                                % just the first column of x
x(2,:)                                % just the 2nd row of x
A = [1 -1; 1 2]                       % make 2-by-2 matrix, acts on R^2 col vec
x(:,2) = A*x(:,1)                     % set 2nd col of x = A*(first col of x)
x(:,3) = A*x(:,2)                     % set 3rd col of x = A*(2nd col of x)
% ... you get the idea: we could repeat, with each col of x storing an ITERATE
% of the linear map x <- Ax
% Let's do this less tediously, using a loop!
for n=2:10                            % give a list of n values (stop at 10)
  x(:,n) = A*(x(:,n-1));              % iteration x(n) = A*x(n-1)
end                                   % ends the loop
figure; plot(x(1,:), x(2,:), '+-');   % tell me why this works...
xlabel('x_1'); ylabel('x_2'); title('iterate A')

% See course website links for more complete introductions. Enjoy!
