Ryerson Crest Ryerson Header

MTH 207 Lab Lesson 4

Plotting


Up to Main Lab Page Next Lesson - Working with Functions Previous Lesson - Functions

Maple has the ability to create plots of functions and expressions which we define. This is done through the plot function.
The syntax is plot(expression, range [, parameters])
function is the function we which plotted.
range is the range of values to be plotted.
parameters is an optional list of parameters for plot.

Thus to plot the function x2 from -1 to 1:
> plot(x^2, x = -1..1);

The range and plot variable is indicated by x=-1..1. This tells Maple that we want x to be the plotted variable, -1 is the lower limit and 1 is the upper limit.
The double dots '..' is generally used by Maple to indicate a range.
NOTE: This is not an assignment of x, we do not write 'x:=-1..1'.

The default is an inline plot, you can tell Maple to plot things in a seperate window by selecting |Options| |Plot Display| |Window|.

We can also use previously defined functions and expressions for the function parameter.
> f:=x->x^2;
> plot(f(x), x = -1..1);

  1. Plot the following functions on the given ranges.
    1. y4 + 2y3 - 4y2 - 5y - 6, from -4 to 3.
    2. sin(x) for x from -2pi to 2pi
    3. ln(x) from 0 to 10.
    4. ln(sin(x)) from 0 to pi.
    5. ln(sin(x)) from 0 to 2pi.
    6. sin(ln(x)) from 0 to pi.
Note that if a function is not defined over a range then it is not plotted over that range. For example try
> plot(sqrt(y),y=-1..1);
There are no negative x values plotted since sqrt is not defined for negative x values.

If you want to plot two functions on the same graph you can enclose them in parenthasise. Thus
> f:=x->x^2;
> g:=x->2*x;
> plot({f(x), g(x)}, x = -1..1);
will plot f and g on the same graph from -1 to 1.

Discontinuities

If we try ploting the function trunc(x).
> plot(trunc(x),x=-3..3);
Maple produces a (nealy) vertical line at each integer value. This is because Maple assumes that any function you give it to plot is continuous, but trunc is discontinuous, at integer values it jumps.

We can tell Maple that the function is discontinuous by using the discont parameter in plot. Try
> plot(trunc(x), x=-3..3, discont=true);

  1. Plot the following functions from -5 to 5.
    1. round(x)
    2. floor(x2)
    3. f(x) = { x if x < 0
      x2 + 1 if 0 <= x < 1
      x2 Otherwise
    4. f(x) = { ex if x < 0
      sin2(x) if 0 <= x < PI
      sqrt(cos(x)) Otherwise

To see other parameters avalaible for plot try
> ?plot[options]

Infinite plots

Maple allows us to plot an infinite range by using infinity:
> plot(x^2, x = -infinity..infinity);
Gives a plot of x2 from -infinity to infinity.

While this can be useful it is generally discouraged. The reason for this is that in order to fit an infinite range on the x axis Maple has to squeeze the ends. Thus the plot may look nothing like the original function. Even in this simple example the ends of the plot do not look right, they start to curve away and down, rather than up (they appear cancave up rather than concave down). For this reason Maple does not print values on the axes when doing an infinite plot.

To see an example of this try plotting sin(x) on an infinite range and notice how the ends bunch up, rather than giving a smooth sinusoidal curve out to infinity.

    1. Plot sin(x) on an infinite range.
      Notice how the ends bunch up, rather than giving a smooth sinusoidal curve out to infinity.
    2. Plot x^4+2*x^3-2*x^2-5 on an infinite range. What errors would you expect?
    3. Plot ln(x) on an infinite range. What errors would you expect? How does this graph differ from the true graph?

Problems with plot

Maple is not magic, and plot has many pitfalls and shortcomings. Often you must help Maple to get things right. Suppose we try to plot the following polynomial
> f:=x->x^4+2*x^3-2*x^2-5;
> plot(f(x), x=-10..10);
This plot appears to go to 0 at x=0. However Now try
> plot(f(x), x=-infinity..infinity);
While the shape of the graph is clearly wrong near infinity, this graph shows a definite wiggle below the x axis near zero.
Which plot is correct?

If we try
> f(0);

-5
This would tend to suggest that the infinite plot is more accurate near zero. On the other hand
> f(10);
11795
What is happening is that the y scale must go to 1200, in order to show f(10). However on this scale -5 is not differentiable from 0. The simplest way to fix a problem of this sort is to include a horizontal range as an optional third argument to plot.
> plot(f(x), x=-10..10, -20..20);
The third argument, -20..20, tells Maple to restrict the vertical range to this. In this case we get the squiggle at zero below the x axis back.

This is a general problem of scale, the fine detail was lost because the scale of the detail was far less than the scale of the graph, yet we are often interested in where graphs cross the x axis, and in turning points of a graph.

For another similar problem consider the plot
> plot(1/(1-x)^2, x=-infinity..infinity);
While the behaviour at infinity seems correct, this plot shows a peak at x = 1, but we know that in fact the function is unbounded at x = 1, i.e. this peak is infinite. If we try
> plot(1/(1-x)^2, x=0..2);
The scale of the peak completely overshadows the nearby behaviour. About the best we can get is
> plot(1/(1-x)^2,x=0..2, 0..50);

    1. Plot f(x) = sin(1/x) on the intervals -1 to 1 and -0.1 to 0.1, what is the difference? What happens if you try to plot from -infinity to infinity?
    2. Use plot to estimate the roots of x3 + 2x2 - 5x - 6.
    3. Obtain a reasonable plot of (x^2+x+1)/(x^2-1) for x between -2 and 2.



Up to Main Lab Page Next Lesson - Working with Functions Previous Lesson - Functions Top of this Lesson


Maintained by: P. Danziger, January 1998