We need to go one step further to turn this implementation into something that
is generally useful. In the past, we have packaged complicated
expressions into functions with simple interfaces. Now, we need to
package this complicated sequence of statements into something resembling
a function. Maple provides procedures for this purpose.
A procedure is like a function in that it takes zero or more parameters and
returns a value. It is different in that whereas the body of a function must
be an expression, the body of a procedure can be a sequence of statements.
If we define a procedure that implements the bisection method, what should it
take as parameters?
Click here for the answer
Here, then, is a definition of a bisection procedure:
| bisection := proc (function, positive, negative)
local f, pos, neg, ave, val;
f := function;
pos := positive;
neg := negative;
while (abs(pos - neg) >= 1e-6) do
ave := (pos + neg) / 2.0;
val := f(ave);
if (val < 0)
then neg := ave;
else pos := ave;
fi;
od;
RETURN(ave);
end; |
Now that this procedure is defined, we can call it with a variety of arguments:
| bisection(x -> cos(x) - x, 0, 1); |
| bisection(x -> cos(x) - sin(x), 0, 1); |
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah