www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/FunctionPlottingFunctionExample.m

    %% Function Plotting Function
% This example shows how to use the |fplot| function to plot a mathematical
% function between a given set of axes limits.

% Copyright 2015 The MathWorks, Inc.


%%
% You can control the x-axis limits only, or both the x- and y-axis limits.

%%
% Plot the |humps.m| function over the x-axis range |[-5 5]|.
fplot(@humps,[-5 5])
grid on

%%
% Zoom in on the function by selecting y-axis limits of |-10| and |25|.
ylim([-10 25])

%%
% You can also pass the function handle for an anonymous function for
% |fplot| to graph.
fplot(@(x)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 anonymous function consisting of several functions to
% |fplot|, the anonymous function also must return a matrix where each
% column corresponds to each function evaluated at each value of |x|
fplot(@(x)[2*sin(x+3),humps(x)],[-5 5])

%%
% This plots the first and second functions on the same graph.

%%
% Note that the anonymous function
fh = @(x)[2*sin(x+3),humps(x)];

%%
% evaluates to a matrix of two columns, one for each function, when |x| is a column vector.
fh([1;2;3])