Tải bản đầy đủ (.pdf) (10 trang)

Advanced M-file Features

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (86.86 KB, 10 trang )


43
A line starting with two percent signs (
%%
) denotes the
beginning of a MATLAB code cell. This type of cell has
nothing to do with cell arrays, but defines a section of
code in an M-file. Cells can be executed by themselves,
and cell publishing (discussed in Chapter 20) generates
reports whose sections are defined by an M-file’s cells.
7.7 MATLAB’s path
M-files must be in a directory accessible to MATLAB.
M-files in the current directory are always accessible.
The current list of directories in MATLAB’s search path
is obtained by the command
path
. This command can
also be used to add or delete directories from the search
path. See
doc

path
. The command
which
locates
functions and files on the path. For example, type
which

hilb
. You can modify your MATLAB path with the
command


path
, or
pathtool
, which brings up another
window. You can also select
File



Set

Path
.
8. Advanced M-file Features
This section describes advanced M-file techniques, such
as how to pass a function as an argument to another
function and how to write high-performance code in
MATLAB.
8.1 Function handles and anonymous
functions
A function handle (
@
) is a reference to a function that can
then be treated as a variable. It can be copied, placed in
cell array, and evaluated just like a regular function. For
example,

44
f = @sqrt
f(2)

sqrt(2)
The
str2func
function converts a string to a function
handle. For example,
f = str2func('sqrt')
f(2)
Function handles can refer to built-in MATLAB
functions, to your own function in an M-file, or to
anonymous functions. An anonymous function is defined
with a one-line expression, rather than by an M-file. Try:
g = @(x) x^2-5*x+6-sin(9*x)
g(1)
Some MATLAB functions that operate on function
handles need to evaluate the function on a vector, so it is
often better to define an anonymous function (or M-file)
so that it can operate entry-wise on scalars, vectors, or
matrices. Try this instead:
g = @(x) x.^2-5*x+6-sin(9*x)
g(1)
The general syntax for an anonymous function is
handle
= @(
arg1
,
arg2
,
...
)
expression


Here is an example with two input arguments:
norm2 = @(x,y) sqrt(x^2 + y^2)
norm2(4, 5)
norm([4 5])

45
One advantage of anonymous functions is that they can
implicitly refer to variables in the workspace or the
calling function without having to use the
global

statement. Try this example:
A = [3 2 ; 1 3]
b = [3 ; 4]
y = A\b
resid = @(x) A*x-b
resid(y)
A*y-b
In this case,
x
is an argument, but
A
and
b
are defined in
the calling workspace.
To find out what a function handle refers to, use
func2str
or

functions
. Try these examples:
func2str(f)
func2str(g)
func2str(norm2)
func2str(resid)
functions(f)
Cell arrays can contain function handles. They can be
indexed and the function evaluated in a single expression.
Try this:
h{1} = f
h{2} = g
h{1}(2)
f(2)
h{2}(1)
g(1)
Here is a more useful example. The
bisect
function,
below, solves the nonlinear equation f(x)=0. It takes a
function handle or a string as one of its inputs. If the

46
function is a string, it is converted to a function handle
with
str2func
.
bisect
also gives you an example of
nargin

and
nargout
(see also Section 7.5). Compare
bisect
with the built-in
fzero
discussed in Section
18.4.
function [b, steps] = bisect(f,x,tol)
% BISECT: zero of a function of one
% variable via the bisection method.
% bisect(f,x) returns a zero of the
% function f. f is a function
% handle or a string with the name of a
% function. x is an array of length 2;
% f(x(1)) and f(x(2)) must differ in
% sign.
%
% An optional third input argument sets
% a tolerance for the relative accuracy
% of the result. The default is eps.
% An optional second output argument
% gives a matrix containing a trace of
% the steps; the rows are of the form
% [c f(c)].

if (nargin < 3)
% default tolerance
tol = eps ;
end

trace = (nargout == 2) ;
if (ischar(f))
f = str2func(f) ;
end
a = x(1) ;
b = x(2) ;
fa = f(a) ;
fb = f(b) ;
if (trace)
steps = [a fa ; b fb] ;
end
% main loop
while (abs(b-a) > 2*tol*max(abs(b),1))
c = a + (b-a)/2 ;
fc = f(c) ;
if (trace)
steps = [steps ; [c fc]] ;
end

47
if (fb > 0) == (fc > 0)
b = c ;
fb = fc ;
else
a = c ;
fa = fc ;
end
end
Type in
bisect.m

, and then try:
bisect(@sin, [3 4])
bisect('sin', [3 4])
bisect(g, [0 3])
g(ans)
Some of MATLAB’s functions are built in; others are
distributed as M-files. The actual listing of any M-file,
MATLAB’s or your own, can be viewed with the
MATLAB command
type
. Try entering
type

eig
,
type

vander
, and
type

rank
.
8.2 Name resolution
When MATLAB comes upon a new name, it resolves it
into a specific variable or function by checking to see if it
is a variable, a built-in function, a file in the current
directory, or a file in the MATLAB path (in order of the
directories listed in the path). MATLAB uses the first
variable, function, or file it encounters with the specified

name. There are other cases; see
Help
:
MATLAB
:
Desktop

Tools

and

Development

Environment
:
Workspace
,
Search

Path
,
and

File

Operations
:
Search

Path

. You can use the command
which
to find
out what a name is. Try this:
clear
i
which i

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×