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