% Math 32a   Fall 2003  Richard Palais  Brandeis Univ.

% In the following I assume that you already know how
% to start up Matlab and that you know how to add 
% directories (= folders) to the Matlab search path.
% The idea is to get a feeling for the Matlab syntax,
% and a feeling for how to create certain data 
% structures and manipulate them. What I suggest is that
% you go through the topics one by one, first reading the 
% notes, and then entering the commands in the Matlab 
% Command Window and make sure you understand why you 
% get the responses that you do.


% HELP and TUTORIALS

help  % gives topics and functions for which help is available
help <topic>   e.g., help plot
helpwin  % opens a help browser window
helpdesk  % A hypertext browser for Matlab documentation
lookfor <topic>   e.g. lookfor cos
% which <function-name>  % gives full path to function M-File.
demo  % stsrts a Matlab demo
 
% CALCULATOR

2 + 2
2 + 2;
ans*ans
a=2;
b=3;
c=a+b

% COMPLEX NUMBERS

a = 1 + 2i
b = 3 - 1i
c = a + b 

% VECTORS AND THE COLON AND LINSPACE NOTATION

a= [1 1 2 3.5 pi]
b = 1:5
c = a + b
1:0.1:2 % Creates a row vector with entries 1  1.1  1.2 ... 1.9  2

% ANOTHER WAY IS linspace(FIRST,LAST, NUMBER) 
linspace(1,2,10)   % Gives a row vector with 10 equi-spaces elements from 1 to 2.
% WHOOPS!
linspace(1,2,11)   % Gives a row vector with 11 equi-spaces elements from 1 to 2.

% MATRICES
% Explicit manual entering. [a b c...;d e f...; ...]
% OR  [a,b,c...;d,e,f...;...] 
% each element entered should either be a constant or an initialized variable.
% entering a semi-colon starts a new row.
% elements in each row can be separated either by spaces or commas.
A = [1 2 3 4; 5 6 7 8; 9 10 11 12] 
size(A)  % returns (r,c) where r is # of rows of A and c = # of columns
size(A,1)  % returns r
size(A,2)  % returns c

% BRANCHING
% if <condition> <statement>, <statement>, ... end
% or (better formatting)
% if <condition> 
%    <statement>
%    <statement>
%     ... 
%  end

% Notice that if you enter several Matlab commands on a single line,
% then they should be separated by either commas or (to supress output)
% semi-colons.

if (j ~= 0)  
  k = 1/j 
 end
 
 % the following defines the signum of x, i.e., +1, 0, or -1 depending on whether
 % x is positive, zero, or negative.
if  (x > 0) 
   sgnum = 1
 elseif (x == 0)  % NOTE!  == tests for equality. A single = is assignment.
   signum = 0 
 else
   signum = -1
end

% ITERATION USING FOR LOOPS

for i = 1:5 i*i, end
%
sum = 0;
for i = 1:5  sum = sum + i*i, end
%
sum = 0;
for i = 1:5  sum = sum + i*i; end
sum
%
t=0;
t=cos(t)

% The UP-ARROW KEY takes you back to the preceding command 
% This would make it easy here to execute t = cos(t) many times.
% But even more convenient here is to use a "for loop"
t = 0;
for i = 1:5, t = cos(t), end
% Better still make this more general as follows:
t = 0;
N = 5;
for i = 1:N  t = cos(t); end,  t
% For more convenience still we could make this into a Script
% Create a file named IterateCosScript.m (in your Matlab path)
% with the following lines. 
t = 0;
for i = 1:N  
   t = cos(t); 
 end 
 t
% Then---
N = 8;
IterateCosScript
% The next step is to make a function M-File  of this!
% Create a file named IterateCos.m 
function IterateCos(N)
t=0;
for i = 1:N 
   t = cos(t); 
end;
t
%
% Now call IterateCos(5), IterateCos(10),
% use format long to see more precision
format long
IterateCos(30)
IterateCos(60)
IterateCos(80)
IterateCos(100)

% Matlab keeps track of all the variables that you have 
% defined in the current session and their current values.
% The command 
%  whos 
%  gives list of variables in the workspace.
whos
% clear    clears the current workspace
clear
whos
% MORE COLON NOTATION
horizontal=[1:6]
horizontal=[horizontal, 7, 8]
horizontal =  (1/8)*horizontal
horizontal = horizontal(2:7)
vertical = horizontal'

% THE PLOT COMMAND
% We will look carefully at the plot command later, 
% but here is how to plot a list of (x,y) values and
% join successive ones by straight lines.

N=10;
x = (1/N)*2*pi*[0:N];  y = sin(x); plot(x,y);

N=20;
x = (1/N)*2*pi*[0:N];  y = sin(x); plot(x,y);

N=20; 
x = (1/N)*2*pi*[0:N];
plot(sin(x),cos(x));
% ??? why don't we get a circle?
plot(sin(x),cos(x)); axis equal;
a=3; b= 2;
plot(a*sin(x),b*cos(x)); axis equal;
