Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)

Similar presentations


Presentation on theme: "Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)"— Presentation transcript:

1 Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)

2 Interface with Mathlab 2012  Command window Only >> line is active, the other lines only remind which previous commands you used NB Matlab does not manipulate symbols; only numbers  Command history window Double click on command to repeat previous command Single click to copy into command window and then modify  Workspace: list of created vectors & matrices  Current Folder Where to up-load files into Matlab (use load command to bring in variables from disk)

3 Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)

4 Creating A Matrix A = [1 2 0; 2 5 -1; 4 10 -1]semicolon starts new row B = [3,4,5]create row vector: “ ” (space) or “,” (comma) separates elements C = [A;B]put row vector B as new row in A D = [A,B’]put new column B in matrix A B is now transposed (single quote) E = eye(5)create 5 x 5 identity matrix F = ones(2,3)create 2 row x 3 col matrix of 1s clear C, D, Eerases matrix (or you can over-write it)

5 Matrix Operations B = A’transpose matrix A C = A + 3 * B D = A * B E = A.* Belement by element multiplication F = A^2multiply matrix A by itself G = A.^2raise each A element to 2 nd power X = inv(A)form inverse of matrix format(controls format of numeric values) format short g

6 Useful Matrix Functions det(A)gives determinant rank(A)gives rank trace(A)gives trace eig(A)gives eigenvalues For complete list, see help -> matlab -> mathematics -> matrices and linear algebra -> function summary

7 Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)

8 Drawing graph (traditional method) x = [-10 : 0.1 : 10];create row vector from -10 to +10 in increments of 1/10 To avoid an input echo, use “;” at end of command y = 3*x.^2 – 5*x + 6; plot(x,y)displays graph of x and y grid ondisplays reference lines

9 Graphs in 3 Dimensions [x, y] = meshgrid(-3:0.02:3, -5:0.02:5); Creates matrix of x between -3 & 3 and y between -5 & 5 in 0.02 increments to graph z in 3 rd dimension z = x.*y.^2create function of x and y plot3(x,y,z) To change the perspective use the “Rotate 3D” icon on the menu

10 A graph with a message !!  [x, y] = meshgrid(-3:.02:3, -5:.02:5);  z = max(0.0003, min(.0004, exp(- 3.*(((0.5+x).^2)+(y.^2)/2)) + exp((-x.^2-(y.^2)/2)).* cos(4.*x) ) );  plot3(x,y,z) To change the perspective use the “Rotate 3D” icon on the menu  http://www.npr.org/blogs/krulwich/2012/01/10/144991 340/don-t-make-me-do-this-the-equations-screamed

11 Other Example Graphs T=R.^2+R.*S+2.*S.^2+3;a bowl plot3(R,S,T) T=-R.^2-S.^2+6.*R+2.*S;a dome plot3(R,S,T) T=R.^3-R.*S+S.^3a beach towel on a hill

12 Interactive Finishing Touches  use “plottools”  Add labels, titles, change perspective, etc.  see help -> matlab -> graphics -> plots and plotting tools -> “interactive plot creation with plot tools demo”

13 Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)

14 Calculating Statistics – a traditional way Z = beta(1:end,3); mean(Z) median(Z) min(Z) max(Z) std(Z)standard deviation cov(Z) hist(Z,20)draws histogram with 20 categories

15 Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)

16 Working with files – a traditional way Cd(‘w:\’)change directory to W disk The file named col2row.prn was saved in excel file using “save as” menu with file delimited by spaces It contains three rows of numbers: 1 2 4 -3 4 1 F = load(‘col2row.prn');looks for spaces or columns Or G = dlmread(‘col2row.prn’,’\t’);the “\t” looks for tab- delimited

17 Reading a data file – an interactive mode Choose “import data” from file menu. Select file “beta” to import from Format short g (so data looks non-zero) X = beta(1:end,1)declare X is beta’s 1 st column Y = beta(1:end,2)declare Y as beta’s 2 nd column Scatter(X,Y)shows X vs. Y Regress(Y,X)best-fit slope (Y ind, X dep)

18 Regression – Manual Method n = size(X,1); X = [ones(n,1),X];puts new col of 1s as 1 st col in new X matrix slop = inv(X’*X)*X’*Ycalculates slope eps = Y – X*slop;calculates error from line sigma = eps’*eps/n; slop_var = sigma*inv(X’*X)covariance matrix


Download ppt "Matlab Introduction  Getting Around Matlab  Matrix Operations  Drawing Graphs  Calculating Statistics  (How to read data)"

Similar presentations


Ads by Google