ridders: Ridders' Root Finding Method

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/ridders.R

Description

Ridders' root finding method is a powerful variant of ‘regula falsi’ (and ‘false position’). In reliability and speed, this method is competitive with Brent-Dekker and similar approaches.

Usage

1
ridders(fun, a, b, maxiter = 500, tol = 1e-12, ...)

Arguments

fun

function whose root is to be found.

a, b

left and right interval bounds.

maxiter

maximum number of iterations (function calls).

tol

tolerance, length of the last interval.

...

additional parameters passed on to the function.

Details

Given a bracketing interval $[x_1, x_2]$, the method first calculates the midpoint x_3 = (x_1 + x_2)/2 and the uses an updating formula

x_4 = x_3 + (x_3 - x_1) \frac{sgn(f(x_1) - f(x_2)) f(x_3)}{√{f(x_3)^2 - f(x_1) f(x_2)}}

Value

Returns a list with components

root

root of the function.

f.root

value of the function at the found root.

niter

number of iterations,or more specifically: number of function calls.

estim.prec

the estimated precision, coming from the last brackett.

Note

See function f12 whose zero at √{e} is difficult to find exactly for all root finders.

Author(s)

HwB email: <hwborchers@googlemail.com>

References

Press, Teukolsky, Vetterling, and Flannery (1992). Numerical Recipes in C. Cambridge University Press.

See Also

brent

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
##  Test functions
f1  <- function(x)                          # [0, 1.2],     0.399 422 2917
            x^2 * (x^2/3 + sqrt(2)*sin(x)) - sqrt(3)/18
f2  <- function(x) 11*x^11 - 1              # [0.4, 1.6],   0.804 133 0975
f3  <- function(x) 35*x^35 - 1              # [-0.5, 1.9],  0.903 407 6632
f4  <- function(x)                          # [-0.5, 0.7],  0.077 014 24135
            2*(x*exp(-9) - exp(-9*x)) + 1 
f5  <- function(x) x^2 - (1 - x)^9          # [-1.4, 1],    0.259 204 4937
f6  <- function(x) (x-1)*exp(-9*x) + x^9    # [-0.8, 1.6],  0.536 741 6626
f7  <- function(x) x^2 + sin(x/9) - 1/4     # [-0.5, 1.9],  0.4475417621
f8  <- function(x) 1/8 * (9 - 1/x)        # [0.001, 1.201], 0.111 111 1111 
f9  <- function(x) tan(x) - x - 0.0463025   # [-0.9, 1.5],  0.500 000 0340
f10 <- function(x)                          # [0.4, 1],     0.679 808 9215
            x^2 + x*sin(sqrt(75)*x) - 0.2
f11 <- function(x) x^9 + 0.0001             # [-1.2, 0],   -0.359 381 3664 
f12 <- function(x)                          # [1, 3.4],     1.648 721 27070
            log(x) + x^2/(2*exp(1)) - 2 * x/sqrt(exp(1)) + 1

r <- ridders(f1 , 0, 1.2);       r$root; r$niter # 18
r <- ridders(f2 , 0.4, 1.6);     r$root; r$niter # 14
r <- ridders(f3 ,-0.5, 1.9);     r$root; r$niter # 20
r <- ridders(f4 ,-0.5, 0.7);     r$root; r$niter # 12
r <- ridders(f5 ,-1.4, 1);       r$root; r$niter # 16
r <- ridders(f6 ,-0.8, 1.6);     r$root; r$niter # 20
r <- ridders(f7 ,-0.5, 1.9);     r$root; r$niter # 16
r <- ridders(f8 ,0.001, 1.201);  r$root; r$niter # 18
r <- ridders(f9 ,-0.9, 1.5);     r$root; r$niter # 20
r <- ridders(f10,0.4, 1);        r$root; r$niter # 14
r <- ridders(f11,-1.2, 0);       r$root; r$niter # 12
r <- ridders(f12,1, 3.4);        r$root; r$niter # 30, err = 1e-5

## Not run: 
##  Use ridders() with Rmpfr
options(digits=16)
library("Rmpfr") # unirootR
prec <- 256
.N <- function(.) mpfr(., precBits = prec)

f12 <- function(x) {
    e1 <- exp(.N(1))
    log(x) + x^2/(2*e1) - 2*x/sqrt(e1) + 1
}
sqrte <- sqrt(exp(.N(1)))  # 1.648721270700128...
f12(sqrte)                 # 0

unirootR(f12, interval=mpfr(c(1, 3.4), prec), tol=1e-20)
# $root
# 1 'mpfr' number of precision  200   bits 
# [1] 1.648721270700128...

ridders(f12, .N(1), .N(3.4), maxiter=200, tol=1e-20)
# $root
# 1 'mpfr' number of precision  200   bits 
# [1] 1.648721270700128...

## End(Not run)

Example output

[1] 0.3994223
[1] 18
[1] 0.8041331
[1] 14
[1] 0.9034077
[1] 20
[1] 0.07701424
[1] 12
[1] 0.2592045
[1] 16
[1] 0.5367417
[1] 20
[1] 0.4475418
[1] 16
[1] 0.1111111
[1] 18
[1] 0.5
[1] 22
[1] 0.6798089
[1] 14
[1] -0.3593814
[1] 12
[1] 1.648714
[1] 30
Loading required package: gmp

Attaching package: 'gmp'

The following objects are masked from 'package:pracma':

    gcd, isprime

The following objects are masked from 'package:base':

    %*%, apply, crossprod, matrix, tcrossprod

C code of R package 'Rmpfr': GMP using 64 bits per limb


Attaching package: 'Rmpfr'

The following objects are masked from 'package:pracma':

    erf, erfc, hypot, zeta

The following objects are masked from 'package:stats':

    dbinom, dnorm, dpois, pnorm

The following objects are masked from 'package:base':

    cbind, pmax, pmin, rbind

1 'mpfr' number of precision  256   bits 
[1] 0
$root
1 'mpfr' number of precision  256   bits 
[1] 1.648721270700128146846646232563186502390739239639715883839544419521602210666007

$f.root
1 'mpfr' number of precision  256   bits 
[1] -5.990886829868882755214292403242585553769244339922226738886513273067222766557829e-64

$iter
[1] 140

$estim.prec
[1] 5e-21

$converged
[1] TRUE

$root
1 'mpfr' number of precision  256   bits 
[1] 1.648721270700128146852007375466040865090268621899038745966004893179394387137171

$f.root
1 'mpfr' number of precision  256   bits 
[1] 2.812749137164189827227855413625062572836209203421199542389010479751430197114e-63

$niter
[1] 112

$estim.prec
1 'mpfr' number of precision  256   bits 
[1] 8.823872411340090304978232050697269068126654362371676424807645949304187189151582e-21

pracma documentation built on Dec. 11, 2021, 9:57 a.m.