The last thing that we must do in each repetition of our root-finding
implementation is decide which of the following two assignments to evaluate:
neg := ave;
pos := ave;
It is easy to ask Maple to make the choice for us by using a conditional
statement. Let's rewrite our implementation with a conditional.
We'll begin as before by initializing f, pos, and neg.
| f := x -> cos(x) - x; |
| pos := 0.0; |
| neg := 1.0; |
Again as before, we'll compute the values of ave and val.
| ave := (pos + neg) / 2.0; |
| val := f(ave); |
Here's where we'll use a conditional. Let's ask Maple to choose one or the
other of the assignments based upon the sign of val:
| if (val < 0)
then neg := ave;
else pos := ave;
fi; |
You can continue the process of converging to a root by repeatedly evaluating
the last three statements. The nice thing about this implementation is that
you don't have to make any decisions as to which assignment statement to
evaluate--Maple's conditional statement takes care of that for you.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah