Matlab Notes


Matlab is an interpreted language (unlike C, C++, some Java)

see (http://en.wikipedia.org/wiki/Interpreted_language) for more details if you want

interpreted language - programs are executed line by line at runtime


compiled language - programs are compiled before runtime into machine
code which is then run

Advantages of interpreted language: Easy to use, faster edit-run cycle

Disadvantages of interpreted language: Slower (computer can't do some of the optimizations it can with compiled languages)

Matlab users can avoid some of the slowness by using built in matlab compiled c routines -- I'll show you several examples of this

Another disadvantage: expensive software required to run programs ---------------------------------------------------

Starting Matlab

Type matlab at the prompt (note if this is too slow you can type matlab -nojvm for just a plain text entry box) (to exit type exit)

matlab window can give you a multi-part window
1) main window where you enter commands and get results
2) top right - access to files, variable info
3) bottom right - history info
but you can also have just one big command window
see video at offline video or online video AND online video 2012a
Here's an example of what you will be able to do in a few weeks (click on "optimal fit of a non-linear function" in demos list)
----------------------------
Matlab is very easy to teach yourself
Getting Help:
> helpdesk
> helpwin
> help
> type shows you the code for the function (unless it's a compiled function)

> lookfor
-------------------------------
The "previously recommended" textbook
right column shows you code and output

exercises in each chapter (solutions in Appendix A)

App D: Toolbox Summaries
App E: Graphics Summary
App C: Mathematical Functions grouped by Area
App B: Help info (grouped by Area)
App A: Solutions to Exercises

------------------------------
Emacs commands for editing the command line
Diary command
> diary -- stores output to a file
> diary off

Matlab as a Calculator


---------------
5+4


ans =

9

sin(3.145)

ans =

-0.0034

sin(pi)

ans =

1.2246e-16

help sin
SIN Sine.
SIN(X) is the sine of the elements of X.

See also asin, sind.

Overloaded functions or methods (ones with the same name in other directories)
help sym/sin.m

Reference page in Help browser
doc sin -----------------------------
semicolons at the end of lines suppress output
you do not need to declare variables
> a=1;
> a=1
a = 1
------------------------------

Matlab is built around matrices.


>A=[1 2 3; 4 5 6; 7 8 9] %%% comments after % char rows sep by ;
%% columns separated by space or comma
A =

1 2 3
4 5 6
7 8 9


Can also use carriage return to separate rows

> A = [1 2 3
4 5 6
7 8 9]


A =

1 2 3
4 5 6
7 8 9

vectors are 1-dimensional matrices

-can be created quickly using the colon notation

> a=1:10
a =

1 2 3 4 5 6 7 8 9 10

Matlab can operate on vectors and return vectors

> b=a+1
b =

2 3 4 5 6 7 8 9 10 11

>sin(a)
ans =

Columns 1 through 9
0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121
Column 10
-0.5440

> a = 1:2:10
ans =

1 3 5 7 9

a=1:-1:-10

a =

1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10


---------------------

Control flow in Matlab



logical relations

<
>
<=
>=
==
~=
&
|
~
xor
1 (true)
0 (false)


if

elseif

else

end



for i=

end

usual way to set set of vals 1:5 or initial:step:final

>> for i=1:2:5
i
end

i =

1


i =

3


i =

5




--------------------
timing aside

tic
toc to bracket commands


---------------------
First example of how to write fast matlab code

largenum = 50000;
b=1:largenum;

%% want to add 1 to every number in b

%% standard way
tic
for i=1:largenum
c(i) = b(i)+1;
end
toc

Elapsed time is 13.933294 seconds.

But this is misleading -- most of this is slow incremental memory allocation

Run the same line again (or similar line) or allocate memory for
c first

c=zeros(1,50000);

>> tic; for i=1:50000; c(i)=b(i)+2; end; toc;
Elapsed time is 0.001675 seconds.


%%non loop way
tic
c=1+b;
toc
tic; c=b+1; toc;
Elapsed time is 0.0010772 seconds.

Just a few years back this used to be a much bigger difference!

Big loops in Matlab can be Slow (especially if doing memory allocation)! (Avoid if you can) BUT Matlab is getting a lot smarter at making them fast.


-------------------------ASIDE (optional reading for afficionados) ------
In 2005 Edition of the recommended text
There are two functions rands1.m and rands 2.m
(to generate sorted (ascending) random uniformly distributed numbers)

(stored in file rands1.m)
function u=rands1(n)
utilde=rand(1,n);
u(n)= utilde(n)^(1/n);
for i=n-1:-1:1
u(i)=u(i+1)*utilde(i)^(1/i);
end

(stored in file rands2.m)
function u=rands2(n)
u=fliplr(cumprod(rand(1,n).^(1./(n:-1:1))));



They present the timing as
tic; rands1(100000); toc
elapsed_time= 3.1300


tic; rands2(100000); toc
elapsed_time= 0.3900

Running on my machine version 2009B

I get
>> tic; rands1(100000); toc
Elapsed time is 0.048730 seconds.

>> tic; rands2(100000); toc
Elapsed time is 0.032233 seconds.

Not much of a difference!! Computers have gotten a lot faster since 2005 and
Matlab has also gotten smarter about dealing with loop code.
(Writing vectorized code can still make a difference in big and complicated computations though)

------------END OF ASIDE----------------------------------------------

Vectorizing commands



many commands can be used in a vectorized way (avoids loops) (e.g. sum)

(sums all the numbers in the vector given as the argument)
>> sum([1 2 3 4 6 9 10 20])

ans =
55

Another convenient command to avoid loops is find


%% want to find which entries in an array are less than or equal to 5

largenum=50000;
b=1:largenum;

> find(b<=5)

ans =

1 2 3 4 5

find command gives indices where condition (in parentheses is true)


> find(b > (largenum-10))

ans =

Columns 1 through 8

49991 49992 49993 49994 49995 49996 49997 49998

Columns 9 through 10

49999 50000


regular slow way (currently takes about 2-3 times as long for this example)

j=1;
for i=1:largenum
if (b(i)<=5)
a(j)=i;
j=j+1;
end
end
a