c05 Chapter Contents
c05 Chapter Introduction
NAG C Library Manual

NAG Library Function Documentnag_zero_cont_func_bd_1 (c05sdc)

1  Purpose

nag_zero_cont_func_bd_1 (c05sdc) locates a zero of a continuous function in a given interval by a combination of the methods of linear interpolation, extrapolation and bisection.

2  Specification

 #include #include
void  nag_zero_cont_func_bd_1 (double a, double b, double *x,
 double (*f)(double x, Nag_User *comm),
double xtol, double ftol, Nag_User *comm, NagError *fail)

3  Description

nag_zero_cont_func_bd_1 (c05sdc) attempts to obtain an approximation to a simple zero of the function $f\left(x\right)$ given an initial interval $\left[a,b\right]$ such that $f\left(a\right)×f\left(b\right)\le 0$. The zero is found by a modified version of procedure ‘zeroin’ given by Bus and Dekker (1975). The approximation $x$ to the zero $\alpha$ is determined so that one or both of the following criteria are satisfied:
 (i) $\left|x-\alpha \right|<{\mathbf{xtol}}$, (ii) $\left|f\left(x\right)\right|<{\mathbf{ftol}}$.
The function combines the methods of bisection, linear interpolation and linear extrapolation (see Dahlquist and Björck (1974)), to find a sequence of sub-intervals of the initial interval such that the final interval $\left[x,y\right]$ contains the zero and is small enough to satisfy the tolerance specified by xtol. Note that, since the intervals $\left[x,y\right]$ are determined only so that they contain a change of sign of $f$, it is possible that the final interval may contain a discontinuity or a pole of $f$ (violating the requirement that $f$ be continuous). If the sign change is likely to correspond to a pole of $f$ then the function gives an error return.

4  References

Bus J C P and Dekker T J (1975) Two efficient algorithms with guaranteed convergence for finding a zero of a function ACM Trans. Math. Software 1 330–345
Dahlquist G and Björck Å (1974) Numerical Methods Prentice–Hall

5  Arguments

On entry: the lower bound of the interval, $a$.
2:     bdoubleInput
On entry: the upper bound of the interval, $b$.
Constraint: ${\mathbf{b}}\ne {\mathbf{a}}$.
3:     xdouble *Output
On exit: the approximation to the zero.
4:     ffunction, supplied by the userExternal Function
f must evaluate the function $f$ whose zero is to be determined.
The specification of f is:
 double f (double x, Nag_User *comm)
1:     xdoubleInput
On entry: the point $x$ at which the function must be evaluated.
2:     commNag_User *
Pointer to a structure of type Nag_User with the following member:
pPointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ should be cast to the required type, e.g., struct user *s = (struct user *)comm → p, to obtain the original object's address with appropriate type. (See the argument comm below.)
5:     xtoldoubleInput
On entry: the absolute tolerance to which the zero is required (see Section 3).
Constraint: ${\mathbf{xtol}}>0.0$.
6:     ftoldoubleInput
On entry: a value such that if $\left|f\left(x\right)\right|<{\mathbf{ftol}}$, $x$ is accepted as the zero. ftol may be specified as 0.0 (see Section 8).
7:     commNag_User *
Pointer to a structure of type Nag_User with the following member:
pPointer
On entry/exit: the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$, of type Pointer, allows you to communicate information to and from f(). You must declare an object of the required type, e.g., a structure, and its address assigned to the pointer $\mathbf{comm}\mathbf{\to }\mathbf{p}$ by means of a cast to Pointer in the calling program, e.g., comm.p = (Pointer)&s. The type pointer will be void * with a C compiler that defines void * and char * otherwise.
8:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_2_REAL_ARG_EQ
On entry, ${\mathbf{a}}=〈\mathit{\text{value}}〉$ while ${\mathbf{b}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{a}}\ne {\mathbf{b}}$.
NE_FUNC_END_VAL
On entry, f($〈\mathit{\text{value}}〉$) and f($〈\mathit{\text{value}}〉$) have the same sign, with f($〈\mathit{\text{value}}〉$) $\ne 0.0$.
NE_PROBABLE_POLE
Indicates that the function values in the interval $\left({\mathbf{a}},{\mathbf{b}}\right)$ might contain a pole rather than a zero. Reducing xtol may help in distinguishing between a pole and a zero.
NE_REAL_ARG_LE
On entry, xtol must not be less than or equal to 0.0: ${\mathbf{xtol}}=〈\mathit{\text{value}}〉$.
NE_XTOL_TOO_SMALL
No further improvement in the solution is possible. xtol is too small: ${\mathbf{xtol}}=〈\mathit{\text{value}}〉$.

7  Accuracy

This depends on the value of xtol and ftol. If full machine accuracy is required, they may be set very small, resulting in an error exit with error exit of NE_XTOL_TOO_SMALL, although this may involve many more iterations than a lesser accuracy. You are recommended to set ${\mathbf{ftol}}=0.0$ and to use xtol to control the accuracy, unless you have considerable knowledge of the size of $f\left(x\right)$ for values of $x$ near the zero.

The time taken by nag_zero_cont_func_bd_1 (c05sdc) depends primarily on the time spent evaluating f (see Section 5).

9  Example

This example calculates the zero of ${e}^{-x}-x$ within the interval $\left[0,1\right]$ to approximately five decimal places.

9.1  Program Text

Program Text (c05sdce.c)

None.

9.3  Program Results

Program Results (c05sdce.r)