You are on page 1of 21

Introduction to Matlab

Introduction to Matlab
Maryam Saeedi
August 2, 2009
Introduction to Matlab
Introduction
Why do we use MATLAB?
Relatively easy.
You have an access to it in this computer lab
(and in Econ lab).
A free, similar software is available (GNU Octave).
Most people use it.
BUT for more serious programming problems we use
FORTRAN 90 or C / C++.
Introduction to Matlab
Introduction
Starting Matlab
1 WINDOWS - obvious :)
2 Starting from CLA UNIX server
Run Hummingbird-Exceed
Connect through an SSH terminal (e.g. Putty) with
thor.socsci.umn.edu
(make sure you enable X11 forwarding)
After login, type matlab.
NOTE: you can run MATLAB programs on UNIX without a
pop-up window which makes it faster: No JAVA interface!
Introduction to Matlab
Basic MATLAB programming
Scalar Operations
Fundamental operations and built-in variables/functions
Fundamental operations
math MATLAB
1 + 2 1 + 2
1 2 1 - 2
1 2 1 * 2
1 2 1 / 2
2
2
2^2
There are built-in functions such as sqrt, log, exp, sin etc.
There are built-in variables such as pi, etc.
Introduction to Matlab
Golden Ratio
Golden Ratio
=
1+

5
2
Figure: The Golden Rectangle.
Introduction to Matlab
Golden Ratio
Equating aspect ratio:
1

=
1
1
Therefore is the roots of the following equation:

2
1 = 0
=
1

5
2
The golden ratio is the positive root.
Introduction to Matlab
Golden Ratio
You can use MATLAB to get the roots of the polynomial
directly.
MATLAB represents a polynomial by the vector of its
coecients, in descending order.
p = [1 1 1]
The roots are produced with roots function:
r=roots(p)
r=
-0.61803398874989
1.61803398874989
you can pick the second component with:
phi= r(2)
Introduction to Matlab
Golden Ratio
In MATLAB you can use an anonymous function:
f=@(x) 1./x-(x-1)
denes: f(x) = 1/x (x 1)
The graph of f(x) over the interval 0 < x < 4 can be
obtained with:
ezplot(f,0,4)
To get the root of f(x) around 1 you can use the following
command:
phi=fzero(f,1)
you can plot this point on the top of the old graph:
hold on
plot(phi,0,o)
Introduction to Matlab
Golden Ratio
Figure: The Golden Rectangle.
Introduction to Matlab
Golden Ratio
The following commands will plot the golden rectangle with
the text as shown previously.
Introduction to Matlab
Golden Ratio
Another representation of Golden Number is the following:
We can estimate the above sequence up to n iteration to get
an estimate of the golden number.
Introduction to Matlab
Golden Ratio
You should save the above in a le called goldfract.m to have
access to it.
Introduction to Matlab
Fibonacci Numbers
Fibonacci Numbers
Leonardo Pisano Fibonacci posed the following question:
A man put a pair of rabbits in a place surrounded
on all sides by a wall. How many pairs of rabbits can
be produced from that pair in a year if it is supposed
that every month each pair begets a new pair which
from the second month on becomes productive?
The answer to it generates Fibonacci numbers.
Let f
n
denote the number of pairs of rabbits after n months.
Then we have:
f
n
= f
n1
+f
n2
with these initial conditions:
f
1
= 1 & f
2
= 2
Introduction to Matlab
Fibonacci Numbers
Fibonacci Numbers
The following function generates the rst n Fibonacci numbers.
Introduction to Matlab
Fibonacci Numbers
Fibonacci Numbers
We can generate the Fibonacci numbers using a recursive
function in MATLAB.
A recursive program is elegant, but expensive. You can
measure execution time with tic and toc.
Introduction to Matlab
Fibonacci Numbers
One other estimate for Golden ratio comes from Fibonacci
numbers.
lim
n
f
n+1
f
n
=
To see this, compute 40 Fibonacci numbers:
n=40;
n=bonacci(n);
Then compute their ratios:
f(2:n)./f(1:n-1)
Introduction to Matlab
Magic Squares
Magic Squares
Magic Square is an N-by-N matrix constructed from the
integers 1 through N
2
with equal row, column, and diagonal
sums. Produces valid magic squares for all N > 0 except
N = 2.
You can get the magic square of order N with the following
command:
A=magic(3)
you can compute the sum of rows and columns with the
following commands:
sum(A) & sum(A)
The summation of the elements in the two diagonals can be
calculated with:
sum(diag(A)) & sum(diag(ipud(A)))
Introduction to Matlab
Magic Squares
Some other matrix operations are:
det(A), inv(A), eig(A), rank(A),
A(1,2)=10, A(:,3)= [2;3;4], A=A(:,[3 2 1]),
Introduction to Matlab
The 3n + 1 Sequence
The 3n + 1 Sequence
Start with any positive integer n. Repeat the following steps:
If n = 1, stop.
If n is even, replace it with n/2.
If n is odd, replace it with 3n + 1.
The unanswered question is, does the process always
terminate?
Introduction to Matlab
The 3n + 1 Sequence
You can nd the sequence using the following commands:
Introduction to Matlab
The 3n + 1 Sequence
Do the following exercises:

You might also like