MATLAB Basics

Start MATLAB on your machine. You get a prompt like this:

`>>`

Here are some things you can do.

```>> diary on   % Causes Matlab to save a record of the session in a
% file named diary.  Later you can edit this file,
% print it out, turn it in as homework, or whatever.

>> a = [1 2 ; 3 4]

a =

1     2
3     4

>> x = ones(2,1)

x =

1
1

>> b = a*x

b =

3
7

>> y = a\b    % Solves system Ay=b for y.

y =

1.0000
1.0000
```
```>> c = a'        % transpose

c =

1     3
2     4

>> H = a'*a

H =

10    14
14    20

>> chol(H)     % upper triangular Cholesky factor

ans =          % The default variable name is ans.

3.1623    4.4272
0    0.6325

>> m = ans

m =

3.1623    4.4272
0    0.6325

>> A = a^2; % exponentiation.  Semicolon suppresses printing of result.
>> A        % To have a variable printed out, just type its name.

A =         % Matlab is case sensitive:  A and a are different.

7    10
15    22

>> A(1,1) = A(1,1) + 13

A =

20    10
15    22

>> [L U] = lu(A)   % LU decomposition (with pivoting if necessary)

L =

1.0000         0
0.7500    1.0000

U =

20.0000   10.0000
0   14.5000

>> AI = inv(A)

AI =

0.0759   -0.0345
-0.0517    0.0690

>> p = A*AI

p =

1.0000         0
0    1.0000

>> format long               % some alternative formats
>> p

p =

1.00000000000000                  0
0   1.00000000000000

>> format short e
>> p

p =

1.0000e+00            0
0   1.0000e+00

>> format                     % The default format is called `short'.
>> p

p =

1.0000         0
0    1.0000

>> a = hilb(3)       % 3 by 3 Hilbert matrix

a =

1.0000    0.5000    0.3333
0.5000    0.3333    0.2500
0.3333    0.2500    0.2000

>> b = 1:3

b =

1     2     3

>> b=b'

b =

1
2
3

>> x=a\b

x =

27.0000
-192.0000
210.0000

>> r=b-a*x
```
```r =

1.0e-14 *       % scale factor for the array

0
-0.7105
0

>> n = norm(r)

n =

7.1054e-15

>> cond(a)

ans =

524.0568

>> a = zeros(4,4)

a =

0     0     0     0
0     0     0     0
0     0     0     0
0     0     0     0

>> for i=1:3
a(i+1,i)=1;      % a simple loop
a(i,i)=2;        % The whole thing could have gone on one line.
a(i,i+1)=3;
end
>> a(4,4)=2;
>> a

a =

2     3     0     0
1     2     3     0
0     1     2     3
0     0     1     2

>> quit                 % Quit MATLAB

% Some other useful commands are:

>> intro

>> demo

>> help

>> help help

>> help elmat

>> help eig

>> help plot

>> help sparse

>> help [name your favorite topic]

% Other commands are less useful:

>> why

% Chances are, you are running a recent version of MATLAB with
% a GUI interface.  In this case you can do lots of things using
% the pulldown menus and push buttons.  There is also a built-in
% editor.
```

2004-01-15