% Bare essentials of MATLAB for Math 23 Diff Eq - Alex Barnett 9/25/07
% To start: 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' (ignored by matlab).
% You can type or paste each line to the command line,
% or save it to your workspace as intro.m and
% run the whole thing as a program by typing `intro' (not so enlightening)
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. Careful with order!
% NOTE: generally * computes matrix product.
% So here b*c computes dot product (a scalar),
% but here c*b would compute `outer product' (a 3-by-3 matrix) - watch out.
dot(b,c) % = dot(c,b). Better way for dot prod
d = c' % d is now transpose of c (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!)
% NOTE we didn't have to write a loop to compute sin of many numbers at once.
% 2D GRAPHS
plot(b, c, '+'); % plot graph of c vs b using + symbols
% 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 axes range
plot(b, c, 'g-'); % plot same using instead green lines
help % list of help menus
help plot % give help on eg plot command
doc % if you prefer graphical help window
whos % show all currently-defined variables
save mydata % save all variables as file mydata.mat
quit % close matlab
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
% I will postpone matrices & linear algebra for later...
% DIFF EQs
% Solving general 1st-order ODE y' = f(t,y) numerically ----------------------
f = @(t,y) 1-y; % set up func of 2 vars f(t,y) = 1-y
f(2,3) % check it evaluates correctly
yo = 0; % initial condition at t=0 is yo=0
[ts, ys] = ode45(f, [0, 3], yo); % solve ODE (using 'Runge-Kutta')
% over the t-range [0,3]
% ts is now a col vec of t values, ys is col vec of corresponding y values
% ----------------------------------------------------------------------------
plot(ts, ys, '+-'); % plot solution with + symbols and lines
yex = 1 - exp(-ts); % compute analytic (exact) y values
% at each already-existing t value
% Note that we subtracted exp(ts) which is a col vec, from 1 which is a scalar,
% but it figured out to do the right thing (make 1 an appropriate VECTOR of 1s)
hold on; % plot the next graph over this one
plot(ts, yex, 'ro'); % compare exact solution as red o symbols
hold off; % stop overlaying future graphs
figure(2); % start a new figure
plot(ts, ys-yex, '+-'); % Subtract the two - best way to compare!
xlabel('t'); ylabel('y error'); % label axes: important to communicate!
title('dy/dt = 1-y: error in numerical solution ys-yex');
% See course website links for more complete introductions. Enjoy!