Integer type:** int32**** int64**** nag_int** show int32 show int32 show int64 show int64 show nag_int show nag_int

nag_roots_contfn_cntin_rcomm (c05ax) attempts to locate a zero of a continuous function using a continuation method based on a secant iteration. It uses reverse communication for evaluating the function.

nag_roots_contfn_cntin_rcomm (c05ax) uses a modified version of an algorithm given in Swift and Lindfield (1978) to compute a zero α$\alpha $ of a continuous function
f(x)
$f\left(x\right)$. The algorithm used is based on a continuation method in which a sequence of problems

are solved, where
1
=
θ_{0}
>
θ_{1}
>
⋯
>
θ_{m}
=
0
$1={\theta}_{0}>{\theta}_{1}>\cdots >{\theta}_{m}=0$ (the value of m$m$ is determined as the algorithm proceeds) and where
x_{0}
${x}_{0}$ is your initial estimate for the zero of
f(x)
$f\left(x\right)$. For each
θ_{r}
${\theta}_{r}$ the current problem is solved by a robust secant iteration using the solution from earlier problems to compute an initial estimate.

f(x) − θ _{r}f(x_{0}), r = 0,1, … ,m
$$f\left(x\right)-{\theta}_{r}f\left({x}_{0}\right)\text{, \hspace{1em}}r=0,1,\dots ,m$$ |

You must supply an error tolerance tol. tol is used directly to control the accuracy of solution of the final problem (
θ_{m} = 0
${\theta}_{m}=0$)
in the continuation method, and
sqrt(tol)
$\sqrt{{\mathbf{tol}}}$ is used to control the accuracy in the intermediate problems
(
θ_{1}
,
θ_{2}
,
…
,
θ_{m − 1}
${\theta}_{1},{\theta}_{2},\dots ,{\theta}_{m-1}$).

Swift A and Lindfield G R (1978) Comparison of a continuation method for the numerical solution of a single nonlinear equation *Comput. J.* **21** 359–362

- 1: x – double scalar
*On initial entry*: an initial approximation to the zero.- 2: fx – double scalar
- 3: tol – double scalar
*On initial entry*: a value that controls the accuracy to which the zero is determined. tol is used in determining the convergence of the secant iteration used at each stage of the continuation process. It is used directly when solving the last problem ( θ_{m}= 0 ${\theta}_{m}=0$ in Section [Description]), and sqrt(tol) $\sqrt{{\mathbf{tol}}}$ is used for the problem defined by θ_{r}${\theta}_{r}$, r < m $r<m$. Convergence to the accuracy specified by tol is not guaranteed, and so you are recommended to find the zero using at least two values for tol to check the accuracy obtained.- 4: ir – int64int32nag_int scalar
*On initial entry*: indicates the type of error test required, as follows. Solving the problem defined by θ_{r}${\theta}_{r}$, 1 ≤ r ≤ m $1\le r\le m$, involves computing a sequence of secant iterates x_{r}^{0},x_{r}^{1}, … ${x}_{r}^{0},{x}_{r}^{1},\dots \text{}$. This sequence will be considered to have converged only if:for ir = 0${\mathbf{ir}}=0$,for ir = 1${\mathbf{ir}}=1$,|x_{r}^{(i + 1)}− x_{r}^{(i)}| ≤ eps × max (1.0,|x_{r}^{(i)}|),$$|{x}_{r}^{(i+1)}-{x}_{r}^{\left(i\right)}|\le \mathit{eps}\times \mathrm{max}\phantom{\rule{0.125em}{0ex}}(1.0,\left|{x}_{r}^{\left(i\right)}\right|)\text{,}$$for ir = 2${\mathbf{ir}}=2$,|x_{r}^{(i + 1)}− x_{r}^{(i)}| ≤ eps,$$|{x}_{r}^{(i+1)}-{x}_{r}^{\left(i\right)}|\le \mathit{eps}\text{,}$$for some i > 1 $i>1$; here eps $\mathit{eps}$ is either tol or sqrt(tol) $\sqrt{{\mathbf{tol}}}$ as discussed above. Note that there are other subsidiary conditions (not given here) which must also be satisfied before the secant iteration is considered to have converged.|x_{r}^{(i + 1)}− x_{r}^{(i)}| ≤ eps × |x_{r}^{(i)}|,$$|{x}_{r}^{(i+1)}-{x}_{r}^{\left(i\right)}|\le \mathit{eps}\times \left|{x}_{r}^{\left(i\right)}\right|\text{,}$$- 5: c(26$26$) – double array
- ( c(5) ${\mathbf{c}}\left(5\right)$ contains the current θ
_{r}${\theta}_{r}$, this value may be useful in the event of an error exit.) - 6: ind – int64int32nag_int scalar
*On initial entry*: must be set to 1$1$ or − 1 $-1$.

- 1: scal – double scalar
*On initial entry*: a factor for use in determining a significant approximation to the derivative of f(x) $f\left(x\right)$ at x = x_{0}$x={x}_{0}$, the initial value. A number of difference approximations to f^{′}(x_{0}) ${f}^{\prime}\left({x}_{0}\right)$ are calculated usingwhere |h| < |scal| $\left|h\right|<\left|{\mathbf{scal}}\right|$ and h$h$ has the same sign as scal. A significance (cancellation) check is made on each difference approximation and the approximation is rejected if insignificant.f^{′}(x_{0}) ∼ (f(x_{0}+ h) − f(x_{0})) / h$${f}^{\prime}\left({x}_{0}\right)\sim (f({x}_{0}+h)-f\left({x}_{0}\right))/h$$*Suggested value*: sqrt(ε)$\sqrt{\epsilon}$, where ε$\epsilon $ is the machine precision returned by nag_machine_precision (x02aj).*Default*: sqrt(machine precision) $\sqrt{\mathit{machine\; precision}}$

None.

- 1: x – double scalar
*On intermediate exit*: the point at which f$f$ must be evaluated before re-entry to the function.*On final exit*: the final approximation to the zero.- 2: c(26$26$) – double array
- 3: ind – int64int32nag_int scalar
*On intermediate exit*: contains 2$2$, 3$3$ or 4$4$. The calling program must evaluate f$f$ at x, storing the result in fx, and re-enter nag_roots_contfn_cntin_rcomm (c05ax) with all other parameters unchanged.*On final exit*: contains 0$0$.- 4: ifail – int64int32nag_int scalar
- ifail = 0${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

Errors or warnings detected by the function:

On entry, tol ≤ 0.0 ${\mathbf{tol}}\le 0.0$, or ir ≠ 0${\mathbf{ir}}\ne 0$, 1$1$ or 2$2$.

- The parameter ind is incorrectly set on initial or intermediate entry.

- The current problem in the continuation sequence cannot be solved, see c(5) ${\mathbf{c}}\left(5\right)$ for the value of θ
_{r}${\theta}_{r}$. The most likely explanation is that the current problem has no solution, either because the original problem had no solution or because the continuation path passes through a set of insoluble problems. This latter reason for failure should occur rarely, and not at all if the initial approximation to the zero is sufficiently close. Other possible explanations are that tol is too small and hence the accuracy requirement is too stringent, or that tol is too large and the initial approximation too poor, leading to successively worse intermediate solutions.

- Continuation away from the initial point is not possible. This error exit will usually occur if the problem has not been properly posed or the error requirement is extremely stringent.

- The final problem (with θ
_{m}= 0 ${\theta}_{m}=0$) cannot be solved. It is likely that too much accuracy has been requested, or that the zero is at α = 0 $\alpha =0$ and ir = 2 ${\mathbf{ir}}=2$.

The accuracy of the approximation to the zero depends on tol and ir. In general decreasing tol will give more accurate results. Care must be exercised when using the relative error criterion (
ir = 2
${\mathbf{ir}}=2$).

If the zero is at
x = 0
${\mathbf{x}}=0$, or if the initial value of x and the zero bracket the point
x = 0
${\mathbf{x}}=0$, it is likely that an error exit with ifail = 4${\mathbf{ifail}}={\mathbf{4}}$, 5${\mathbf{5}}$ or 6${\mathbf{6}}$ will occur.

It is possible to request too much or too little accuracy. Since it is not possible to achieve more than machine accuracy, a value of
tol ≪ machine precision
${\mathbf{tol}}\ll \mathit{machineprecision}$ should not be input and may lead to an error exit with ifail = 4${\mathbf{ifail}}={\mathbf{4}}$, 5${\mathbf{5}}$ or 6${\mathbf{6}}$. For the reasons discussed under ifail = 4${\mathbf{ifail}}={\mathbf{4}}$ in Section [Error Indicators and Warnings],
tol should not be taken too large, say no larger than
tol = 1.0e−3
${\mathbf{tol}}=\text{1.0e\u22123}$.

For most problems, the time taken on each call to nag_roots_contfn_cntin_rcomm (c05ax) will be negligible compared with the time spent evaluating
f(x)
$f\left(x\right)$ between calls to nag_roots_contfn_cntin_rcomm (c05ax). However, the initial value of x and the choice of tol will clearly affect the timing. The closer that x is to the root, the less evaluations of f$f$ required. The effect of the choice of tol will not be large, in general, unless tol is very small, in which case the timing will increase.

Open in the MATLAB editor: nag_roots_contfn_cntin_rcomm_example

function nag_roots_contfn_cntin_rcomm_examplefx = 0; c = zeros(26, 1); for k=3:4 x = 1; tol = 10^-k; ir = int64(0); ind = int64(1); while (ind ~= 0) [x, c, ind, ifail] = nag_roots_contfn_cntin_rcomm(x, fx, tol, ir, c, ind); fx = x - exp(-x); end if ifail == 4 || ifail ==6 fprintf('FTol = %11.4e, final value = %11.4e, theta = %10.2e\n', tol, x, c(5)); elseif ifail == 0 fprintf('Tol is %11.4e, Root is %11.4e\n', tol, x); end end

Tol is 1.0000e-03, Root is 5.6715e-01 Tol is 1.0000e-04, Root is 5.6715e-01

Open in the MATLAB editor: c05ax_example

function c05ax_examplefx = 0; c = zeros(26, 1); for k=3:4 x = 1; tol = 10^-k; ir = int64(0); ind = int64(1); while (ind ~= 0) [x, c, ind, ifail] = c05ax(x, fx, tol, ir, c, ind); fx = x - exp(-x); end if ifail == 4 || ifail ==6 fprintf('FTol = %11.4e, final value = %11.4e, theta = %10.2e\n', tol, x, c(5)); elseif ifail == 0 fprintf('Tol is %11.4e, Root is %11.4e\n', tol, x); end end

Tol is 1.0000e-03, Root is 5.6715e-01 Tol is 1.0000e-04, Root is 5.6715e-01

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013