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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ANONYMOUS FUNCTIONSm = size(P,1)m =3n = size(P,2)n =2The last th<strong>in</strong>g that should be added to the function is somehelp <strong>in</strong>formation. All of the first block of contiguouscomment l<strong>in</strong>es <strong>in</strong> a function is returned when the helpcommand is executed for the function. Comment l<strong>in</strong>es are<strong>in</strong>dicated <strong>by</strong> an asterisk (%).To get help:help mydistMYDIST Euclidean distance from x to P.d = mydist(x,P)x = n-element vector s<strong>in</strong>gle po<strong>in</strong>tP = m x n matrix of n po<strong>in</strong>tsd = m-element vector, where d(i) =distance from x to P(i,:)The function mydist can now be used <strong>in</strong>side of anyexpression; e.g.,sumd = sum(mydist(x,P))sumd =10If other people will be us<strong>in</strong>g your function, it is a good ideato <strong>in</strong>clude some error check<strong>in</strong>g to make sure the values<strong>in</strong>put to the function are correct; e.g., check<strong>in</strong>g to make surethe po<strong>in</strong>ts <strong>in</strong> x and P have the same dimension.9. Anonymous FunctionsAnonymous functions provide a means of creat<strong>in</strong>g simplefunctions without hav<strong>in</strong>g to create M-files. Givenx = [3 1];P = [1 1; 6 1; 6 5];the sum of the distances from x to each of the po<strong>in</strong>ts <strong>in</strong> Pcan be determ<strong>in</strong>ed <strong>by</strong> creat<strong>in</strong>g a function handle sumMydistto an anonymous function:sumMydist = @() sum(mydist(x,P));sumMydistsumMydist =@() sum(mydist(x,P))sumMydist()ans =10The values of x and P are fixed at the time sumMydist iscreated. To make it possible to use different values for x:sumMydist = @(x) sum(mydist(x,P));sumMydist([6 1])ans =9sumMydist([4 3])ans =9.262410. Example: M<strong>in</strong>imum-Distance LocationAnonymous functions can be used as <strong>in</strong>put arguments toother functions. For example, fm<strong>in</strong>search performs generalpurposem<strong>in</strong>imization. Start<strong>in</strong>g from an <strong>in</strong>itial location x 0 , itcan be used to determ<strong>in</strong>e the location x that m<strong>in</strong>imizes thesum of distances to each po<strong>in</strong>t <strong>in</strong> P:x0 = [0 0];[x,sumd] = fm<strong>in</strong>search(sumMydist,x0)x =5.0895 1.9664sumd =8.6972For this particular location problem, any <strong>in</strong>itial location canbe used to f<strong>in</strong>d the optimal location because the objective isconvex:[x,sumd] = fm<strong>in</strong>search(sumMydist,[10 5])8

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

Saved successfully!

Ooh no, something went wrong!