26
Introduction to Matlab StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th , 2013

Introduction to Matlab

  • Upload
    floyd

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to Matlab. StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th , 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to  Matlab

Introduction to MatlabStatLab Workshop

Yale UniversityMaximiliano Appendino, Economics

October 18th, 2013

Page 2: Introduction to  Matlab

What is Matlab?Matlab is a matrix-based tool for numerical

computationsPowerful Easy to use

Programming language Interactive environmentLots of available toolboxes

Page 3: Introduction to  Matlab

Getting HelpUseful links:

http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jspMathworks' Getting Started Matlab CenterKermit Sigmon’s Matlab PrimerMany others

Google your questionMatlab’s help

OnlineStatlab Consultants: Max Perez Leon,

Zhentao Shi

Page 4: Introduction to  Matlab

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

Page 5: Introduction to  Matlab

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

Page 6: Introduction to  Matlab

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

Page 7: Introduction to  Matlab

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…

Page 8: Introduction to  Matlab

Matrix Operation+ addition- subtraction* multiplication^ power‘ transposeElement-by-element: preceded the operators

by .Subscripts:

>> B = A(2,3)

Page 9: Introduction to  Matlab

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]

Page 10: Introduction to  Matlab

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]

Page 11: Introduction to  Matlab

Matrix Generation FunctionsZeros:

>> zeros(3,3)Ones:

>> ones(3,3)Identity:

>> eye(3)More on matrices and linear algebra:

>> help elmat>> help matfun

Page 12: Introduction to  Matlab

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)

Page 13: Introduction to  Matlab

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)

Page 14: Introduction to  Matlab

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

Page 15: Introduction to  Matlab

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

Page 16: Introduction to  Matlab

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

Page 17: Introduction to  Matlab

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)

Page 18: Introduction to  Matlab

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

Page 19: Introduction to  Matlab

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

Page 20: Introduction to  Matlab

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)

Page 21: Introduction to  Matlab

Flow ControlMatlab has the standard flow controls

If statements

For loops

While loops

Page 22: Introduction to  Matlab

If statementa = 10;b = 11;

if a > bdisp('greater‘)

elseif a < bdisp('less‘)

elsedisp('equal‘)

end

>> [a==b]

Page 23: Introduction to  Matlab

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

Page 24: Introduction to  Matlab

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)

Page 25: Introduction to  Matlab

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

Page 26: Introduction to  Matlab

Thank you!

Questions, Comments,

Problems to solve?