Week 12: Matlab/Octave for Engineering Applications - Part 1 BJ Furman 06NOV2009 The Plan for Today Matlab/Octavefor engineering applications Overview of Matlab and Octave Matlab environment Octave command line Basic operations Script files Resources for more information Learning Objectives Explain what Matlab and Octave (M/O) are Navigate the Matlab user interface and Octave command line Use M/O as a scratch pad Create script files to execute a sequence of commands Matlab and Octave ‘High-level’ languages abstracts away the nitty-gritty details Numerical calculation oriented toward engineering, mathematical, and scientific computing Powerful graphics and analysis tools Matlab (Matrix Laboratory) Controls, signal processing, data analysis, and other specialized ‘toolboxes’ are available Widely used in industry Matlab is commercial software (a student version is available) http://www.mathworks.com/ Octave is open source and free: http://www.gnu.org/software/octave/ Matlab (ver. 6.5) Environment (GUI) Workspace Window Command Window Current Directory Window Octave Command Line Matlab as a ‘scratch pad’ dynamically typed Octave as a ‘scratch pad’ dynamically typed Matlab/Octave Basics - 1 Fundamental element: the array even scalars --> stored as 1x1 array of double floating point value How many bytes? See Workspace Window Useful commands (see documentation for more details!) who (information about what is in memory) whos (more detailed information about what is in memory) clc (clears the command window) clear (clears all user defined variables) help name (provides information on command name) Matlab/Octave Basics - 2 Script files Text files containing Matlab/Octave statements Type in a text editor (Matlab or Octave) or use M-file editor in Matlab (File/New/M-File or Ctrl-n) Comments Matlab: % Octave: % or # Example: a cool plot Script File Example Plot of z ( x, y ) = e −0.5[ x 2 + 0.5( x − y ) 2 ] over − 4 ≤ x, y ≤ 4 Octave Script File Example cool_plot.m needs to be in the ‘load path’ • Octave: addpath(dir-path) addpath('C:\Octave\Octave_code') • Matlab: File/Set Path Plot of z ( x, y ) = e −0.5[ x 2 + 0.5( x − y ) 2 ] over − 4 ≤ x, y ≤ 4 Matlab Script File Example Ch Example function exp(-0.5(x*x+0.5(x-y)(x-y))) z 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 4 3 2 y 1 0 -1 -2 -1 -3 -4 -4 See: chap. 23 of The Ch Language User's Guide and chap. 2 of The Ch Language Environment Reference -3 0 1 2 3 4 x -2 This plot is generated by Ch Student Edition Excel Example Matlab/Octave Basics - 3 Array creation A=[1 2 3; 4,5,6; 7 8 9] size(A) Indexing elements Separate elements by spaces or commas and rows by semicolon or CR A(1,2) Index by enclosing indices in ( ) Contrast how elements are indexed in C Adding elements A(4,2)=13 Contrast with what would happen in C Matlab/Octave Basics - 4 Vectors (just 1xn or nx1 arrays) Vectors using the colon operator C=1:0.25:2 D=0:5 Linspace B=[sin(pi/4), -2^3, size(A,1)] E=linspace(0,5,3) Logspace F=logspace(1,3,5) base : increment : limit For an increment of 1: base : limit start : end : n For n elements linearly spaced between start and end start : end : n For n elements logarithmically spaced between start and end Matlab/Octave Basics - 5 Manipulating arrays Add a row A=[A; [10 11 12]] Extract a sub-array G=A(1:3,1:2) Colon operator by itself means, “all the elements” Can apply to the dimensions of the arrays to access all the elements of a row or column Can apply to the entire array to get all the elements as a column vector What order will they come out? Examples H=[1:4; linspace(10,40,4); 400:-100:100] I=H([1 2], :) What will this produce? J=H(:) Matlab/Octave Basics - 6 Special matrices zeros, ones, eye K=zeros(3) L=ones(4) M=eye(3) Matlab/Octave Basics - 7 Matrix operations Arithmetic just like with scalars! (But need to take care that dimensions agree) N=L*7 O=N+eye(4) What will this produce? P=B*E P=B*E’ What will this produce? What does the ‘ do? Q=A+B Q=B+E [1 2]*[3 ; 4] What will this produce? Matlab/Octave Basics - 8 Matrix operations, cont. Matrix division Recall last week’s circuit analysis R1=10k R2=R3=5k V=10V ⎡1 ⎢0 ⎢ ⎢⎣0 Matrix solution Ri = V ⇒ i = R −1V use ' left' division to solve for i i=R\V ‘right’ division ( i = R / V ) solves iR=V --> i = VR-1 −1 − 1⎤ ⎡ i1 ⎤ ⎡ 0 ⎤ 0 R1 ⎥ ⎢i2 ⎥ = ⎢V ⎥ ⎥⎢ ⎥ ⎢ ⎥ (R2 + R3 ) 0 ⎥⎦ ⎢⎣i3 ⎥⎦ ⎢⎣V ⎥⎦ R i V Matlab/Octave Basics - 9 Matrix operations, cont. Element-by-element operations (use . (dot) and the arithmetic operator) dot product of two vectors r r ˆ ˆ ˆ v1 = 3i + 2 j − 5k v2 = 2iˆ − 4 ˆj + 10kˆ r r what is v1 • v2? r r v1 • v2 = (3)( 2) + ( 2)( −4) + ( −5)(10) = 6 − 8 − 50 = −52 in Matlab/Octave : v1 = [3 2 − 5] v 2 = [2 − 4 10] sum( v1 . * v 2) Matlab/Octave Basics - 10 Functions Like script M-files, but several differences: first line must be of the form: function [output args] = function_name(input args) variables generated in the function are local to the function, whereas for script files, variables are global Make sure you add comments at the start that describe what the function does Example: root-mean-square function Review References Matlab. (2009, November 6). In Wikipedia, the free encyclopedia. Retrieved November 6, 2009, from http://en.wikipedia.org/wiki/Matlab GNU Octave. (2009, October 31). In Wikipedia, the free encyclopedia. Retrieved November 6, 2009, from http://en.wikipedia.org/wiki/GNU_Octave Matlab tutorials: http://www.mathworks.com/academia/student_center/tutorials/la unchpad.html Octave tutorial: http://homepages.nyu.edu/~kpl2/dsts6/octaveTutorial.html ftp://www.chabotcollege.edu/faculty/bmayer/ChabotEngi neeringCourses/ENGR-25.htm