So, You*re Going to Write an Empirical Senior Thesis

advertisement
StatLab Workshop
Yale University
Maximiliano Appendino, Economics
October 18th, 2013
What is Matlab?
 Matlab is a matrix-based tool for numerical
computations
 Powerful
 Easy to use
 Programming language
 Interactive environment
 Lots of available toolboxes
Getting Help
 Useful links: http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp
 Mathworks' Getting Started
 Matlab Center
 Kermit Sigmon’s Matlab Primer
 Many others
 Google your question
 Matlab’s help
 Online
 Statlab Consultants: Max Perez Leon, Zhentao Shi
Acquiring Matlab
 ITS Software Library & Resources:
 http://www.yale.edu/its/software/
 Free for students: Matlab R2013a
 Available in Statlab locations


The Center for Science and Social Science Information
 219 Prospect St, Basement
 Kline Biology Tower
Rosenkranz Hall
 115 Prospect St, Room 01
Launching Matlab
 Double-click the “MATLAB R2013a” icon on the
desktop
 Or click the start bottom, type MATLAB and enter
 Usual Interface:
 Command Window
 Workplace

.MAT files
 Command history
 Current Folder
Interface
 Can be used as a calculator
 “help” gives you a list of all topics
 “help topic” informs you about the particular topic
 Example:
>> help
>> help stats
>> help normcdf
Entering Matrices
 Entered manually:
>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
 Generate it by built-in functions

Loaded from a file

.MAT or Menu File  Import Data…
Matrix Operation
 + addition
 - subtraction
 * multiplication
 ^ power
 ‘ transpose
 Element-by-element: preceded the operators by .
 Subscripts:
>> B = A(2,3)
Matrix Operation
 Matrix Multiplication
>> [1 2; 3 4]*[1 2; 3 4]
[7 10; 15 22]
 Element-by-element Multiplication
>> [1 2; 3 4].*[1 2; 3 4]
[1 4;9 16]
The Colon Operator :
 One of Matlab’s most important operators:
 Portions of a matrix:
>> C = A(:,3)
>> D = A(1,:)
>> E = A(1:2,1:3)

Create matrices using increments:
>> F = [1:0.1:1.5]’
>> G = [1:0.1:1.5; 1:0.5:3.5]
Matrix Generation Functions
 Zeros:
>> zeros(3,3)
 Ones:
>> ones(3,3)
 Identity:
>> eye(3)
 More on matrices and linear algebra:
>> help elmat
>> help matfun
Random Matrices
 Pseudo-Random numbers:
 Change the seed:
>> rng('shuffle')
>> rng(15)
 U[0,1] random variable:
>> RU = rand(3,4)
 Normal random variable:
>> RN = randn(4,3)
Matrix manipulation
 Concatenation:
>> A2 = [A A.^2; A./2 A]
 Deleting rows and columns:
>> A2(:,7:8) = []
 Adding rows and columns:
>> A2 = [1:2:11;A2]
 Knowing the size:
>> sizeA2 = size(A2)
Suppressing Output
 If you simply type a statement and press Enter Matlab
automatically displays the results on the Command
Window
 If you end the line with a semicolon
; Matlab performs
the computation but does not display any result
>> H = rand(2,2)
>> H = rand(2,2);
>> H
Functions
 Matlab provides a large number of standard
elementary mathematical functions:
>> abs(-10)
>> sqrt(9)
>> x = [0:0.1:2*pi];
>> y = sin(y);
 Look for the ones you need:
 Google
>> help
Graphics
 Matlab generate 2-dimensional plots easily:
>> plot(x,y)
>> y2 = y + 0.25;
>> y3 = y + 0.50;
>> plot(x,y,x,y2,x,y3)
 With a friendly interface to edit them
Graphics
 Also 3-dimensional ones:
 First we need to generate a grid:
>> [X,Y] = meshgrid([-2:.2:2]);
 Second we can calculate the function to graph:
>> Z = X.*exp(-X.^2-Y.^2);
 Finally the graph:
>> surf(X,Y,Z)
Programming
 M-files contain Matlab code
 .m is their extension
 Matlab editor
 Can be used as any command or function as long as
they are in the “Current Folder”
 Two types:
 Scripts
 Functions
Scripts
 A list of code grouped together
 It does not accept argument or return output
 Use them to register your work
 Example:
File  New  Script
disp(‘Hello’)
File  Save  test.m
>> test
Functions
 Functions are M-files that can accept input arguments
and return output arguments.
 The M-file and the function should have the same
name
 Example:
function ar = area(radius)
ar = pi*radius.^2;
File  Save  area.m
>> area(2)
Flow Control
 Matlab has the standard flow controls
 If statements
 For loops
 While loops
If statement
a = 10;
b = 11;
if a > b
disp('greater‘)
elseif a < b
disp('less‘)
else
disp('equal‘)
end
>> [a==b]
For loops
betavec = zeros(100,1);
beta = 0.925;
a = [1:100];
for i = 1:100
betavec(i)=beta^a(i);
end
plot(betavec)
 But you should avoid for loops if possible:
>> newbetavec=beta.^a
While loops
 The previous example would be:
betavec = zeros(100,1);
beta = 0.925;
a = [1:100];
i = 1;
while i < 101
betavec(i)=beta^a(i);
i = i + 1;
end
plot(betavec)
Solving problems
 Check if somebody else has already solved it
 Matlab itself
 Anybody using Matlab
 Solve it by yourself
 Use Matlab’s matrix processing capacity as much as you
can
 Be organized with your code
Thank you!
Questions,
Comments,
Problems to solve?
Download