Roots of equations by interpolation.
A sample call is: INTERPOLATE(SIN(X)-X/2,X,.1,%PI);
This finds the zero of SIN(X)-X/2 as X varies. The last two args give the
range to look in. The function must have a different sign at each endpoint.
If this condition is not met, the action of the of the function is governed
by INTPOLERROR (default value TRUE). If INTPOLERROR is TRUE then an error
occurs, otherwise the value of INTPOLERROR is returned (thus for plotting
INTPOLERROR might be set to 0.0). Otherwise (given that MACSYMA can
evaluate the first argument in the specified range, and that it is
continuous) interpolate is guaranteed to come up with the zero (or one of
them if there is more than one zero).
The accuracy of INTERPOLATE is governed by INTPOLABS and INTPOLREL which must
be non-negative floating point numbers. INTERPOLATE will stop when the
first arg evaluates to something less than or equal to INTPOLABS or is
successive approximants to the root differ by no more than INTPOLREL *
<one of the approximants>. The defaults values of INTPOLABS and INTPOLREL
are 0.0 so INTERPOLATE gets as good an answer as is possible with the single
precision arithmetic we have. The first arg may be an equation. The order
of the last two args is irrelevant. Thus
would also work.
The method used is a binary search in the range specified by the last two
args. When it thinks the function is close enough to being linear, it
starts using linear interpolation.
An alternative syntax has been added to interpolate, this replaces the first
two arguments by a function name.
INTERPOLATE(SIN(X)-X/2,X,0.1,%PI) TIME= 60 MSEC
INTERPOLATE(F(X),X,0.1,%PI); TIME= 68 MSEC
INTERPOLATE(F,0.1,%PI); TIME= 60 MSEC
INTERPOLATE(F(X),X,0.1,%PI); TIME= 26 MSEC
INTERPOLATE(F,0.1,%PI); TIME= 5 MSEC
For further improvement one can COMPILE(), which creates a LISP
and FASL file of functions which are then loaded into macsyma. See
the documentation on COMPILE.
Comments, questions etc. to CFFK@MC