% Bare essentials of MATLAB for Math 23 Diff Eq - Alex Barnett 9/22/05
% touched up after class, 9/22
% 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
% You can type each line at the command line, or save it as intro.m and
% run the whole thing as a program by typing `intro'
a = 5; % a symbol can be a number (scalar)
a % without a ; the answer is printed
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] % a column vector (col vec)
c(3) % look up the 3rd element of c
b*c % dot product. Careful with order!
% * computes matrix product. Therefore b*c computes dot product (a scalar),
% but c*b computes 3-by-3 `outer product'.
dot(b,c) % dot(c,b) same, 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
sin(b) % sin of all elements at once (fast!)
plot(b, c, '+'); % plot 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 % 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
% I will postpone matrices & linear algebra for later...
% Solving general 1st-order ODE y' = f(t,y) numerically ---0------------------
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!