Plotting with Maple

Copyright Michael Sullivan, 2007

Basic plotting.

Maple has a powerful set of graphing tools. The two   with commands below load in the graphing commands we need. The restart clears the memory. We shall start each section with these three commands. However, the plotting commands in this section are loaded by default anyway. (Change the colons to semi-colons and notice the effect.)

> restart;

> with(plots):

Warning, the name changecoords has been redefined

> with(plottools):

We can define a function f(x) as follows:

Warning, the assigned name arrow now has a global binding

> f := x -> x*sin(x);       

f := proc (x) options operator, arrow; x*sin(x) end proc

> f(2);                        

2*sin(2)

To get the result in floating point format we use the evalf command:

> evalf(f(2));               

1.818594854

Now we do a simple plot of f(x).

> plot(f(x),x=-3*Pi..3*Pi,y=-5..10, color=black,thickness=2);

[Plot]

Next we do a rational function.

> g := x -> (x+1)/(x-3);         

g := proc (x) options operator, arrow; (x+1)/(x-3) end proc

> plot(g(x),x=-5..5,y=-10..10,color=black,thickness=2);

[Plot]

Maple does not understand vertical asymptotes. But it will know what do do if we give it  hint! The option "discont=true" tells Maple that the function may have discontinuities. Then Maple will skip other them.

> plot(g(x),x=-5..5,y=-10..10,color=black,thickness=2,discont=true);

[Plot]

Graphs can be overlayed.

> plot([f(x),g(x)],x=-10..10,y=-10..10,discont=true,color=[red,blue],thickness=2);

[Plot]

Here and below you can use the HELP button (top right-hand corner) to look up information about any command or option.  You should practice a few graphs of your own before going on.

Example 1: Marking special points.

> restart;with(plots):with(plottools):

Warning, the name changecoords has been redefined

Warning, the assigned name arrow now has a global binding

> f := x -> x^3+x^2+1;         

f := proc (x) options operator, arrow; x^3+x^2+1 end proc

We find the extrema. We use the diff command and then the solve command.

> diff(f(x),x);      

3*x^2+2*x

> solve(3*x^2+2*x=0);   

0, (-2)/3

These are the critical numbers. We evaluate the function f(x) at these two numbers.

> f(0);      

> f(-2/3);       

Next we find the second derivative.

> diff(f(x),x$2);    

Clearly f(0) is a relative min and f(-2/3) is a relative max. We also see that the inflection point is at x=-1/3.

> f(-1/3);      

Note: Obviously you do not need computer to analyze a cubic. We are starting with easy examples to learn how Maple works. Using a computer or calculator to solve such an easy problem will, in general, hurt your learning.

Now we are ready to set up our graph.

> Min:=disk([0,1], 0.03, color=red):

> Max:=disk([-2/3,31/27], 0.03, color=red):

> Inflect:=disk([-1/3,29/27], 0.03, color=blue):

> Cubic:=plot(f(x),x=-1..1,y=0..2,color=black,thickness=2,
title=`A cubic`):

> display(Cubic,Max,Min,Inflect);

[Plot]

Example 2: Drawing in asymptotes.

> restart;with(plots):with(plottools):

Warning, the name changecoords has been redefined

Warning, the assigned name arrow now has a global binding

> f := x -> (2*x+1)/(x-1);     

f := proc (x) options operator, arrow; (2*x+1)/(x-1) end proc

> solve(1/f(x)=0,x);       

1

> limit(f(x),x=infinity);   

> limit(f(x),x=-infinity);  

Of course, it is very easy to find these asymptotes by hand, or in your head. The line command, used below, is in plottools and draws a line connected two points. The option "linestyle=3" produces a dashed line.

> VA := line([1,-10], [1,10], color=red, linestyle=3):

> HA := line([-10,2], [10,2], color=red, linestyle=3):

> RatFunc:=plot(f(x),x=-10..10,y=-10..10,discont=true,
color=black,thickness=2):

> display(RatFunc,VA,HA);

Example 3: Another example.

> restart;with(plots):with(plottools):

Warning, the name changecoords has been redefined

Warning, the assigned name arrow now has a global binding

> f := x -> (exp(x)+2*exp(-x))/(2*exp(x)+exp(-x));

First we compute the limits. You should check these by hand.

> limit(f(x),x=infinity);    

1/2

> limit(f(x),x=-infinity);       

2

Next we look for critical numbers. First we look for places where f'(x) = 0.

> Df:= x -> diff(f(x),x);

Df := proc (x) options operator, arrow; diff(f(x), x) end proc

> solve(Df(x)=0);

>

When Malpe does not respond, there are no solutions. In order to see if there are places where f'(x) is undefined we need to see f'(x).

> Df(x);       

This is a bit messy.

> simplify(Df(x));        

It is clear that f'(x) is always defined but never equa to zero. Next we look for inflection points. It is obvious that f''(x) is always defined. Why? Thus we just look for solutions to f''(x)=0. As it truns out there is only one inflection point and below I save it as  Xinflect.

> Xinflect:=solve(diff(f(x),x$2)=0);

Xinflect := -1/2*ln(2)

> Yinflect:=f(Xinflect);

Yinflect := 5/4

Thus, there are no extrema and only one inflection point.

> Inflect:=disk([Xinflect,Yinflect], 0.1 ,color=blue):

> HA1:=line([-4,1/2],[4,1/2], color=red, linestyle=3):

> HA2:=line([-4,2],[4,2], color=red, linestyle=3):

> ExpFunc:=plot(f(x),x=-4..4,y=0..3,color=black,thickness=2):

> display(ExpFunc,HA1,HA2,Inflect);

[Plot]

>

Example 4: Dividing into sections.

> restart;with(plots):with(plottools):

Warning, the name changecoords has been redefined

Warning, the assigned name arrow now has a global binding

Plot the function f(x)=(x^2-4)/(x^4+1) . Label the extrema and inflection points. You should do a quick sketch by hand. Notice the denominator is never zero. The function is zero at x = 2 and -2. Finally the limit as x goes to +infinity or -infinity is clearly zero.  

> f:= x -> (x^2-4)/(x^4+1);      

STEP 1: We do a first plot to get a feel for the function.

> plot(f(x),x=-5..5,y=-5..5,color=black,thickness=2);

This looks different than our hand plot in class. We may need to plot it in sections. After some experimenting I came up with this. I plotted the function from [-10,-2] and restricted the y range. The plot over [2,10] will be the mirror image since the function is even.

> plot(f(x),x=-10..-2,y=0..0.1,color=black,thickness=2);

STEP 2: Find the extrema.

> diff(f(x),x);

> simplify(%); (The % sign says to insert the output of the command last executed.)

>

>

So one zero of f'(x) is at x=0, and f(0)=4. For the others we solve x^4-8x^2-1 = 0.

> solve(x^4-8*x^2-1=0,x);

Two of these are actually complex numbers. We ignore these. Since the function is even we only need to compute f at one of the two real solutions.

> f(sqrt(4+sqrt(17)));

Let's convert this to decimal form.

> evalf(%);

STEP 3: We compute f''(x).

> simplify(diff(f(x),x$2));

The solve command cannot handle this function in the mannor we want: it will give a messy formula for finding all complex roots. (Try it.) Instead we use fsolve , a floating-point solver. Let a=x^2.

> fsolve(3*a^4-12*a^2+1-40*a^3+24*a,a,real);

-.8862623902, -0.4094323555e-1, .6762337382, 13.58430522

Two of these are negative. We only want the square roots of the positive ones.

> sqrt(.6762337382); sqrt(13.58430522);

.8223343226

3.685689246

Thus, we have four inflection points (plus and minus these two numbers).

STEP 4: We put it all together a make a nice plot (in sections).

> Section1:=plot(f(x), x=-10..-2, y=0..0.1,  color=black,thickness=2):

> Section2:=plot(f(x), x=-2..2,   y=-5..0.1, color=black,thickness=2):

> Section3:=plot(f(x), x=2..10,   y=0..0.1,  color=black,thickness=2):

> Max1:=ellipse([-sqrt(4+sqrt(17)),f(sqrt(4+sqrt(17)))],0.07,0.002,filled=true ,color=green): (I am using the ellipse command to overcome a scaling distortion problem.)

>

> Min:=disk([0,-4],0.07,color=green):

> Max2:=ellipse([sqrt(4+sqrt(17)),f(sqrt(4+sqrt(17)))], 0.07,0.002,filled=true ,color=green):

> Inflect1:=ellipse([-3.685689246,f(-3.685689246)], 0.07, 0.002, filled=true ,color=blue):

> Inflect2:=disk([-.8223343226,f(-.8223343226)], 0.07 ,color=blue):

> Inflect3:=disk([0.8223343226,f(.8223343226) ], 0.07, color=blue):

> Inflect4:=ellipse([3.685689246, f(3.685689246)], 0.07, 0.002, filled=true, color=blue):

> display(Section1,Max1,Inflect1);

> display(Section2,Min,Inflect2,Inflect3);

> display(Section3,Max2,Inflect4);

>

We are done. But notice that it looks as though the plots would not match up smoothly at -2 and +2. Explain why they do.

Homework Problems.

Problem 1:
Plot the function f(x) = x sqrt(1-x^2) over its domain. Find and mark the local extreme and the inflection point.

Problem 2:
Graph f(x)=x^3-2x^2 over [-2,2]. Find the line tangent to this curve when x=-1. Overlay the graph of this tangent line with your graph of f(x).

Problem 3:
Let f(x) = (x+1)/((x-1)*(x-3)). Plot f(x), drawing in the vertical asymptotes and marking the local extrema and any inflection points.

Problem 4:

Let f(x) = sin(x) + cx, where c>0.  Graph several examples using different values of c.  For what values of c will f(x) fail to have loacl extrema? What can you say about the inflection points of f(x) ?