root: One Dimensional Root (Zero) Finding

View source: R/root.R

rootR Documentation

One Dimensional Root (Zero) Finding

Description

Search one root with given precision (on y). Iterate over uniroot as long as necessary.

Usage

root(
  f,
  lower,
  upper,
  maxerror_f = 1e-07,
  f_lower = f(lower, ...),
  f_upper = f(upper, ...),
  tol = .Machine$double.eps^0.25,
  convexity = FALSE,
  rec = 0,
  max.rec = NA,
  ...
)

Arguments

f

the function for which the root is sought.

lower

the lower end point of the interval to be searched.

upper

the upper end point of the interval to be searched.

maxerror_f

the maximum error on f evaluation (iterates over uniroot to converge).

f_lower

the same as f(lower).

f_upper

the same as f(upper).

tol

the desired accuracy (convergence tolerance on f arg).

convexity

the learned convexity factor of the function, used to reduce the boundaries for uniroot.

rec

counter of recursive level.

max.rec

maximal number of recursive level before failure (stop).

...

additional named or unnamed arguments to be passed to f.

Author(s)

Yann Richet, IRSN

Examples

f=function(x) {cat("f");1-exp(x)}; f(root(f,lower=-1,upper=2))
f=function(x) {cat("f");exp(x)-1}; f(root(f,lower=-1,upper=2))

.f = function(x) 1-exp(1*x)
f=function(x) {cat("f");y=.f(x);points(x,y,pch=20,col=rgb(0,0,0,.2));y}
plot(.f,xlim=c(-1,2)); f(root(f,lower=-1,upper=2))

.f = function(x) exp(10*x)-1
f=function(x) {cat("f");y=.f(x);points(x,y,pch=20);y}
plot(.f,xlim=c(-1,2)); f(root(f,lower=-1,upper=2))

.f = function(x) exp(100*x)-1
f=function(x) {cat("f");y=.f(x);points(x,y,pch=20);y}
plot(.f,xlim=c(-1,2)); f(root(f,lower=-1,upper=2))

f=function(x) {cat("f");exp(100*x)-1}; f(root(f,lower=-1,upper=2))

## Not run: 

  # Quite hard functions to find roots

  ## Increasing function
  ## convex
  n.f=0
  .f = function(x) exp(10*x)-1
  f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20);y}
  plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
  print(n.f)
  ## non-convex
  n.f=0
  .f = function(x) 1-exp(-10*x)
  f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20);y}
  plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
  print(n.f)

  # ## Decreasing function
  # ## non-convex
  n.f=0
  .f = function(x) 1-exp(10*x)
  f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20,col=rgb(0,0,0,.2));y}
  plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
  print(n.f)
  # ## convex
  n.f=0
  .f = function(x) exp(-10*x)-1
  f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20,col=rgb(0,0,0,.2));y}
  plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
  print(n.f)

## End(Not run)

IRSN/DiceView documentation built on Jan. 31, 2024, 10:09 a.m.