The FROOT function finds zeros of the univariate function “fun” by using Brent’s numerical root-finding method (Brent, 1973; Moler, 2004). Brent’s method uses a combination of bisection, linear interpolation, and quadratic interpolation to converge to a root when given an interval in which the function changes signs.
The arguments are as follows:
is the name of a SAS/IML function module. The module defines the function whose roots you want to compute. The module takes one argument and returns a scalar value. You can use a GLOBAL statement to pass parameters to “fun”.
is an matrix. Each row of bounds specifies an interval in which the function changes sign. This implies that there is a root inside the interval. The return value of FROOT is an vector, where the th element contains the root in the interval bounds[i,].
is an optional vector that contains three elements. Each element specifies a parameter that controls the convergence of Brent’s algorithm. A missing value specifies that the algorithm should use the default parameter value. The parameters are as follows:
specifies the maximum number of iterations used to search for a root. The default value is 100.
specifies a tolerance that determines how close the computed root is to the true root. The default value is machine epsilon, which on many computers is approximately .
specifies a tolerance that determines how close the function at the computed root is to zero. The default value is machine epsilon.
Brent’s algorithm starts with an interval in which the function changes signs. At each step, the algorithm computes a smaller interval in which the function also changes signs. (If each interval is half the sign of the previous, this is the bisection method.) The algorithm stops when one of the following conditions is met:
The algorithm has performed opt[1] iterations.
The bounding interval is sufficiently small. If is the value of opt[2], then the algorithm stops when .
The function that is evaluated on the interval is sufficiently small. If is the value of opt[3], then the algorithm stops when .
The following program defines a cubic function that has three roots. The roots are contained in the intervals , , and , as shown by computing the function at the endpoints of these intervals and noticing that the function changes signs on each interval.
start Func(x); return( 2 - 3#x - 1#x##2 + x##3 ); finish; bounds = {-2 0, 0 1, 1 2 }; fBounds = Func(bounds[,1]) || Func(bounds[,2]); print bounds fBounds; roots = froot( "Func", bounds); print roots;
Figure 24.137: Bounding Intervals and Roots
bounds | fBounds | ||
---|---|---|---|
-2 | 0 | -4 | 2 |
0 | 1 | 2 | -1 |
1 | 2 | -1 | 0 |
roots |
---|
-1.618034 |
0.618034 |
2 |
The FROOT function returns a missing value if the search fails to return a root. Usually this indicates that the function does not change signs at the endpoints of the specified interval. For example, the following statement returns a missing value:
r = froot("Func", {3 4});