Transcript Document
Introduction to
Matlab Workshop
Matthew Johnson, Economics
October 17, 2008
7/17/2015
1
Getting Help
Useful links: http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp
The help links on this page include Mathworks’
Getting Started (the official online
documentation of Matlab) Kermit Sigmon’s
MATLAB Primer (a very good beginner
manual); University of Utah’s MATLAB
Tutorial and some others.
MATLAB’s online help manual
Statlab Consultants
2
What is MATLAB?
MATLAB is a matrix-based tool for numerical
computations. It’s very powerful and easy to use.
Both programming language and interactive
environment
Very fast native functions; very slow when
processing loops
Lots of available toolboxes
3
Acquiring Matlab
www.yale.edu/software
Free for Yale students
Installed on all Statlab computers
4
Launching Matlab
Double-click the “MATLAB R2008a” icon on
the desktop
5
The Interface
Main Window: Input/Output
Workspace: consists of the variables
you create during a MATLAB session;
Command History: double click them
to evaluate them;
Current Directory browser: shows you
where you are.
6
Interface (continued)
Can use Matlab as a calculator
Type “help” for a list of all help topics
help abs gives the syntax and information about
the absolute value command
7
Entering Matrices
Matrices can be :
Entered manually
A = [1 2 3 ; 4 5 6 ; 7 8 9]
Generated by built-in functions
Loaded from a file
File>Import Data
8
Matrix operations:
+ addition
- subtraction
* multiplication
^ power
‘ transpose
To make operations apply element-by-element,
we precede the operators by ‘.’
9
Subscripts:
Subscripts: the element in row i and column j of
A is denoted by A(i, j).
i,j can also be vectors of indices or logical arrays:
A=4*[1 2 3 4 5 6 7 8 9]’
b=A>18; c=[5 6 7 8 9]’
A(b), A(c)
10
The Colon Operator ‘:’
The colon ‘:’ is one of MATLAB ’s most important
operators. It has many formats:
Subscript expressions involving colons refer to
portions of a matrix:
A(5:9) is the fifth to the ninth elements of A.
[0:0.2:3] is a row vector containing integers from 0
to 3, in increments of 0.2
11
Matrices and Random Numbers:
Four functions that generate basic matrices:
Zeros: all zeros. A = zeros(1,3)
Ones: all ones. A = ones(2,4)
Rand: elements are U[0,1] random variables
A = rand(3,5)
Randn: elements are standard-normal random variables
A = randn(2,5)
Be careful: Matlab always sets the same seed.
Get ‘more random’ numbers by typing
rand('state', sum(100*clock))
12
A=eye(3) gives a 3-by-3 identity matrix
sparse(m,n): same as zeros(m,n), use if
most elements are zeros.
Concatenation: join small (compatible) matrices
to make bigger ones:
B = [A A-2; A*2 A/4]
Deleting rows and columns:
B(:,2) = [ ]
13
Suppressing Output:
If you simply type a statement and press Enter,
MATLAB automatically displays the results on screen.
If you end the line with a semicolon ‘;’, MATLAB
performs the computation but does not display any
result.
Example: C = randn(5,1) v.s.
C = randn(5,1);
14
Aside: You can run Matlab remotely!
Available to anyone with a Yale pantheon
account
You need the following (free) software
Xming (available from
http://sourceforge.net/projects/xming)
SSH (available from www.yale.edu/software)
Useful if you don’t want to have Matlab take up
memory/processing power on you computer
15
Aside: You can run Matlab remotely!
Instructions:
Start Xming (it will run in the background)
Log in to eli.yale.edu through SSH
In SSH click on Edit, then click Settings, then click
Tunneling
Check the box at the bottom that says “Tunnel X11
Connections” and click OK
You’re all set! Type “matlab &” in SSH to run the
program
Note that this is an older version of matlab
16
Functions:
MATLAB provides a large number of standard
elementary mathematical functions, including abs,
sqrt, exp, sin.
For a list of the elementary mathematical functions,
type:
help elfun
For a list of more advanced mathematical and matrix
functions, type
help specfun
help elmat
17
Programming with MATLAB:
Files that contain code in the MATLAB
language are called M-files. You can create Mfiles using the matlab editor, then use them as
you would any other MATLAB functions or
commands. There are two types of M-files:
Scripts and Functions.
18
Scripts
Scripts: a bunch of code grouped together; doesn’t
accept argument or return output.
You should do most of your work using scripts!
Example:
open m-file editor
type disp(‘Hello’)
type disp(‘World’)
save as test.m in your current directory
type “test” in Matlab
19
Functions:
Functions are M-files that can accept input
arguments and return output arguments. The
name of the M-file and of the function should
be the same.
For example, save this as area.m:
function ar = area(radius)
ar=pi*radius^2;
20
Flow Control:
MATLAB has following flow controls:
If statement
For loops
While loops
21
if … elseif … else … end
if A > B
'greater'
elseif A < B
'less'
else
'equal'
end
22
for … end
beta=0.925;
a=1:100000;
for i = 1:100000
betavec(i)=beta^a(i);
end
betavec
But you should avoid for loops if possible
newbetavec=beta.^a
23
Graphics: plot
x = [0 : .01 : 2*pi];
y = sin(x);
plot(x,y)
y2 = sin(x-.25)
y3 = sin(x-.5)
plot(x,y,x,y2,x,y3)
24
Cool Graphs!
[x,y] = meshgrid([-2:.2:2]);
Z = x.*exp(-x.^2-y.^2);
surf(x,y,Z,gradient(Z))
25
Learn from others
There are lots of Matlab functions already out
there:
Google them!
http://www.mathworks.com/matlabcentral/
James LeSage’s econometrics toolbox:
http://www.spatial-econometrics.com/
26