Next: Two-dimensional Geometric Shapes, Previous: Axis Configuration, Up: Two-Dimensional Plots [Contents][Index]
Octave can plot a function from a function handle, inline function, or
string defining the function without the user needing to explicitly
create the data to be plotted. The function fplot
also generates
two-dimensional plots with linear axes using a function name and limits
for the range of the x-coordinate instead of the x and y data. For
example,
fplot (@sin, [-10, 10], 201);
produces a plot that is equivalent to the one above, but also includes a legend displaying the name of the plotted function.
Plot a function fn within the range defined by limits.
fn is a function handle, inline function, or string containing the name of the function to evaluate.
The limits of the plot are of the form [xlo, xhi]
or
[xlo, xhi, ylo, yhi]
.
The next three arguments are all optional and any number of them may be given in any order.
tol is the relative tolerance to use for the plot and defaults to 2e-3 (.2%).
n is the minimum number of points to use. When n is specified,
the maximum stepsize will be xhi - xlo / n
. More
than n points may still be used in order to meet the relative
tolerance requirement.
The fmt argument specifies the linestyle to be used by the plot command.
If the first argument hax is an axes handle, then plot into this axis,
rather than the current axes returned by gca
.
With no output arguments the results are immediately plotted. With two
output arguments the 2-D plot data is returned. The data can subsequently
be plotted manually with plot (x, y)
.
Example:
fplot (@cos, [0, 2*pi]) fplot ("[cos(x), sin(x)]", [0, 2*pi])
Programming Notes:
fplot
works best with continuous functions. Functions with
discontinuities are unlikely to plot well. This restriction may be removed
in the future.
fplot
requires that the function accept and return a vector argument.
Consider this when writing user-defined functions and use .*
,
./
, etc. See the function vectorize
for potentially
converting inline or anonymous functions to vectorized versions.
Other functions that can create two-dimensional plots directly from a
function include ezplot
, ezcontour
, ezcontourf
and
ezpolar
.
Plot the 2-D curve defined by the function f.
The function f may be a string, inline function, or function handle
and can have either one or two variables. If f has one variable, then
the function is plotted over the domain -2*pi < x < 2*pi
with 500 points.
If f2v is a function of two variables then the implicit function
f(x,y) = 0
is calculated over the meshed domain
-2*pi <= x | y <= 2*pi
with 60 points in each dimension.
For example:
ezplot (@(x, y) x.^2 - y.^2 - 1)
If two functions are passed as inputs then the parametric function
x = fx (t) y = fy (t)
is plotted over the domain -2*pi <= t <= 2*pi
with 500 points.
If dom is a two element vector, it represents the minimum and maximum
values of both x and y, or t for a parametric plot. If
dom is a four element vector, then the minimum and maximum values are
[xmin xmax ymin ymax]
.
n is a scalar defining the number of points to use in plotting the function.
If the first argument hax is an axes handle, then plot into this axis,
rather than the current axes returned by gca
.
The optional return value h is a vector of graphics handles to the created line objects.
See also: plot, ezplot3, ezpolar, ezcontour, ezcontourf, ezmesh, ezmeshc, ezsurf, ezsurfc.
Plot the contour lines of a function.
f is a string, inline function, or function handle with two arguments
defining the function. By default the plot is over the meshed domain
-2*pi <= x | y <= 2*pi
with 60 points in each dimension.
If dom is a two element vector, it represents the minimum and maximum
values of both x and y. If dom is a four element vector,
then the minimum and maximum values are [xmin xmax ymin ymax]
.
n is a scalar defining the number of points to use in each dimension.
If the first argument hax is an axes handle, then plot into this axis,
rather than the current axes returned by gca
.
The optional return value h is a graphics handle to the created plot.
Example:
f = @(x,y) sqrt (abs (x .* y)) ./ (1 + x.^2 + y.^2); ezcontour (f, [-3, 3]);
See also: contour, ezcontourf, ezplot, ezmeshc, ezsurfc.
Plot the filled contour lines of a function.
f is a string, inline function, or function handle with two arguments
defining the function. By default the plot is over the meshed domain
-2*pi <= x | y <= 2*pi
with 60 points in each dimension.
If dom is a two element vector, it represents the minimum and maximum
values of both x and y. If dom is a four element vector,
then the minimum and maximum values are [xmin xmax ymin ymax]
.
n is a scalar defining the number of points to use in each dimension.
If the first argument hax is an axes handle, then plot into this axis,
rather than the current axes returned by gca
.
The optional return value h is a graphics handle to the created plot.
Example:
f = @(x,y) sqrt (abs (x .* y)) ./ (1 + x.^2 + y.^2); ezcontourf (f, [-3, 3]);
Plot a 2-D function in polar coordinates.
The function f is a string, inline function, or function handle with
a single argument. The expected form of the function is
rho = f(theta)
.
By default the plot is over the domain 0 <= theta <= 2*pi
with 500 points.
If dom is a two element vector, it represents the minimum and maximum values of theta.
n is a scalar defining the number of points to use in plotting the function.
If the first argument hax is an axes handle, then plot into this axis,
rather than the current axes returned by gca
.
The optional return value h is a graphics handle to the created plot.
Example:
ezpolar (@(t) sin (5/4 * t), [0, 8*pi]);
Next: Two-dimensional Geometric Shapes, Previous: Axis Configuration, Up: Two-Dimensional Plots [Contents][Index]