Download presentation

Presentation is loading. Please wait.

Published byJustin Norford Modified over 2 years ago

1
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013

2
What is Matlab? Matlab is a matrix-based tool for numerical computations Powerful Easy to use Programming language Interactive environment Lots of available toolboxes

3
Getting Help Useful links: http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp 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

4
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

5
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

6
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

7
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…

8
Matrix Operation + addition - subtraction * multiplication ^ power ‘ transpose Element-by-element : preceded the operators by. Subscripts: >> B = A(2,3)

9
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]

10
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]

11
Matrix Generation Functions Zeros: >> zeros(3,3) Ones: >> ones(3,3) Identity: >> eye(3) More on matrices and linear algebra: >> help elmat >> help matfun

12
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)

13
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)

14
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

15
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

16
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

17
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)

18
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

19
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

20
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)

21
Flow Control Matlab has the standard flow controls If statements For loops While loops

22
If statement a = 10; b = 11; if a > b disp('greater‘) elseif a < b disp('less‘) else disp('equal‘) end >> [a==b]

23
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

24
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)

25
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

26
Thank you! Questions, Comments, Problems to solve?

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google