Compute numerically zeros of a function or simultaneous zeros of multiple functions.
Usage
findZeros(
expr,
...,
xlim = c(near - within, near + within),
near = 0,
within = Inf,
nearest = 10,
npts = 1000,
iterate = 1,
sortBy = c("byx", "byy", "radial")
)
# S3 method for class 'formula'
solve(
form,
...,
near = 0,
within = Inf,
nearest = 10,
npts = 1000,
iterate = 1,
sortBy = c("byx", "byy", "radial")
)Arguments
- expr
A formula. The right side names the variable with respect to which the zeros should be found. The left side is an expression, e.g.
sin(x) ~ x. All free variables (all but the variable on the right side) named in the expression must be assigned a value via\ldots- ...
Formulas corresponding to additional functions to use in simultaneous zero finding and/or specific numerical values for the free variables in the expression.
- xlim
The range of the dependent variable to search for zeros.
Infis a legitimate value, but is interpreted in the numerical sense as the non-Inf largest floating point number. This can also be specified replacingxwith the name of the variable. See the examples.- near
a value near which zeros are desired
- within
only look for zeros at least this close to near.
nearandwithinprovide an alternative to usingxlimto specify the search space.- nearest
the number of nearest zeros to return. Fewer are returned if fewer are found.
- npts
How many sub-intervals to divide the
xliminto when looking for candidates for zeros. The default is usually good enough. IfInfis involved, the intervals are logarithmically spaced up to the largest finite floating point number. There is no guarantee that all the roots will be 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 works well when searching in
c(-Inf,Inf)for a modest number of zeros nearnear.- sortBy
specifies how the zeros found will be sorted. Options are 'byx', 'byy', or 'radial'.
- form
Expression to be solved
Value
A dataframe of zero or more numerical values. Plugging these into the expression on the left side of the formula should result in values near zero.
a dataframe with solutions to the expression.
Details
Searches numerically using uniroot.
Uses findZerosMult of findZeros to solve the given expression
Examples
findZeros( sin(t) ~ t, xlim=c(-10,10) )
#> t
#> 1 -12.5664
#> 2 -9.4248
#> 3 -6.2832
#> 4 -3.1416
#> 5 0.0000
#> 6 3.1416
#> 7 6.2832
#> 8 9.4248
#> 9 12.5664
# Can use tlim or t.lim instead of xlim if we prefer
findZeros( sin(t) ~ t, tlim=c(-10,10) )
#> t
#> 1 -12.5664
#> 2 -9.4248
#> 3 -6.2832
#> 4 -3.1416
#> 5 0.0000
#> 6 3.1416
#> 7 6.2832
#> 8 9.4248
#> 9 12.5664
findZeros( sin(theta) ~ theta, near=0, nearest=20)
#> theta
#> 1 -28.2743
#> 2 -25.1327
#> 3 -21.9912
#> 4 -18.8496
#> 5 -15.7080
#> 6 -12.5664
#> 7 -9.4248
#> 8 -6.2832
#> 9 -3.1416
#> 10 0.0000
#> 11 3.1416
#> 12 6.2832
#> 13 9.4248
#> 14 12.5664
#> 15 15.7080
#> 16 18.8496
#> 17 21.9912
#> 18 25.1327
#> 19 28.2743
findZeros( A*sin(2*pi*t/P) ~ t, xlim=c(0,100), P=50, A=2)
#> t
#> 1 0
#> 2 25
#> 3 50
#> 4 75
# 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 )
#> x
#> 1 -11.7741
#> 2 11.7741
# 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 )
#> Error in f(x): could not find function "f"
# Better to look nearer to 0
findZeros( f(x) ~ x, near=0, within=100 )
#> Error in f(x): could not find function "f"
findZeros( f(x) ~ x, near=0, within=100, iterate=0 )
#> Error in f(x): could not find function "f"
findZeros( f(x) ~ x, near=0, within=100, iterate=3 )
#> Error in f(x): could not find function "f"
# Zeros in multiple dimensions (not run: these take a long time)
# findZeros(x^2+y^2+z^2-5~x&y&z, nearest=3000, within = 5)
# findZeros(x*y+z^2~z&y&z, z+y~x&y&z, npts=10)
solve(3*x==3~x)
#> x
#> 1 1
# plot out sphere (not run)
# sphere = solve(x^2+y^2+z^2==5~x&y&z, within=5, nearest=1000)
# cloud(z~x+y, data=sphere)