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.

EXAMPLE: THE MONTI CARLO METHOD1 0 0 1 0a(ispos)ans =5 9idxpos = f<strong>in</strong>d(a > 0)(Index array)idxpos =1 4a(idxpos)ans =5 9Some functions return logical or <strong>in</strong>dex arrays:s = {'Miami','Detroit','Boston'};idxDetroit = strmatch('Detroit',s)idxDetroit =2isDetroit = strcmpi('detroit',s)isDetroit =0 1 0Although similar, the use of logical and <strong>in</strong>dex arrays havedifferent advantages:Advantages of Logical Arrays1. Direct address<strong>in</strong>g: It is easy to determ<strong>in</strong>e if <strong>in</strong>dividualelements of the target array satisfy the logicalexpression; e.g., a value of 1 (true) for ispos(4)directly determ<strong>in</strong>es that a(4) is positive, while itwould be necessary to search through each element ofidxpos to determ<strong>in</strong>e if 4 is an element of the array(i.e., any(idxpos == 4)).2. Use of logical vs. set operators: When compar<strong>in</strong>g multiplelogical arrays, logical operators like & (AND), | (OR),and ~ (NOT) can be used <strong>in</strong>stead of the morecumbersome set operator functions like <strong>in</strong>tersect,union, and setdiff that are necessary if <strong>in</strong>dexarrays are comb<strong>in</strong>ed.Advantages of Index Arrays1. Order <strong>in</strong>formation: Unlike logical arrays, the order of theelements <strong>in</strong> an <strong>in</strong>dex array provides useful <strong>in</strong>formation;e.g., the <strong>in</strong>dex array idxa returned <strong>by</strong> the functionsort <strong>in</strong>dicates the sorted order of the orig<strong>in</strong>alunsorted array a:aa =5 0 -1 9 0[sa, idxa] = sort(a)sa =-1 0 0 5 9idxa =3 2 5 1 42. Duplicate values: An <strong>in</strong>dex array can have multipleelements with the same <strong>in</strong>dex value, allow<strong>in</strong>g arrays tobe easily manipulated; e.g.,idx = [1 2 1];a(idx)ans =5 0 53. Space-sav<strong>in</strong>g: If only a few elements of the target arrayare be<strong>in</strong>g considered, then an <strong>in</strong>dex array need onlystore these elements, <strong>in</strong>stead of a logical array that isequal <strong>in</strong> size to the target array; e.g., the <strong>in</strong>dex arrayidxm<strong>in</strong>a has only a s<strong>in</strong>gle element:[m<strong>in</strong>a, idxm<strong>in</strong>a] = m<strong>in</strong>(a)m<strong>in</strong>a =-1idxm<strong>in</strong>a =316. Example: The Monti Carlo MethodThe Monti Carlo method is a general-purpose simulationtechnique that uses random numbers to estimate thesolutions to problems that are too difficult to solveanalytically or <strong>by</strong> us<strong>in</strong>g other, more specialized,approximation techniques. It differs from other types ofsimulation because it is used for static problems where timeis not <strong>in</strong>volved.In this example * , the value of pi will be estimated <strong>by</strong>determ<strong>in</strong><strong>in</strong>g the number m out of n po<strong>in</strong>ts that fall with<strong>in</strong> aunit circle (r = 1). The probability that a po<strong>in</strong>t (x, y)randomly generated <strong>in</strong>side a square is also <strong>in</strong>side the circle isequal to the ratio of area of the circle and the square:2 2 Acircleπr π mPx ( + y < 1) = = = ≈A 4 4 nsquare* Adapted from A. Csete, http://www.daimi.au.dk/~u951581/pi/MonteCarlo/pi.MonteCarlo.html.214

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

Saved successfully!

Ooh no, something went wrong!