A Few of the Most Useful Things to Do With Maple
for Calculus One Students
Part I: Differential Calculus
by
Eric A. Carlen
The point of this worksheet is to explain how you can use a small and simple, but quite powerful collection of Maple commands analyze many of the problems you will run into in your Calculus One class.
Hopefully, you will find these commands very useful even if computer projects are not assigned in your section.
This is partly because you can use them to figure out if you've got the right answer to a homework problem, or a problem on a practice quiz for which the answers aren't provided. And figure out is the key phrase: In calculus, a picture really is woth a thousand words, and so good graphing tools that produce good "figures" are very, very helpful.
(1.) The basic plot command . Let's begin with a basic plot command. What you type is
plot(some expression ,x=left limit ..right limit,bottom limit..top limit);
The "some expression" is what you want to plot. Type it in using * for all multiplications, and ^ to put things in the exponent. Follow the "some expression" with a comma. Next type x=a..b where a and b are the left and right limitis of the region you want to plot. Notice the two periods that separate a and b. Follow this with a comma, and type c..d where c and d are the top and bottom limits of the region to be graphed. Add the closing parentheses, a final semicolon, and enter the expression by hitting the enter or the return key.
You will soon see your graph. Then you can modify the limits to get better information from it, and renter the function to get more information.
First, let's enter an expression: Click on the formula, and hit the enter or the return key:
> (1-(x/2)^(1/2))/(2-x);
You then see the function displayed in familiar notation -- so you can be sure you entered what you wanted to.
Here is our first example of using this to check a problem, one with limits.
Suppose that you are asked to find the limit of a certain function as x approaches 2. The function in question is the one entered above, namely:
(1-(x/2)^(1/2))/(2-x) .
Note that this involves a square root, which we typed by putting a (1/2) in the exponent with a ^ to put it there. We could just as well have written this same function as
(1-sqrt(x/2))/(2-x)
There are lots of equally good ways of doing many things in Maple. Just do what's natural for you!
Anyway, back to the problem. The function is undefined at x=2 because the denominator vanishes there, this might be tricky -- and indeed the problem is marked with a star in the book.
In chooosing where to graph it, we should pay attention to the domain of the function. We need x to be positive for the square root to be defined, so we had better choose an interval that contains only positive values of x for our graph.
Let's graph it from x=1 to x=3 so that x=2 is smack in the middle. Picking y limits is less clear, as a first stab, lets take the limits to be -1 and 1. Here goes:
> plot((1-(x/2)^(1/2))/(2-x),x=1..3,-1..1);
>
The first thing we see is that there will be a limit -- the function has a continuous graph at x=2. We can even see that the value is between 0.2 and 0.3. Let's zoom in and get a better look. Change the x limits to 1.9 and 2.1, and the y limits to 0.3 and 0.3 and redo the graph:
Regraphing, you see the that limit is about 0.25.
Having figured this much out, we've got a real leg up on solving the problem exactly. Evidently the zero in the denominator must cancel out with the zero in the numerator. What's the relation between the two? -- after all, now we know there is one!
After a bit of thought, we see that the numerator factors as a difference of squares:
2(1-x/2) = 2(1-(x/2)^(1/2))(1+(x/2)^(1/2))
and one of these factors cancels out the denominator. Thus our function can be simplified to
f(x) = 1/(2*(1+(x/2)^(1/2)))
which is -- as our graph suggested -- continuous at x=2. So the limit can be computed by evaluating at x=2. We can do this with Maple too -- it is great for plugging values into functions!
(2.) Plugging into functions -- the substitute command.
Now to our function at x=2, we use the subs command. Click on the following line, and enter it.
> subs(x=2,1/(2*(1+(x/2)^(1/2))));
And that is the answer to the problem!
(3.) Cleaning things up with definitions.
You've noticed that we had to do a lot of typing of formulas the way we used the plot and subs command up above. Look at all the right parentheses in a row in the last expression! Things like that are troublesome to type. It would obviously be much beeter if we could type in such a formula once and for all, ans name it,
and then just use the name in our various commands -- instead of having to type it all over again in each new command.
And of course there is a way ! This is especially important since you can't copy and paste into formulas in Maple.
Here is how you do it. Pick a name for your function -- like f, say.
Then type
> f := 1/(2*(1+(x/2)^(1/2)));
Don't forget the colon!!! You don't define anything if you just type = and not := .
Unfortunately, when you enter the expression, Maple still prints back your function in readable form, so you may think you have registered the function with the name, but you will have trouble when you try to use the name.
Click on the line with the definition, and hit the return key to enter the definition. Notice that Maple prints back the function in readable form so you can see that you coded in the right thing. Go back and edit out one of the parentheses, and re-enter the definition in this incorrect form. This is a common error. What happens?
Now that we have the function entered, it is easy to use subs and easy to use plot: Try out the following:
> subs(x=2,f);
> plot(f,x=1..3,-1..1);
This is much better, no?
Putting it all together in another example.
Just to cement things, lets change the problem by sqauring the denominator. This time, lets put
> g := (1-(x/2)^(1/2))/(2-x)^2;
Now let's plot it (don't forget to enter the definition first!):
> plot(g,x=1..3,-2..2);
Clearly, this time the limit doesn't exist! In fact we see that the limit from above is minus infinity, and the limit from below id plus infinity.
The thing to notice is that we got our answer in exact form. not some decimal approximation. We didn't need to use "answer" on the left side; any name would do. But now that we've got it, we can use this name to get the decimal approximation: To do this just use the evalf( ) command -- this is short for evaluate in floating point decimal form.
(4.) Computing derivatives, and use of the simplify command.
It is easy to use Maple to compute derivatives. The first step is to pick a name. We will use our same function f, so I'll call it's derivative fprime:
> fprime := diff(f,x);
That's all there is to it! However, it might look better with a common denomonator. We can simplify this using the simplify command:
> simplify(fprime);
Let's put this to work for us. Problem 20 on page 178 of Grossman's text is to compute the derivative of
> h:= sqrt(1-sqrt(1-sqrt(x)));
Actually, the book calls the function y. That would get us in trouble below when we plot it though! Don't use y as a function name if you intend to plot it, since y is also a plot command argument. And you should plot a lot!
Also, notice that we used the square root function sqrt( ) instead of an exponent ^(1/2) as before: There are many eqaully good ways to do many things! Now let's differentiate it:
> hprime :=diff(h,x);
> simplify(hprime);
Sorry -- that's as simple as it gets! That's the answer to this problem. Let's compute the second derivative. first pick a name. I'll pick f2prime. Then just do what you did before:
> h2prime := diff(hprime,x);
This time, some simplification is possible.
> simplify(h2prime);
(5.) More on plotting: Two functions together. Now we learn some more about plotting. The first thing is that we don't have to retype the function in the plot command if it has a name -- you can just use the name! Let's plot our old function f, defined above, this way, on say 0 to 3:
> plot(f,x=0..3,y=0..3);
You can also leave out the range on the y values and let Maple guess what they should be:
> plot(f,x=0..3);
Now let's plot f and its derivative, fprime, together . To do this list them in sqaure brackets
separated by commas:
> plot([f,fprime],x=0..3);
You can see here that there is a solution of fprime = 0 at about x = 0.32, which is right under the maximum value of f. Notice the first functione entered is plotted in red and the next in green.
It is useful to plot two functions together if you want to solve an eqaution of the form f(x) = g(x): The solutions are where the graphs cross.
It can also be helpful to plot a function together with its derivatives.
Let's do this with the function h that we worked with above, its first derivative, and its second derivative.
Notice that h is only defined for x in the range 0 to 1, so that is where we will plot it:
> plot([h,hprime,h2prime],x=0..1);
Here you see why it helps to specify the y values. Maple doesn't always make good guesses. It tries to fit too much in. Lets cut things down to size. One way to do this would be to specify the range of the y values.
> plot([h,hprime,h2prime],x=0..1, -5..5);
We can see from this graph the the first derivative -- which is graphed in green -- is positive everywhere,
so this function has no critical points. However, at x= 0.6 the second derivative seems to be equal to zero.
Places where the second deivative vanishes are called inflection points. Let's see if we've found one.
First, let's substitute in this value:
> subs(x=0.6,h2prime);
Well, not quite. It is still negative there. Let's try a bit to the right:
> subs(x=0.62,h2prime);
That's better -- this is positive, so the graph of y2prime crosses the x axis somewhere in between
x = 0.6 and 0.62. So try 0.61:
> subs(x=0.61,h2prime);
Now we are pretty close -- and the right answer is just a bit more to the right. But is there a better
way than trial and error? Yes! Using the solve command.
(6.) Using the solve command to solve equations, and the evalf command to get decimal form answers.
One of the things you will be doing a lot if is solving equations. Maple can help you cut through the algebra!
You just need to use the solve command.
This command is used by typing solve (eqaution, variable) . We will continue with the example above,
and solve for the values of x at which the second derivative of h is equal to zero. That is, we
will find the "inflection points". More on the name later.
Anyway, to solve the equation
y2prime=0 with x as our variable, we enter:
> solve(h2prime=0,x);
Wow!This is the exact answer. Try to do that by hand!
Having the exact answer is nice, but often we want our answers in decimal form. As it is,
it may not be appearant to you whether this result is bigger or smaller than 1. We know from our
trial and error above that it should be about 0.61.
To get the answer in decimal form with lots of accuracy, use evalf(%) -- that's a double quote mark inside the parentheses:
> evalf(%);
A few things about this command. The "f" in evalf is there for "floating point". The name of the command is
a short form of "evaluate in floating point format"; i.e., in decimal format.
The quote mark trick only works if the number you want in decimal form is on the line
just above (text not included). If you don't have the theing you want to evaluate right up above,
you must put it, or its name (if it has one regiatered) in the command in place of the double quote:
> evalf(sqrt(7));
If you leave off the f, and just use eval(something), you get the exact evaluation:
> eval(1/3+1/7);
However, the star of this section is the solve command. So let's get back to it.
Let's see what else can happen when we use solve( ). We'll also use the percent mark trick to get a
long polynomial eqautions into solve:
> x^5 + 2*x^4 -6*x -3 =0;
> solve(%, x);
Too bad -- well, there is no formula for solving fifth degree polynomials, so we might have guessed we were asking too much. Let's try to plot this to see what's going on:
> plot(x^5+2*x^4-6*x-3,x=-3..3);
As you can see, there are three real roots. How can we find them?
(7.) Numerical solution of equations -- Newton's method.
Maple has an implementaion of Newton's method built in,
and you invoke it with the fsolve command. ("f" for floating point once again.) You can use this just like solve:
> fsolve(x^5 + 2*x^4 - 6*x -3 =0, x);
There are our three roots, right where the graph says they should be! Let's try another example.
Consider the following problem: Let's find all solutions of
(x^4 + x^2 + 1)^(1/2) = x+2
> plot([(x^4+x^2+1)^(1/2),x+2],x=-2..2);
We can see there are two solutions, one near x=-1 and one near x=2. Let's write the problem in
standard eqaution form
(x^4 + x^2 + 1)^(1/2) - x - 2 = 0
and use fsolve:
> fsolve((x^4+x^2+1)^(1/2)-x-2,x);
Notice this time we only got one solution!
This often happens, especially when the equaition is
not simply a polynomial equation. (Maple usually, but not always, finds all real roots for those.)
This shows once again why it is so important to graph.
Now that we know that there is another solution, how do we find it? You can specify a range in which Maple should look -- just put in a..b after the variable (and a comma too of course). In the problem above,
let's look between -1 and 0, since the graph tells us that there is another root there .
> fsolve((x^4+x^2+1)^(1/2)-x-2,x,-1..0);
There is our second root! Now you know how to solve pretty much any single variable eqaution you'll
run into in Calculus classes. One more thing that's useful along this line though: Maple is pretty good at factoring polynomials:
(7.) Factoring polynomials.
Let's try one out:
> polynomial := x^6+2*x^4-3*x^2 -4;
> factor(polynomial);
(8.) Plotting Equations -- as opposed to plotting functions.
So far we've been plotting functions. What about plotting an equation
like (x^2+y^2)^2 = 2*(x^2-y^2) ? Since we can't solve for y, what do we do?
The answer is to write the equation for the curve in implicit form : f(x,y)= 0. This is called implicit
because it implicitly specifies the y values that go with some x -- the ones that make the equation true -- instead of giving a
fomula for them. That would be explicit.
To plot equations, i.e., curves given in implicit form, we need to load the plots package. you
do this by typing with(plots);
> with(plots);
Warning, the name changecoords has been redefined
As you can see, this package does does a lot -- we just produced a list of the capabilities we've loaded
into memory. Here, we'll just use implicitplot and polarplot .
To use implicitplot, you enter an equation, the x range of the graph, the y range, and a gridsize.
This last item determines how fine a grid Maple uses when drawing a graph. Here is our example:
> implicitplot((x^2+y^2)^2-2*(x^2-y^2)=0,x=-2..2,y=-2..2,grid=[100,100]);
Notice it changes the scale, stretching the figure to make it fit a sqaure. To draw it so the proportions are kept,
stick the command scaling=constrained in at the end. This gives:
> implicitplot((x^2+y^2)^2-2*(x^2-y^2)=0,x=-2..2,y=-2..2,grid=[100,100],scaling=constrained);
This curve -- the infinity symbol -- is called the Bernoulli lemiscate. A standard implicit differentiation
problem is to find all of the points at which the tangent to this curve is horizontal. You can see that there
are exactly four of them, and you can read their approximate values off the graph, thus checking your work.