flag = Zero(f, &b, &c, abserr, relerr, &residual);
in C. In FORTRAN F, or f in C and C++, is the name of the function subprogram for evaluating F(x). In FORTRAN it must be declared in an EXTERNAL statement in the program that calls ZERO. Normal input consists of a continuous function F(x) and values B and C such that F(B)F(C) < 0. Both B and C are also output quantities, so they must be variables in the calling program. On output it is always the case that |F(B)| < |F(C)|
The code attempts to bracket a root between B and C, with B being the better approximation, so that the convergence test
is satisfied. It makes no sense to allow RELERR < u, the unit roundoff of the com- puter used, because this is asking for a more accurate result than the correctly rounded true result. To provide a little protection near limiting precision, it is required that RELERR > 10u. If the desired root should be zero, or very close to zero, a pure relative error test is not appropriate. For this reason it is required that ABSERR > 0.
Normal output has either F(B)F(C) < 0 and the convergence test met, or F(B) = 0. This is signaled by FLAG = 0. At most 500 evaluations of F are allowed. If more appear to be necessary, FLAG is set to 1 and the code terminates before the convergence test is satisfied. The value FLAG = -1 indicates invalid input, that is, ABSERR < 0 or RELERR < 10u, and FLAG = -2 means F(B)F(C) > 0. The value RESIDL (or residual in C and C++) is the final residual F(B). Convergence is judged by the length of an interval known to contain a root. The algorithm is so robust that it can locate roots of functions that are only piecewise continuous. If it is applied to a function that has a pole of odd multiplicity, it might locate a pole rather than a root. This is recognized by a “large” residual and signaled by FLAG = 2.
Example 4.4. The function F(x) = e-x - 2x has F(0) > 0 and F(1) < 0, hence the equation F(x) = 0 has a root between C = 0 and B = 1. The sample program provided illustrates the use of the zero-finding routine. Note that a globally defined integer is used to count the number of F evaluations required. In FORTRAN this is accom- plished via a COMMON statement (preferably labeled COMMON) and in C or C++ it is done by an appropriate placement of the variable declarations. The output is
Example 4.5. In Chapter 5 we discuss a problem that requires the solution of
for its smallest positive root. The particular values of the parameters used there are x = y = z = 50 and a = l, b = 2, c= 100. Figure 4.5 is a rough sketch of φ for a2
<
b2 < c2. It is drawn by looking at the behavior of φ as λ and
The portion of interest to us is between -a2 and Since as from the
right, φ(λ) and since as the continuous function φ must
have a zero larger than -a2. Differentiating φ(λ) we get
Because φ(λ) is strictly decreasing, there is only one zero λ0 greater than -a2. The root is simple since
Interestingly, φ(λ) can be scaled to a cubic polynomial
This allows us to apply some bounds on the relative error of an approximate zero of a polynomial developed in Exercise 4.31.
The equation φ(λ) = 0 was solved using the code Zero with relative and absolute error requests of 10-6 and 10-8, respectively. Poor initial values of B and C were used to show the excellent rate of convergence. Table 4.1 displays successive values of B and C and tells which method was used by Zero in computing B.
Table 4.1. Solution of φ(λ) = 0 by Zero
For the error bounds in Exercise 4.31, we computed P(B) = 1.6261594 × 104 and
P´(B) = 8.5162684 × 107. The constant term in P(λ) is a0 = x
2
b2c2 - y2a2c2 - z2a2b2 + a2b2c2, which in this case is - 1.2497000 × 108. The bounds state that there is a root rj of P such that
and a root ri such that
The second error bound is quite good, but the first is pessimistic. Generally we do not know which of the two bounds will be better. An interesting point here is the size of the residual P(B). The value B is supposed to be a root of φ(λ) = 0 and P(λ) = 0. If its quality were judged by the size of the residual, B might be thought a poor
4.3 ROUTINES FOR ZERO FINDING 155
Figure 4.5 φ(λ) for Example 4.5.
approximation when solving the one equation and a good approximation when solving the other. This is despite the fact that it approximates the same root in both cases. To be more concrete, MATHCAD’S default convergence test that the residual be smaller
in magnitude than 10-3 would reject an approximate root of P(λ) = 0 that is actually quite accurate. This problem illustrates well the issue of scaling. The convergence test in Zero and in similar codes is reliable. The magnitude of the residual reported by the code is important only for detecting the convergence to a pole that is possible when the function input is not continuous. Otherwise its size is a statement about the scaling
of the problem, not the quality of the approximate zero. n
EXERCISES
Unless otherwise indicated, use ABSERR = 10-8 and
(g) RELERR = 10-6 for the computations with Zero.
4.10 Use the code Zero with an initial bracket of [0, l] to (h) (x - 3)(x + 1)
find the roots of the equation F(x) = 0, where F(x) is Print out the approximate roots, FLAG, the number of given by each of the following. function evaluations required, and the residual. Dis-
(a) cos 2x cuss the results. [Sketches of F(x) will be helpful.]
(b) (x - 0.3)(x - 0.6) 4.11 A wire weighs 0.518 lb/ft and is suspended between (c) (x - 0.3)(x - 0.6)(x - 0.9) two towers of equal height (at the same level) and500 ft apart. If the sag in the wire is 50 ft, find (d) (x + 1)(x - 0.8)7
(e) (x + l)[x7 - 7(0.8)x6 + 21(O.8)2x5
- 35(0.8)3x4 + 35(0.8)4x3 - 21(0.8)5x2
+ 7(0.8)6x - (0.8)7] (f) l / cos 2x
the maximum tension T in the wire. The appropriate equations to be solved are
4.12 For turbulent flow of fluid in a smooth pipe, the equa- 4.17 In trying to solve the equations of radiative transfer in
tion semi-infinite atmospheres, one encounters the nonlin-
ear equation models the relationship between the friction factor cf
and the Reynold’s number Re. Compute cf for Re =
104, 105, 106. Solve for all values of the Reynold’s where the number ω0, 0 < ω0 < 1, is called an albedo.
number in the same run. Do this by communicating Show that for fixed ω0, if k is a root, so is -k, and that
the parameter Re to the function subprogram using la- there is a unique value of k with 0 < k < 1 satisfying beled COMMON in FORTRAN or a globally defined the equation. For ω0 = 0.25, 0.50, and 0.75, find the
variable in C or C++. corresponding positive k values. Make some sketches
4.13 In [12] the study of neutron transport in a rod leads to to help you locate the roots.
a transcendental equation that has roots related to the 4.18 Exercise 5.27 concerns a temperature distribution critical lengths. For a rod of length the equation is problem where it is necessary to find positive roots
of
Make a rough sketch of the two functions cot(&) and (x2 - l)/(2x) to get an idea of where they intersect to
yield roots. For = 1, determine the three smallest positive roots.
4.14 An equation determining the critical load for columns with batten plates is derived in [9, p. 151]. Suitable values of the physical parameters for experiments per- formed by Timoshenko lead to the problem
and the smallest positive root is desired. Make a rough sketch of the function to get an idea of where the root is. Scale to avoid difficulties with poles and the appar- ent singularity at 0, and then compute the root. 4.15 An equation for the temperature T at which o-
toluidine has a vapor pressure of 500 mm Hg is found in [8, p. 424]. In degrees absolute T satisfies
It is not obvious where the roots are, but a little anal- ysis will help you locate them. Using brackets from your analysis, compute all the roots.
4.16 The geometrical concentration factor C in a certain so- lar energy collection model [10, p. 33] satisfies
Rescale the problem to avoid poles. Find the small- est positive root A if h = 300, C = 1200, f = 0.8, and
D = 14.
where J0(x) and J1(x) are zeroth and first order Bessel
functions of the first kind. Compute the three smallest positive roots.
4.19 An analysis of the Schrodinger equation for a parti- cle of mass m in a rectangular potential well leads to discrete sets of values of the total energy E that are solutions of a pair of transcendental equations. One of these equations is
where
is Planck’s constant. Find the value of E that satisfies this equation. Use the following data, which corre- spond to a simplified model of the hydrogen atom:
On some machines it may be necessary to scale some of the variables to avoid underflow. Also, be careful with your choice of ABSERR if you want an accurate answer.
4.20 The following problem concerns the cooling of a sphere. Suppose the sphere is of radius a and is ini- tially at a temperature V. It cools by Newton’s law of cooling with thermal conductivity k, thalpance ε, and diffusivity h2 after being suddenly placed in air
4.4 CONDITION, LIMITING PRECISION, AND MULTIPLE ROOTS 157