]> NewtonsMethod.html

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]);

[AntiderivativePlot, ApproximateInt, ArcLength, Asymptotes, Clear, CriticalPoints, DerivativePlot, ExtremePoints, FunctionAverage, FunctionChart, GetMessage, GetNumProblems, GetProblem, Hint, Inflectio...
[AntiderivativePlot, ApproximateInt, ArcLength, Asymptotes, Clear, CriticalPoints, DerivativePlot, ExtremePoints, FunctionAverage, FunctionChart, GetMessage, GetNumProblems, GetProblem, Hint, Inflectio...
[AntiderivativePlot, ApproximateInt, ArcLength, Asymptotes, Clear, CriticalPoints, DerivativePlot, ExtremePoints, FunctionAverage, FunctionChart, GetMessage, GetNumProblems, GetProblem, Hint, Inflectio...
[AntiderivativePlot, ApproximateInt, ArcLength, Asymptotes, Clear, CriticalPoints, DerivativePlot, ExtremePoints, FunctionAverage, FunctionChart, GetMessage, GetNumProblems, GetProblem, Hint, Inflectio...

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;

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);

[Maple Plot]

It looks like the function has roots between -2 and 2.  Let's plot that area of the function.

>    plot(f,x=-2..2);

[Maple Plot]

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);

[Maple 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);

.8, .8244704340, .8241323766, .8241323123

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);

.8, .8244704340, .8241323766, .8241323123

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);

RootOf(-cos(_Z)+_Z^2,label = _L1)

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));

.8241323123+0.*I

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);

>   

>   

[Maple 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).