Troubleshooting

Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.
. . . . . . . . . . . . . . . . . 14-15
Converting Your Optimization Code to MATLAB Version
5 Syntax . . . . . . . . . . . . . . . . . . . 14-16
Numerical Integration (Quadrature) . . . . . . . . 14-19
Example: Computing the Length of a Curve . . . . . . . 14-19
Example: Double Integration . . . . . . . . . . . . . 14-20

14 Function Functions
All of the functions described in this chapter are called function functions
because they accept a function as an input arguments. You can pass such a
function either as a function handle or as an inline object that defines a
mathematical function. The function that is passed in is referred to as the
objective function.
This chapter includes:
Function Summary
A summary of some function functions
Representing Functions in MATLAB
Some guidelines for representing functions in MATLAB
Plotting Mathematical Functions
A discussion about using fplot to plot mathematical functions
Minimizing Functions and Finding Zeros
A discussion of high-level function functions that perform optimization-related
tasks
Numerical Integration (Quadrature)
A discussion of the MATLAB quadrature functions
Note See the “Differential Equations” and “Sparse Matrices” chapters for
information about the use of other function functions.
Note For information about function handles, see the function_handle (@),
func2str, and str2func reference pages, and the “Function Handles” section
of “Programming and Data Types” in the MATLAB documentation.
14-2
Function Summary
Function Summary
The function functions are located in the MATLAB funfun directory.
This table provides a brief description of the functions discussed in this
chapter. Related functions are grouped by category.
Function Summary
Category
Function
Description
Plotting
fplot
Plot function.
Optimization
fminbnd
Minimize function of one variable with
and zero finding
bound constraints.
fminsearch
Minimize function of several variables.
fzero
Find zero of function of one variable.
Numerical
quad
Numerically evaluate integral, adaptive
integration
Simpson quadrature.
quadl
Numerically evaluate integral, adaptive
Lobatto quadrature.
dblquad
Numerically evaluate double integral.
14-3
14 Function Functions
Representing Functions in MATLAB
MATLAB can represent mathematical functions by expressing them as
MATLAB functions in M-files or as inline objects. For example, consider the
function
1
f( x)
1
= ---------------------- + ---------------------- – 6
( x – 0.3)2 + 0.01 ( x – 0.9)2 + 0.04
This function can be used as input to any of the function functions.
As MATLAB Functions
You can find the function above in the M-file named humps.m.
function y = humps(x)
y = 1./((x - 0.3).^2 + 0.01) + 1./((x - 0.9).^2 + 0.04) - 6;
To evaluate the function humps at 2.0, use @ to obtain a function handle for
humps, and then pass the function handle to feval.
fh = @humps;
feval(fh,2.0)
ans =
-4.8552
As Inline Objects
A second way to represent a mathematical function at the command line is by
creating an inline object from a string expression. For example, you can create
an inline object of the humps function
f = inline(‘1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04)-6’);
You can then evaluate f at 2.0.
f(2.0)
ans =
-4.8552
You can also create functions of more than one argument with inline by
specifying the names of the input arguments along with the string expression.
For example, the following function has two input arguments x and y.
14-4
Representing Functions in MATLAB
f= inline('y*sin(x)+x*cos(y)','x','y')
f(pi,2*pi)
ans =
3.1416
14-5
14 Function Functions
Plotting Mathematical Functions
The fplot function plots a mathematical function between a given set of axes
limits. You can control the x-axis limits only, or both the x- and y-axis limits.
For example, to plot the humps function over the x-axis range [-5 5], use
fplot(@humps,[-5 5])
grid on
100
80
60
40
20
0
−20
−5
−4
−3
−2
−1
0
1
2
3
4
5
You can zoom in on the function by selecting y-axis limits of -10 and 25, using
fplot(@humps,[-5 5 -10 25])
grid on
14-6
Plotting Mathematical Functions
25
20
15
10
5
0
−5
−10
−5
−4
−3
−2
−1
0
1
2
3
4
5
You can also pass an inline for fplot to graph, as in
fplot(inline('2*sin(x+3)'),[-1 1])
You can plot more than one function on the same graph with one call to fplot.
If you use this with a function, then the function must take a column vector x
and return a matrix where each column corresponds to each function,
evaluated at each value of x.
If you pass an inline object of several functions to fplot, the inline object also
must return a matrix where each column corresponds to each function
evaluated at each value of x , as in
fplot(inline('[2*sin(x+3), humps(x)]'),[-5 5])
which plots the first and second functions on the same graph.
14-7
14 Function Functions

100
80
60
40
20
0
−20
−5
−4
−3
−2
−1
0
1
2
3
4
5
Note that the inline
f= inline('[2*sin(x+3), humps(x)]')
evaluates to a matrix of two columns, one for each function, when x is a column
vector.
f([1;2;3])
returns
-1.5136 16.0000
-1.9178 -4.8552
-0.5588 -5.6383
14-8
Minimizing Functions and Finding Zeros
Minimizing Functions and Finding Zeros
MATLAB provides a number of high-level function functions that perform
optimization-related tasks. This section describes:
• Minimizing a function of one variable
• Minimizing a function of several variables
• Setting minimization options
• Finding a zero of a function of one variable
• Converting your code to MATLAB Version 5 syntax
The MATLAB optimization functions are:
fminbnd
Minimize a function of one variable on a fixed interval
fminsearch
Minimize a function of several variables
fzero
Find zero of a function of one variable
lsqnonneg
Linear least squares with nonnegativity constraints
optimget
Powered by wordpress | Theme: simpletex | © Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.