findzeros: Find All Roots

Description Usage Arguments Details Value See Also Examples

View source: R/findzeros.R

Description

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

Usage

1
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.

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.

If the function points are both positive or negative and the slope in this interval is high enough, the minimum or maximum will be determined with optimize and checked for a possible zero.

The function need not be vectorized.

Value

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

See Also

findmins

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
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

f3 <- function(x) sin(pi/x) + 1
findzeros(f3, 0.1, 0.5)
# 0.1052632 0.1333333 0.1818182 0.2857143

f4 <- function(x) sin(pi/x) - 1
findzeros(f4, 0.1, 0.5)
# 0.1176471 0.1538462 0.2222222 0.4000000

## Not run: 
# 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)

## End(Not run)

Example output

 [1] 0.1000000 0.1111028 0.1250183 0.1428641 0.1666655 0.2000004 0.2499867
 [8] 0.3333441 0.4999794 1.0000000
[1] 0.15 0.35 0.55 0.75 0.95
[1] 0.1052632 0.1333333 0.1818182 0.2857143
[1] 0.1176471 0.1538462 0.2222222 0.4000000
 [1]  3.131324  6.601254  9.882900 13.100429 16.289487 19.463512 22.628656
 [8] 25.788118 28.943728 32.096610 35.247487 38.396850 41.545101 44.692365
[15] 47.838886 50.984802 54.130217 57.275218 60.419866 63.564213 66.708305
[22] 69.852177 72.995859 76.139313 79.282681 82.425913 85.568962 88.711962
[29] 91.854861 94.997668 98.140397

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