INTRODUCTION TO MAPLE
Teresa Dern Henson, Lecturer
Department of Mathematics
Naval Postgraduate School
September, 1995
SECTION 3
PLOTTING WITH MAPLE
Single-Variable Functions and Equations in Two Variables
GOALS:
In this section we will learn how to
1. plot single-variable functions;
2. plot several single-variable functions together;
3. customize plots through the use of plot options;
4. plot functions with discontinuities;
5. put several customized plots too-ether in one window; and,
6. plot equations in two variables.
THE PLOT COMMAND
1. Plotting Single-Variable Functions. If expr is an expression
involving only the single variable x, then the input
allows us to treat f as a function of x, although it does
not formally define f as a function.
We may plot this function by using the plot command. The input
> plot( expr, x); or > Plot( f, x);
will plot f, as a function of x, over the (default) interval
from x = -10 to x = 10. You may specify a plotting
interval by including the argument x = a..b where [a, b]
is the interval you wish to plot over.
The vertical scale in the plot window will reflect the natural range of
f over the specified x-interval. You can limit the range
by including an optional argument in the plot command. SEE item 2, Plot
Options for more information.
EXAMPLE: To plot the function f(x)
= x + sin x over the interval [-2p , 2p ], input
> plot ( x + sin(x), x = -2 * Pi..2 * Pi );
You can plot several functions together by listing the functions in a set
within the plot command.
EXAMPLE: We will define the functions f(x)
= x + sin x and g(x) = x2 and
then plot the two functions together over the interval [-2, 2].
>f := x + sin(x);
>g := x^2;
>plot( { f, g }, x = -2..2 );
2. Plot Options. There are optional entries that can be included
in the basic plot command which allow you to control the range of the plot,
the scaling, the line style, the color in which the graph is drawn, as
well as other features. If you type ?plot[options] at a prompt you can
learn more about the options available.
Optional entries are included in the plot command AFTER the specification
of the x-interval and are separated by commas. If you wish to include
an entry specifying a range for the plot, this argument must appear immediately
following the specification of the x-interval is given in the form
of an interval. The plot command with a specified range would have the
form
> plot( f, x = a..b, c..d );
An example is given immediately below. Otherwise, the order in which the
optional arguments are listed is unimportant.
EXAMPLE: To plot the function f (x)
= tan(x) over the x-interval [-2p , 2p ] and with a range
of [-10, 10], input
>plot( tan(x), x = -2*Pi..2*Pi, -10..10 );
that it is not necessary
to specify the name of the dependent variable when including a range. In
the resulting plot window, no name will be assigned to the vertical axis.
You can put a name on the vertical axis by using the argument of the form
name = c..d.
EXAMPLE: To plot the function f(x)
= tan(x) over the x-interval [ -2p , 2p ], with a range of [
-10, 10] and with the name f(x) on the vertical axis,
input
>plot( tan(x),x = -2*Pi..2*Pi, f(x) = -l0..10 );
: If some expression
has been assigned to the variable name f(x) then Maple
will use that expression to label the vertical axis, not the variable name.
EXAMPLE: To plot the function
over
the interval [-2, 2] in cyan (yes, this is a color) with diamond shaped
points input
>plot( x/(x^2 + 1), x = -2..2, style = point, symbol = diamond, color
= cyan );
3. Plotting Over Discontinuities. Plotting over discontinuities
in Maple poses a special problem. Maple plots a graph in the plane by dividing
the specified x-interval up into a number of points, computing function
values at those points and then "connecting the dots". When plotting over
discontinuities, Maple can end up connecting disconnected pieces or trying
to plot function values which are going to infinity. Try plotting tan(x)
over the interval [ -2p , 2p ] without specifying a range to see what we
mean.
There are several ways in which you can handle this problem. One way
is to include the optional argument discont = true in the plot command
after specifying a range interval. Unfortunately, when this argument is
included Maple automatically selects a color for the plot which cannot
be altered.
input >plot( tan(x), x = -2*Pi..2*Pi, f(x) = -10..10,
discont = true);
An alternative is to plot the function in pieces, selecting plotting
intervals which avoid the points of discontinuity, and then display the
pieces together. The use of the display command is explained in item four
below.
4. display. As we have seen, we can plot several functions
together by plotting a set of functions. But it is not possible to control
the color or style of the plotted functions individually when doing this.
[Added NOTE: control of the colors can be done using the argument color
= {color1, color2,…,colorN}, whenever there are N functions being plotted.]
As an alternative, we can plot each function separately and store the plot
data. in a file. These plot files can then be displayed together by using
the display command.
EXAMPLE: We will plot the function
together
with its first and second derivatives.
(a) First, define f and find its first and second derivatives,
df1 and df2, respectively.
>f := (x^2) ^ (1/3) + x ;
>dfl := diff( f, x ) ; df2 := diff( dfl, x )
that we can put more than
one input on a line. Also note that the first and second derivatives will
be undefined at x = 0.
(b) Plot f to select appropriate horizontal and vertical
ranges. Based on the plot of f, we will use an x-interval
of [-2, 2) and a y-interval of [ -1, 3].
(c) Next, create the plot files for f, df1, and df2.
Note that the input line for each plot file ends with a colon, not a semicolon,
so that output is suppressed. When the plot is assigned to a variable name,
the output is simply the plot data, not a plot. In order to distinguish
between the three graphs, the graphs of the derivatives are plotted in
different colors and styles. The first line of input loads the plotting
package plots.
>with(plots):
>pf := plot( f, x = -2..2, y =-1..3 ):
>pdfl := plot( dfl, x = -2..2, y = -l..3, color = cyan ):
>pdf2 := plot( df2, x = -2..2, y = -l..3, color = gold, style = point
):
Where options are not specified, the defaults will be used (e.g., f
will be plotted as a white line).
(d) Now display these plots in one window using the display command.
The plots must be enclosed in a set within the argument of the command.
>display( {pf, pdfl, pdf2} );
5. implicitplot. Equations such as
and x = 2 have graphs (what are they?) but their graphs are not
the graphs of functions. To plot equations in Maple we use the implicitplot
command. The name of this command comes from the fact that an equation
in x and y implicitly defines y as a function of x.
(Remember implicit differentiation?). The syntax of this command is similar
to that of the plot command: But, instead of a function you have an equation,
and you must specify both x- and y-intervals. Thus
>implicitplot( eqtn, x = a..b, y = c..d );
To access this function, you must first load the plots package. To load
the plots package input the command
The plots package need only be loaded once in a session. All of the plot
options available for plot may also be used with implicitplot.
EXAMPLE: The equation
defines
a circle of radius
centered
at (2, -1). To plot this circle first load the plots package and then input
>implicitplot((x - 2)^2 + (y + 1)^2 = 3, x = 0..4, y = -3..l );
An alternative approach to the above is to assign the equation to a variable
and then use the variable in the implicitplot command in place of the equation.
>eqtn := (x - 2)^2 + (y + 1)^2 = 3;
>implicitplot( eqtn, x = 0..4, y = -3..1 );
EXAMPLE: To plot the equation x = 2
use
>implicitplot( x = 2, x = 0..4, y = -3..3 );
that when plotting equations
of the form x = k, for some constant k, you must select
an x-interval [ a, b] which satisfies a < k
< b.