31.07.2015 Views

Basic Concepts in Matlab, by M. G. Kay

Basic Concepts in Matlab, by M. G. Kay

Basic Concepts in Matlab, by M. G. Kay

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FUNCTIONS AND SCRIPTS1 2 3 4 5sum(a)(Array summation)ans =15cumsum(a)(Cumulative summation)ans =1 3 6 10 15By default, <strong>Matlab</strong> is column oriented; thus, for matrices,summation is performed along each column (the 1stdimension) unless summation along each row (the 2nddimension) is explicitly specified:AA =1 3 45 7 8sum(A)ans =6 10 12sum(A,2)(Sum along rows)ans =820sum(A,1)(Sum along columns)ans =6 10 12sum(sum(A))(Sum entire matrix)ans =28Forc<strong>in</strong>g column summation: Even if the default columnsummation is desired, it is useful (<strong>in</strong> programm<strong>in</strong>g mode) toexplicitly specify this <strong>in</strong> case the matrix has only a s<strong>in</strong>glerow, <strong>in</strong> which case it would be treated as a vector and sumto a s<strong>in</strong>gle scalar value (an error):A = A(1,:) (Convert A to s<strong>in</strong>gle-row matrix)A =1 3 4sum(A)(Incorrect)ans =8sum(A, 1)(Correct)ans =1 3 48. Functions and ScriptsFunctions and scripts <strong>in</strong> <strong>Matlab</strong> are just text files with a .mextension. User-def<strong>in</strong>ed functions can be used to extend thecapabilities of <strong>Matlab</strong> beyond its basic functions. A userdef<strong>in</strong>edfunction is treated just like any other function.Scripts are sequences of expressions that are automaticallyexecuted <strong>in</strong>stead of be<strong>in</strong>g manually executed one at a time atthe command-l<strong>in</strong>e. A scripts uses variables <strong>in</strong> the (base)workspace, while each function has its own (local)workspace for its variables that is isolated from otherworkspaces. Functions communicate only through their<strong>in</strong>put and output arguments. A function is dist<strong>in</strong>guishedfrom a script <strong>by</strong> plac<strong>in</strong>g the keyword function as the firstterm of the first l<strong>in</strong>e <strong>in</strong> the file.Although develop<strong>in</strong>g a set of functions to solve a particularproblem is at the heart of us<strong>in</strong>g <strong>Matlab</strong> <strong>in</strong> the programm<strong>in</strong>gmode, the easiest way to create a function is to do it <strong>in</strong> an<strong>in</strong>cremental, calculator mode <strong>by</strong> writ<strong>in</strong>g each l<strong>in</strong>e at thecommand-l<strong>in</strong>e, execut<strong>in</strong>g it, and, if it works, copy<strong>in</strong>g it tothe function’s text file.Example: Given a 2-D po<strong>in</strong>t x and m other 2-D po<strong>in</strong>ts <strong>in</strong>P, create a function mydist.m to determ<strong>in</strong>e the Euclidean(i.e., straight-l<strong>in</strong>e) distance d from x to each of the m po<strong>in</strong>ts<strong>in</strong> P:⎡ p1,1 p1,2⎤x = [ x ] = ⎢ ⎥1 x2, P ⎢ ⎥⎣pm,1 pm,2⎦⎡2 2 ⎤⎢ ( x1− p1,1) + ( x2 − p1,2)⎥d = ⎢⎥⎢2 2 ⎥⎢⎣( x1 − pm,1) + ( x2 − pm,2)⎥⎦The best way to start is to create some example data forwhich you know the correct answer:6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!