StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
-
Upload
justin-norford -
Category
Documents
-
view
217 -
download
1
Transcript of StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Introduction to MatlabStatLab Workshop
Yale University
Maximiliano Appendino, Economics
October 18th, 2013
What is Matlab?Matlab is a matrix-based tool for numerical
computationsPowerful Easy to use
Programming language Interactive environmentLots of available toolboxes
Getting HelpUseful links:
http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp
Mathworks' Getting Started Matlab CenterKermit Sigmon’s Matlab PrimerMany others
Google your questionMatlab’s help
OnlineStatlab Consultants: Max Perez Leon,
Zhentao Shi
Acquiring MatlabITS Software Library & Resources:
http://www.yale.edu/its/software/Free for students: Matlab R2013aAvailable 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 MatlabDouble-click the “MATLAB R2013a” icon on
the desktopOr click the start bottom, type MATLAB and
enterUsual Interface:
Command WindowWorkplace
.MAT filesCommand historyCurrent Folder
InterfaceCan 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 MatricesEntered 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‘ transposeElement-by-element: preceded the operators
by .Subscripts:
>> B = A(2,3)
Matrix OperationMatrix 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 FunctionsZeros:
>> zeros(3,3)Ones:
>> ones(3,3)Identity:
>> eye(3)More on matrices and linear algebra:
>> help elmat>> help matfun
Random MatricesPseudo-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 manipulationConcatenation:
>> 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 OutputIf 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
FunctionsMatlab 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
GraphicsMatlab 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
GraphicsAlso 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)
ProgrammingM-files contain Matlab code.m is their extensionMatlab editorCan be used as any command or function as
long as they are in the “Current Folder”Two types:
ScriptsFunctions
ScriptsA list of code grouped together
It does not accept argument or return outputUse them to register your workExample:
File New Scriptdisp(‘Hello’)File Save test.m>> test
FunctionsFunctions are M-files that can accept input
arguments and return output arguments. The M-file and the function should have the
same nameExample:
function ar = area(radius)ar = pi*radius.^2;File Save area.m>> area(2)
Flow ControlMatlab has the standard flow controls
If statements
For loops
While loops
If statementa = 10;b = 11;
if a > bdisp('greater‘)
elseif a < bdisp('less‘)
elsedisp('equal‘)
end
>> [a==b]
For loopsbetavec = zeros(100,1);beta = 0.925;a = [1:100];for i = 1:100
betavec(i)=beta^a(i);endplot(betavec)But you should avoid for loops if possible:
>> newbetavec=beta.^a
While loopsThe 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;
endplot(betavec)
Solving problemsCheck if somebody else has already solved it
Matlab itselfAnybody using Matlab
Solve it by yourselfUse Matlab’s matrix processing capacity as
much as you canBe organized with your code
Thank you!
Questions, Comments,
Problems to solve?