Note:
Recall in section 3.2 we discussed how to pass a function as an argument.
42

4.1. Two-Dimensional Graphics
This function uses adaptive step control to generate as many data points as it considers necessary to
plot the function accurately. You can also store the data points calculated by
>> [x, y] = fplot(<function name>, <limits>)
rather than having the function plotted directly. You then have complete control over how to plot the
curve using the
plot
function.
The other command which can plot a function is
ezplot
, which is more general than
fplot
. To plot a
function on the interval [
−
2
π,
+2
π
] enter
>> ezplot(<function name>)
To include limits (as with
fplot
) enter
>> ezplot(<function name>, <limits>)
In addition, a parametrically defined function can be plotted by
>> ezplot(<fnc 1>, <fnc 2>, <limits>)
Finally, this command can also plot an implicitly defined function, i.e.,
f
(
x, y
) = 0, by
>> ezplot(<2D fnc>, <limits>)
For example,
>> f = @(x, y) (x
ˆ
2 + y
ˆ
2)
ˆ
2 - (x
ˆ
2 - y
ˆ
2);
>> ezplot(f)
plots the lemniscate of Bernoulli (basically an “
∞
” symbol).
Warning:
Be particularly careful when plotting implicit functions because they can be
and
occasionally
ezplot
may not get it right.
There is an important difference between
>> fplot(f, [-5 5])
and
>> ezplot(f, [-5 5])
In the former
f(x)
is only evaluated for scalar values of
x
, while in the latter
f(x)
is evaluated for vec-
tor values of
x
.
Thus, when using
ezplot
care must be taken if
f
is evaluated in a function m-file. If
f(x)
cannot be evaluated for vector values, the error message
Warning: Function failed to evaluate on array inputs; vectorizing the function may
speed up its evaluation and avoid the need to loop over array elements.
will be generated
fplot
and
ezplot
do not always generate exactly the same curves. For example, in
>> f = @(x) log(x) + 1;
>> fplot(f, [-2*pi 2*pi])
>> ezplot(f)
fplot
generates a spurious plot for
x
∈
[
−
2
∗
pi,
0) where it plots the real part of log
x
while
ezplot
only plots the function for
x
∈
(0
,
2
π
].
Also, in
>> f = @(x) x ./ (x
ˆ
2 + 0.01);
>> fplot(f, [-2*pi +2*pi])
>> ezplot(f)
the vertical axes are different and
ezplot
is missing part of the curve. Finally, in
f = @(x) x
ˆ
3/(x
ˆ
2 + 3*x - 10);
ezplot(f, [-10 +10])
the function blows up at
x
=
−
5 and 2 and part of the curve for
x
∈
(
−
5
,
2) is not shown.
Polar plots can also be generated by the
polar
command. There is also an “easy” command for gener-
ating polar plots, namely
ezpolar
.
Since you often want to label the axes and put a title on the plot, there are specific commands for each
of these. Entering
>> xlabel(<string>)
>> ylabel(<string>)
>> title(<string>)
put labels on the
x
-axis, on the
y
-axis, and on top of the plot, respectively. Note that a title can contain
more than one line as was discussed in section 3.
43

4.1. Two-Dimensional Graphics
For example, typing
title(t)
where
t = [
′
The Dead
′
′
Parrot Sketch
′
]
or
t =
{
′
The Dead
′
′
Parrot Sketch
′
}
or
t =
{
′
The Dead
′
;
′
Parrot Sketch
′
}
results in a two-line title. The former uses a “standard” array and so requires all the rows to have the
same number of columns, whereas the latter uses a cell array and so each row can have a different length.

#### You've reached the end of your free preview.

Want to read all 146 pages?

- Fall '13
- Ivan
- matlab, .........