Last update 22.1.99 Tue 19.1 1999

L1.html

Learning how to enjoy Matlab (and Maple)

Sources

Starting Matlab, path and startup.m

Task # 1, startup.m (Re-edit please! (22.1.))

Include all directories (perhaps later on) where you (will) have useful m-files. Cut (left button)/paste (middle button).
mkdir matlab     # Make directory (if not made already)
cd matlab        # go there
emacs startup.m &

path(path,'/p/edu/matlab'); % path(path,'/p/edu/matlab/mathews/'); % path(path,'/p/edu/matlab/piche/'); path(path,'/p/edu/mat-1.174/matlab'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap2/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap3/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap4/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap5/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap6/') path(path,'/p/edu/mat-1.174/matlab/cooper/chap7/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap8/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap9/'); path(path,'/p/edu/mat-1.174/matlab/cooper/chap10/'); % path(path,'/p/edu/mat-1.192/matlab');
This is an m-file containing Matlab-commands. It makes all m-files in these directories visible (and usable) to you. (You can include any Matlab-commands you want to be executed in the beginning of your Matlab- session.)

Documenting your work, "poor (wo)man's worksheet"

It is recommended you have an Emacs (or any text editor you please) window open while you work. Cut/paste commands back and forth. That is, all your inputs and interesting outputs. If you want an .m-file that you can execute as a batch file, include %-signs in the beginning of each "non-command-line"=comment-line .

If you don't like this, you can work in the Matlab window only (make it large enough). Give the command

>> diary mysession.txt
>> command1
>> ...
>> diary off
Then all the inputs and outputs produced between these two diary commands will be written to the file mysession.txt (and also echoed on the terminal). If you work this way you should tidy up and add comments into the session file afterwards. Of course this requires you to remember to put diary on before you start to produce something worth saving.

I personally prefer the cut/paste-way of working but you are free to disagree.

helpdesk -- Getting started-- comments

What is Matlab ...

Who cares ???

Matrices and magic squares

Reading Kermit Sigmon: Matlab Primer

First steps in Matlab, comparison to Maple
>> x = []; for i = 1:n, x=[x,i^2], end   % This is misuse of Matlab

> x:=[];for i to 4 do x:=[op(x),i^2] od; # This is misuse of Maple

>> 1:5                  % Do this instead

> $1..5;                # Do this instead or 
> seq(i,i=1..5);

Table of values 

>> x = [0.0:0.1:2.0]';         > x:=[seq(i*0.1,i=0..20)]:
>>      y = sin(x);            > y:=map(sin,x):
>>      [x y]                  > matrix([x,y]); linalg[transpose](%);

5-point discretization of Laplace/Poisson

Suitable textbooks here are for instance many AEM-books, (Advanced Engineering Mathematics) like

Assembling the matrix and RHS

Differential equations with Matlab

y'=A y

Matlab has the eA t -function expm . The above kind of "misuse" can be quite nice for a short and easy solution: (I don't blame anybody for this, although it's not hard to write it in a more vectorizable form.)
>> A=[0 -6 -1;6 2 -16;-5 20 -10]

A =

     0    -6    -1
     6     2   -16
    -5    20   -10

>> x0=[1,1,1]'

x0 =

     1
     1
     1

>> X=[];
>> for t=0:.01:1 X=[X,expm(t*A)*x0];end
>> size(X)
ans =

     3   101

>> plot3(X(1,:),X(2,:),X(3,:))
>> grid
>> figure;plot(X(1,:),X(2,:)) 
>> 

Direction fields

% seuler.m  -- Let's agree to take initial s for 'session' or 'script'
% (as opposed to function)
m=2; hold on;
% cut/paste into Matlab-session: "up-arrow-iterate"
[T,Y]=meuler('fode1',0,3,2,m); plot(T,Y,'r',T,Y,'o');[T' Y'],m=2*m;

[t0,y0]=ginput(1);[T,Y]=meuler('fode1',t0,4,y0,m); plot(T,Y,'r');


dir1.m
% Direction field m-file (script) 
% Source: Bäckström: Praktisk matematik med Matlab
clear variables; hold off;
xmax=4; ymax=3; delx=xmax/30; dely=ymax/30;   % *** problem dependent
[X,Y]=meshgrid(0:delx:xmax,0:dely:ymax);
  DX=ones(size(X));  % Unit x-xomponents of arrows
  DY=fode1(X,Y);      % Slopes, *** Change the name 'fode' to your m-file name
  L=sqrt(DX.^2+DY.^2); % Initial lengths
  DX1=DX./L; DY1=DY./L; % Unit length for all arrows
%figure(1); quiver(X,Y,DX1,DY1,'.'); grid on;  % The period ('.') suppresses
                                               % arrowheads
figure(1); quiver(X,Y,DX1,DY1); grid on;
  axis([0 xmax 0 ymax]); title('Direction field');

Example m-files

fode1.m

function yp=fode1(X,Y); yp=-Y;