
Scripts and Functions
The function plot_f han dle, shown below, receives a function handle and
data, generates y-axis data using the function handle, and plots it:
function x = p lot_fhandle(fhandle , data)
plot(data, fhandle(data))
When you call plot_fhandle with a handle to the sin function and the
argument shown below, the resulting evaluation produces a sine wave plot:
plot_fhandle(@sin, -pi:0.0 1:pi)
Function Functions
A class of functions called “function functions” works with nonlinear functions
of a scalar variable. That is, one function works on another function. The
function functions include
• Zero finding
• Optimization
• Quadrature
• Ordinary differen tial equations
MATLAB represents the nonlinear function by a function M-file. For example,
here is a si mplified version of the function
humps from the matlab /dem os
directory:
function y = h umps(x)
y = 1./((x-.3).^2 + .01) + 1./((x-.9).^2 + . 04) - 6;
Evaluate this function at a set of points in the interval 0 ≤ x ≤ 1with
x = 0:.002:1;
y = humps(x);
Then plot the function with
plot(x,y)
4-29
Comentarios a estos manuales