next up previous
Next: Exercises Up: Preliminaries about Maple Previous: The subs command

Subsections


Plotting with Maple

Let us now use the graphics facilities of Maple:
> 
  plot(x^2, x=-10..10);

\begin{mfigure}\centerline{ \psfig {figure=BFig3.ps,height=2.5in}}\end{mfigure}

Maple displays the graph of the function x2 from x = - 10 to x = 10 as requested. In the worksheet interface, clicking the right mouse button on the graph pops up a menu with various options which allow you to manipulate your graph, and/or change its appearance in a suitable way. Clicking the left button ``selects'' the graphic, drawing a black box with ``handles'' around it. You can drag the mouse button on these to resize the plot. Also, the coordinates of the point where you clicked appear in the upper left of the worksheet. Selecting a graphic also changes the context bar: shortcuts for manipulating the axes, aspect ratio, and plotting style appear as buttons along the top.

The basic syntax of the plot command is

> 
  plot(f,range);

where f is the expression to be plotted and range is the range of the parameter(s) for which you would like to see the plot of f. In the example above, we indicated the range of x by x=-10..10. Maple automatically chooses a scale on the vertical axis. There are many options to the plot command; while we will mention a few of them, the on-line help system has all the details.

Try both of the two similar plot commands below:

> 
  plot(x^2-x, x=-1..2, y=-1..2);
  plot(x^2-x, x=-1..2,   -1..2);

In both cases, the plot is displayed for values of y between -1 and 2, but in the first, the variable y is explicitly named in the command, and the vertical axis gets a label. Whenever you are plotting an expression, the variable in the domain of the function must be specified.

Plotting several functions (or curves) on the same axes

We can plot more than one function (or curve) on the same coordinate system. Consider the following example, recalling the slope function of the line passing through the points (x1, y1), (x2, y2), used to define the slope m(x) of the line passing through (1, 1) and (x, x2):
> 
  slope:=(x1,y1,x2,y2)->(y2-y1)/(x2-x1);
  m:=x->slope(1,1,x,x^2);


\begin{maplelatex}
\begin{displaymath}\mathit{slope} := (x1, y1, x2, y2) \righta...
...h}\mathit{m} := x \rightarrow slope(1,1,x,x^2) \end{displaymath}\end{maplelatex}

We now define the equation of the lines passing through (1, 1) and (x, x2) for x = 2, 3, 4, 5 and plot the four lines together with the function x^2.

> 
  line1:=m(2)*(x-1)+1;
  line2:=m(3)*(x-1)+1; 
  line3:=m(4)*(x-1)+1; 
  line4:=m(5)*(x-1)+1; 
  plot(x^2,line1,line2,line3,line4,x=-3..6);


\begin{maplelatex}
\begin{displaymath}line1 := 3 x - 2 \end{displaymath}\begin{d...
...isplaymath}\begin{displaymath}line4 := 6 x - 5 \end{displaymath}\end{maplelatex}

\begin{mfigure}\centerline{ \psfig {figure=BFig4.ps,height=2.5in}}\end{mfigure}

Observe that the expressions to be plotted are enclosed in braces { }. Besides specifying the range on which x varies as done above, you can also specify and label the y range proceeding exactly in the same manner as when plotting a single function.1.13

Fancier plotting

The plot command is very powerful, and you should look for details about it using the online help facility; the command ?plot will bring this up. There are also several related pages, and some instructive example worksheets that are well worth looking at.

To make you aware of some of its features, we will discuss a few of them here:

We end with a word about plotting functions (as opposed to expressions), and a situation in which it is a good idea to do so. Sometimes you will have a relationship that you want to plot in the form of a function rather than an expression:

> 
  f:=x->x^3*exp(-x):

In this situation, you can plot f(x) as explained above. Alternatively, you may use:

> 
  plot(f, 0..3);

The two ways of plotting a function should not be confused. Neither of the following two statements will work correctly:

> 
  plot(f(x), 0..3);


\begin{maplettyout}
Plotting error, empty plot
\end{maplettyout}

> 
  plot(f, x=0..3);

As you see, the first gives an error; the second gives a plot with nothing plotted on it.


If you have a function defined using an if-then clause, you must use the function plotting command:

> 
  f:=x-> if x<3 then x+1 else -x^2+13 fi:
  plot(f(x),x=0..5);


\begin{maplettyout}
Error, (in f) cannot evaluate boolean
\end{maplettyout}

The error resulted because Maple attempts to understand the expression f(x) before it has a value for x. One way around this1.14is to use the command

> 
  plot(f, 0..5);

\begin{mfigure}\centerline{ \psfig {figure=BFig6.ps,height=2.4in}}\end{mfigure}

You can see from the graph that the function f is probably continuous but not differentiable at x=3.



Footnotes

... function.1.13
We could have done this all in a single plot command, either explicitly typing each of the lines, or using seq to generate them: plot({x^2, seq( m(i)*(x-1)+1, i=2..5)}, x=-3..6);
... this1.14
We could also delay evaluation of f(x) using quotes, with plot('f(x)', x=0..5);. Alternatively, we could define f using piecewise, as f:=x-> piecewise(x<3, x+1, -x^2+13);.

next up previous
Next: Exercises Up: Preliminaries about Maple Previous: The subs command

Translated from LaTeX by Scott Sutherland
2002-08-29