Let's automate the bisection method by implementing it in Maple. For
completeness, we will redefine the function:
| f := x -> cos(x) - x; |
Next we will define variables pos and neg that bracket the
root from above and below:
| pos := 0.0; |
| neg := 1.0; |
Next we compute the average of pos and neg and compute the value of
f(ave):
| ave := (pos + neg) / 2.0; |
| val := f(ave); |
Here's where we have to make a choice. If val had been negative, we
would have changed the value of neg:
| #neg := ave; |
But since it was positive, we instead want to change the value of pos:
| pos := ave; |
Notice that we commented out the assignment to neg so that it wouldn't
actually happen.
At this point, we have completed one pass through the root-finding process.
You can continue by using the mouse to put the cursor back into the statement
that assigns a value to ave. Let Maple evaluate that and the following
statement once again. Finally, depending upon whether val is negative or
positive, evaluate one of the next two statements. (The first time that
you evaluate the assignment to neg, you'll need to remove the comment
character.)
If you continue in this fashion, evaluating the assignment to ave, the
assignment to val, and the assignment to neg or pos as
appropriate, pos, neg, and ave will all gradually converge
to the solution to the equation, and val will gradually approach zero.
When you think you've gotten close, verify this by evaluating
| f(ave); |
We could use this Maple implementation to find the root of any function, but it
leaves a few things to be desired. What are some annoying things about using
this implementation?
Click here for the answer
In the remainder of this lesson, we will address these three complaints in
turn.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah