Unformatted text preview:

31 October 2002Matlab, IntroductionResources: intro1. Matlab Primer: short and clear introduction.2. Matlab on Athena (MIT computer services web page). 3. www.mathworks.com, Matlab: detailed online documentation.medium level4. Matlab Guide D.J.Higan,N.J.Higan, SIAM, 2000.Intro to Matlab 6.0.more advanced5. Numerical Methods with Matlab, G. Recktenwald, Prentice Hall.6. Mastering Matlab 6, D.Hanselman, B.Littlefield,Prentice Hall 2001, comprehensive tutorial & reference.also7. Lecture notes: 10.001 web page.31 October 2002Main Features of Matlab• Matlab = matrix laboratory, matrix oriented programminglanguage + desktop environment.• Any variable is an array by default, thus almost nodeclarations. All variables are by default double.• Translator - interpreter: line after line, no exe files.• High level language:(i) quick and easy coding (ii) tools assembled into toolboxes (Spectral Analysis, ImageProcessing, Signal Processing, Symbolic Math etc.) (iii) relatively slow.• All Matlab functions are precompiled.• One may add extra functions by creating M-files and modifythe existing ones.31 October 2002Comparison with C.• Syntax is similar• Language structure is similar to C:– MATLAB supports variables, arrays,structures, subroutines, files– MATLAB does NOT support pointers and doesnot require variable declarations– MATLAB has extra built-in tools to work withmatrices/arrays31 October 2002Matlab, Getting Started1. Accessing Matlab on Athena: add matlab matlab &2. Log out: quit or exitMATLAB desktop (version 6):1) Command window2) Launch Pad / Workspace window3) Command history / Current Directory window31 October 2002Useful Hints & Commands• input: variable_name Å output: variable_value• semicolon at the end will suppress the output• command history: upper & lower arrows, also command name guess:(i) type abc (ii) hit “upper arrow” key Å get the last commandstarting from abc• format compact - no blank lines in the outputformat loose - back to default• help commandname - info on commmandname31 October 2002Workspace Maintenance• all the assigned variables “reside” in the workspace• clear all - clears all the memory (workspace) clear xyz - removes xyz from the memory• who - lists all the variables from the workspace• whos - also gives the details>> whoYour variables are:ans c1 c2>> whosName Size Bytes Classans 1x1 8 double arrayc1 1x1 16 double array(complex)c2 2x2 64 double array(complex)31 October 2002Workspace Maintenance• save saves all workspace variables on disk in filematlab.mat• save filename x y z - x, y, z are saved infile filename.mat• load filename - loads contents of thefilename.mat to the workspace• load filename x y z - loads only x, y, zfrom filename.mat to the workspace• Each array requires a continuous chunk ofmemory; use pack for memory defragmentation.31 October 2002Built in Constants & Functions• pi – π number• i & j stand for “imaginary one” (i = (-1)1/2), however may be redefined• Trigonometric: sin, cos, tan, sec, cot• Inverse trig.: asin, acos, atan, asec, acot• Exponential: log, log2, log10, exp• Complex: abs – abs. value, angle – phase angle,conj – conjugate transpose, imag – imaginary andreal- real part31 October 2002Linear AlgebraVector: an ordered set ofreal or complex numbersarranged in a row orcolumn.=mxxxxM21[]nyyyy L21=m-elementcolumn vector (n x 1)n-element row-vector (1 x n)31 October 2002Vector OperationsAddition/subtraction (element-wise, array operation):c = a + b Å ci = ai + bi, i = 1,…,nd = a - b Å ci = ai - bi, i = 1,…,nMultiplication/division by a scalar:b = α a Å bi = α aib = a/α Å bi = ai/αVector transpose, turns row into column and vise versa:x = [1, 2, 3] Å xT =321xxx(xT)T = x31 October 2002Vector OperationsVector inner (dot, scalar) product(vector/matrix operation):a = x . y Åx is a row vectory is a column vectorThe dimensions of x and y must agree.∑==niiiyxa1a = yT . xTNB General rule for vector/matrix multiplication:“row times column” – take i-th row of the left multiplier andmultiply by the j-th column of the right multiplier31 October 2002Vector OperationsOuter (tensor) product:[]===4434241443332313423222124131211143214321xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxxxxyyyyyxMMij = xiyj31 October 2002Vector NormsTo compare two vectors a vector norm (analogous tolength, size) is introduced:||x||>||y|| Å “norm of x is greater than norm of y”Euclidian norm, length in nD (also called L2 norm):||x||2 = (x12 + x22 + x32 + … + xn2)1/2||x||1 = |x1| + |x2| + |x3| + … + |xn| (L1)||x||inf. = max(|x1|, |x2|, |x3|, … |xn|)||x||p = (x1p + x2p + x3p + … + xnp)1/p(Lp)31 October 2002Dealing with Vectors/Matrices Entering matrices by explicit list of elements:A =[1 2 3]A= 1 2 3A = [1; 2; 3]A= 1 2 3A = [1 2 3; 4 5 6; 7 8 9]orA=[1 2 3 4 5 6 7 8 9]Spaces separate the elements,semicolons and “new line” symbols separate the rows.31 October 2002Dealing with MatricesComplex matrices:either A=[1 2; 3 4]+i*[5 6; 7 8]or A=[1+5i 2+6i; 3+7i 4+8i]No blank spaces, i or j stands for “imaginary one”.Matrix and array operations, classification.+-*^‘ conjugate transpose\ left division/ right division}element-wise (array operations)}array or matrix operationsonly matrix operations31 October 2002Matrix MultiplicationProduct of the two matrices A(nxm) and B(mxl)is matrix C(nxl):jmimjijiijBABABAC22211...+++=The dimensions of A and B must agree.Cij – is a dot product of i-th row of A and j-th column of B.Again “row (on the left) times column (on the right)”.If A or B is a vector, we will have either“row vector times matrix = column vector” or“matrix times column vector = row vector”31 October 2002Dealing with MatricesIn Matlab left and right division are inverse to multiplication bycolumn and row vectors correspondingly:A * x=b Å x=A \ b (left) A-matrix m x n, b -row vector 1 x nx * A=b Å x = b / A (right) b - column vector m x 1Conjugate transpose: swaps the indices and changes the sign ofimaginary part of each element.C = A’C(i,j) = real( A(j,i) ) - i * imag( A(j,i) )31 October 2002Dealing with Matrices, Examples>> C = A + B;C(k,l) =


View Full Document

MIT 10 001 - Matlab Introduction

Download Matlab Introduction
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Matlab Introduction and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Matlab Introduction 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?