Math 112, section 9, 6 Mar 2003
Here are a few examples using Maple to calculate results using Newton's Method
These examples use the NewtonsMethod function in Maple 8. You could, however, write your own function to do Newtons method in any version of Maple.
First, we need to load the NewtonsMethod function from the Student[Calculus1] package
> | with(Student[Calculus1]); |
We will do the example in the book: find a root to cos(x)-x^2. The first argument to NewtonsMethod is the function, followed by an initial guess for x. Then any options are specified. Valid output options are "plot", "sequence", and "value". You can specify the number of iterations with the iterations option. There are many more options, which you can find in the help for the function. You can see the help for the function by typing ?NewtonsMethod
First, we will assign the function to the variable f. This will save us having to type the function each time we want to use it.
> | f:=cos(x)-x^2; |
To find an initial guess, we plot the function. The domain of the plot is specified in the form x=a..b (notice the two periods between a and b).
> | plot(f,x=-10..10); |
It looks like the function has roots between -2 and 2. Let's plot that area of the function.
> | plot(f,x=-2..2); |
Okay, it seems like a good guess for an initial x value might be around .8. Let's try NewtonsMethod with graphical output to see what is going on.
> | NewtonsMethod(f,x=.8,output=plot); |
It looks like we converge right onto the root. Let's use the sequence output to see all the different x's we received.
> | NewtonsMethod(f,x=.8,output=sequence); |
That looks great! Let's try using a few more iterations to see if we can get a tighter bound on the root.
> | NewtonsMethod(f,x=.8,output=sequence,iterations=20); |
It looks like that's as good as we're going to get. Let's see what Maple says the actual solution is.
> | solve(f,x); |
Hmm...that's not too intuitive. No wonder we couldn't solve it! :) Let's get a decimal approximation for it.
> | evalf(solve(f,x)); |
Don't worry about the 0.I, that just means there is no imaginary part to the solution, so it is a real solution. It looks like that's what we also obtained from the NewtonsMethod function.
Of course, we could have run into problems if we would have picked our first x to be 0 (why?). Let's see what Maple will do anyway.
> | NewtonsMethod(f,x=0,output=plot); |
> |
> |
Aha! Just as we expected, the function has a zero derivative at x=0. So much for finding the root of the tangent line there!
> |
Thanks for exploring the wonderful world of Maple! Have fun, but don't hurt (or hang) yourself!
(p.s., the command for finding the derivative of a function is "diff", and the command for limits is "limit". You can find the help on each of these by prefixing the command with a question mark (i.e., ?diff).