Starting Matlab


matlab -nojvm

editing files: xemacs

most important command:
help <function name>

Variables:

 a = [1 3 4 5];

>> b = [2 3 4; 5 6 7; 4 5 6]
b =

2 3 4
5 6 7
4 5 6

>> x = 1:5:20
x =

1     6    11     16

disp(a)


Useful stuff to know for later


pi

load <filename (.txt .mat)> - make a text file with some numbers

save <filename>

clear, clear all : empty out whatever variables you created

who, whos : list the current variables you created

cd pwd ls : same function as in unix

Language:


for

x=[];
for (i = 1:n)
    x = [x,i^2]
end


while

while (input('again? ', 's') == 'y')
     disp('one more time')
 end

again? y
one more time
again? y
one more time
again? n


if else
 

if (1 > 2)
    disp('error')
else
    disp('whew')
end


Also try if...elseif...else


Useful functions
and operators:


randn, rand

abs sqrt cos sin log exp 

^ * + - /

== && || 


Scripts (.m files), editing, comments (%)


Graphics:


Plot(rand(100,1))

Hold on: next plot command writes ontop of last one, rather than clearing it.

plot(50,0.5,'o')

Stuff to play with later:

Title xlabel ylabel

Surf/mesh: surf(rand(8,8))

Close: close the current figure window

Subplot(3,4)

Figure(3)

Cla clf : delete parts of the plot, or all of it

Ginput: get X and Y values from a plot; not snapped to the line

Load/save/open figures

 % Line plot of a chirp
x=0:0.05:5;
y=sin(x.^2);
plot(x,y);


Matrices


Eye(3)

Ones(2,5)

Zeros(5,2)

Size(a)

>> diag([1     2     3])

ans =

1 0 0
0 2 0
0 0 3

A'

A .* B

>> a(:,3)

ans =

3
3

>> a(1, 3:5)

ans =

3 4 5

>> a(1:2, 4:5)

ans =

4 5
4 5

Sum(A)

Prod()

>> max([1 2 9 3])

ans =

9

>> [amount, location] = max([1 2 9 3])

amount =

9


location =

3



max min mean 


In class assignments:



1. load a list of numbers from a file
2. square them
3. plot them
4. save the figure
5. save the squared numbers




1. enter number N between 5 and 10
2. repeat until user enter valid number
3. print numbers from 1 to N



1. generate 100 random numbers: rand(100,1)
2. plot them as a function
3. compute 5-point running average (using for-loop): 96 nums
4. plot running average on top of rands (different color)
5. find minimum of run av (value and index)
6. plot circle at minimum