Learn R Programming

mosaic (version 0.4-1)

findZeros: Find the zeros of a function

Description

Compute numerically the zeros of a function.

Usage

findZeros(expr, ...,
    xlim = c(near - within, near + within), near = 0,
    within = Inf, nearest = 10, npts = 1000, iterate = 1)

Arguments

expr
A formula, e.g. sin(x) ~ x. The right side names the variable with respect to which the zeros should be found. The left side is an expression. All free variables (all but the variable on the right side) named in the expression must be
...
Specific numerical values for the free variables in the expression.
xlim
The range of the dependent variable to search for zeros. Inf is a legitimate value, but is interpreted in the numerical sense as the non-Inf largest floating point number. This can also be specified replacing x with the
near
a value near which zeros are desired
within
only look for zeros at least this close to near. near and within provide an alternative to using xlim to specify the search space.
nearest
the number of nearest zeros to return. Fewer are returned if fewer are found.
iterate
maximum number of times to iterate the search. Subsequent searches take place with the range of previously found zeros. Choosing a large number here is likely to kill performance without improving results, but a value of 1 (the default) or 2 work
npts
How many sub-intervals to divide the xlim into when looking for candidates for zeros. The default is usually good enough. If Inf is involved, the intervals are logarithmically spaced up to the largest finite floating poin

Value

  • A set of zero or more numerical values. Plugging these into the expression on the left side of the formula should result in values near zero.

Details

Searches numerically using uniroot.

Examples

Run this code
findZeros( sin(t) ~ t, xlim=c(-10,10) )
# Can use tlim or t.lim instead of xlim if we prefer
findZeros( sin(t) ~ t, tlim=c(-10,10) )
findZeros( sin(theta) ~ theta, near=0, nearest=20)
findZeros( A*sin(2*pi*t/P) ~ t, xlim=c(0,100), P=50, A=2)
# Interval of a normal at half its maximum height.
findZeros( dnorm(x,mean=0,sd=10) - 0.5*dnorm(0,mean=0,sd=10) ~ x )
# A pathological example
# There are no "neareset" zeros for this function.  Each iteration finds new zeros.
f <- function(x) { if (x==0) 0 else sin(1/x) }
findZeros( f(x) ~ x, near=0 )
# Better to look nearer to 0
findZeros( f(x) ~ x, near=0, within=100 )
findZeros( f(x) ~ x, near=0, within=100, iterate=0 )
findZeros( f(x) ~ x, near=0, within=100, iterate=3 )

Run the code above in your browser using DataLab