{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

New Word 2007 Document (29)

# New Word 2007 Document (29) - .almost Here are the steps...

This preview shows page 1. Sign up to view the full content.

Variable Function Names Suppose we have written an M file called bisect.m and we want to use it on some function. Then we have to create an M file called cosy.m , because that is what the bisect function expects. Suppose that we have 6 files, called f1.m through f6.m , each of which evaluates a function that we want to use with bisect . We could rename f1.m to cosy.m , and edit its contents, (because the name f1 occurs in the text of the file as well), and repeat this for the other files. But who wants to do that? Besides, cosy is a stupid name! There is a much more convenient way to do this. The bisect routine treats the endpoints of the interval as variables , and gives them "dummy names" that we fill in when we use the routine. We can do the same thing with functions...almost. Here are the steps required:
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ..almost. Here are the steps required: • In the "declaration line" for bisect , include a dummy function name. We might as well call it f , so the line becomes function approx_root = bisect ( f , a, b ) • Where we evaluated the function by writing f(x) , we now have to use the MATLAB "helper" function called feval . For instance, the line fa = cosy(a); must be rewritten as: fa = feval ( f, a ) ; • When we use bisect , we must include the function name, in quotes: x = bisect ( 'cosy' , 0, 3 ) Exercise : Follow the directions, and modify the file bisect.m so that the function name is a variable. Once you are done, see if the command x = bisect ( 'cosy', 0, 3 ) is acceptable to MATLAB and produces the same root as before....
View Full Document

{[ snackBarMessage ]}