Learn R Programming

pracma (version 1.4.5)

findzeros: Find All Roots

Description

Finding all roots of a unvariate function in an interval by splitting the interval in many small subintervals.

Usage

findzeros(f, a, b, n = 100, tol = .Machine$double.eps^(2/3), ...)

Arguments

f
functions whose roots shall be found.
a, b
endpoints of the interval.
n
number of subintervals to generate and search.
tol
tolerance for identifying zeros.
...
Additional parameters to be passed to the function.

Value

  • Numeric vector with the x-positions of all roots found in the interval.

Details

Roots, i.e. zeros in a subinterval will be found by applying uniroot to any subinterval where the sign of the function changes. The endpoints of the interval will be tested separately.

The function need not be vectorized.

See Also

uniroot

Examples

Run this code
f1 <- function(x) sin(pi/x)
findzeros(f1, 1/10, 1)
#  0.1000000  0.1111028  0.1250183  0.1428641  0.1666655
#  0.2000004  0.2499867  0.3333441  0.4999794  1.0000000

f2 <- function(x) 0.5*(1 + sin(10*pi*x))
findzeros(f2, 0, 1)
#  0.15  0.35  0.55  0.75  0.95

# Dini function
Dini <- function(x) x * besselJ(x, 1) + 3 * besselJ(x, 0)
findzeros(Dini, 0, 100, n = 128)
ezplot(Dini, 0, 100, n = 512)

Run the code above in your browser using DataLab