Demo of Basic Matlab Features o o o

Demo of Basic Matlab Features o o o Getting Around Matlab How to do matrix calculation How to draw graphs How to read data How to calculate statistics A very brief look at optimization in Matlab

Matlab’s User Interface o Command window n Only >> line is active, the remainder of the screen is only a reminder of previous commands n NB Matlab does not manipulate symbols; only numbers o Command history window n Double click on command to repeat previous command o Current Directory n Where to up-load files into Matlab (use load command to bring in variables from disk) n Toggle to workspace giving list of vectors, matrices created (this window more useful for our class than the current directory window)

Demo of Basic Matlab Features o o o Getting Around Matlab how to do matrix calculation how to draw graphs how to read data how to calculate statistics
![Creating A Matrix A = [1 2 0; 2 5 -1; 4 10 -1] Creating A Matrix A = [1 2 0; 2 5 -1; 4 10 -1]](http://slidetodoc.com/presentation_image_h2/514fcf7fafc6744278ab5d70e480feff/image-4.jpg)
Creating A Matrix A = [1 2 0; 2 5 -1; 4 10 -1] semicolon starts new row B = [3, 4, 5] create row vector: use “_” (space) or “, ” (comma) to separate elements C = [A; B] put column vector B as new row in matrix 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 1 s clear C, D, E erases matrix (can also over-write)

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

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

Demo of Basic Matlab Features o o o Getting Around Matlab how to do matrix calculation how to draw graphs how to read data how to calculate statistics

Drawing a graph – a traditional way x = [-10 : . 1 : 10]; create row vector from -10 to +10 in increments of 1/10 If do not want an echo of your input, use “; ” at end of command y = 3*x. ^2 – 5*x + 6; plot(x, y) grid on displays graph of x and y displays reference lines
![3 Dimension Graphs [x, y] = meshgrid(-3: . 02: 3, -5: . 02: 5); 3 Dimension Graphs [x, y] = meshgrid(-3: . 02: 3, -5: . 02: 5);](http://slidetodoc.com/presentation_image_h2/514fcf7fafc6744278ab5d70e480feff/image-9.jpg)
3 Dimension Graphs [x, y] = meshgrid(-3: . 02: 3, -5: . 02: 5); Creates matrix of x between -3 & 3 and y between -5 & 5 in. 02 increments to graph z in 3 rd dimension z = x. *y. ^2 plot 3(x, y, z) create function of x and y To change the perspective use the “Rotate 3 D” icon on the menu
![A graph with a message !! o [x, y] = meshgrid(-3: . 02: 3, A graph with a message !! o [x, y] = meshgrid(-3: . 02: 3,](http://slidetodoc.com/presentation_image_h2/514fcf7fafc6744278ab5d70e480feff/image-10.jpg)
A graph with a message !! o [x, y] = meshgrid(-3: . 02: 3, -5: . 02: 5); o z = max(0. 0003, min(. 0004, exp(3. *(((0. 5+x). ^2)+(y. ^2)/2)) + exp((-x. ^2 -(y. ^2)/2)). * cos(4. *x) ) ); o plot 3(x, y, z) To change the perspective use the “Rotate 3 D” icon on the menu o http: //www. npr. org/blogs/krulwich/2012/01/10/144991 340/don-t-make-me-do-this-the-equations-screamed

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

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

Demo of Basic Matlab Features o o o Getting around Matlab how to do matrix calculation how to draw graphs how to read data how to calculate statistics

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

Reading a data file – an interactive mode Choose “import data” from file menu. Select file “beta” to import from Format short g X = beta(1: end, 1) Y = beta(1: end, 2) (so data looks non-zero) declare X is beta’s 1 st column 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)
![Regression – Manual Method n = size(X, 1); X = [ones(n, 1), X]; puts Regression – Manual Method n = size(X, 1); X = [ones(n, 1), X]; puts](http://slidetodoc.com/presentation_image_h2/514fcf7fafc6744278ab5d70e480feff/image-16.jpg)
Regression – Manual Method n = size(X, 1); X = [ones(n, 1), X]; puts new col of 1 s as 1 st col in new X matrix slop = inv(X’*X)*X’*Y calculates slope eps = Y – X*slop; calculates error from line sigma = eps’*eps/n; slop_var = sigma*inv(X’*X) covariance matrix

Demo of Basic Matlab Features o o o Getting Around Matlab how to do matrix calculation how to draw graphs how to read data how to calculate statistics

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
- Slides: 18