R: Function to find a root by grid search.

unirootGrid {exact2x2} | R Documentation

## Function to find a root by grid search.

### Description

Find the root (value where the function equals 0) of a monotonic function, `func`, using a halving algorithm grid search.

### Usage

    unirootGrid(func, power2 = 12, step.up = TRUE, pos.side = FALSE,
        print.steps = FALSE, power2grid = power2gridRatio, ...)

### Arguments

`func` | monotonic function
`power2` | positive integer, number of grid points is 1+2^power2
`step.up` | logical, start the search at the lower end of the grid and step up?
`pos.side` | logical, should the root be on the positive side? In other words, should func(root)>=0
`print.steps` | logical, should each step that is evaluated be printed?
`power2grid` | function that returns the grid. Take one argument, `power2`
`...` | arguments passed to `func`

### Details

The grid is defined with the `power2grid` argument that defines a function with an argument `power2`, and returns a grid with `1+2^power2` elements. The root is found by a halving algorithm on the grid, so `func` is calculated only `power2+1` times. The ‘root’ is the element that is closest to the root, either on the positive side (`pos.side=TRUE`) or not.

The `unirootGrid` function calls `uniroot.integer` and finds roots based on grid search. The functions `power2gridRatio` and `power2gridDifference` create grids for searching (0,Inf) and (-1,1) respectively. The `power2gridRatio` grid is equally spaced on the log scale with about half of the grid between 0.5 and 2. The function `power2grid` allows more flexibility in defining grids.

### Value

A list with elements:

`iter` | number of iterations
`f.root` | value of func at root
`root` | root, element on the grid that is closest to the root on the negative side (if pos.side=FALSE)
`bound` | interval for the accuracy

### Author(s)

Michael P. Fay

### See Also

`uniroot` and `uniroot.integer`

### Examples

    # print.steps prints all iterations,
    # with x=rank of grid value (e.g., x=1 is lowest value in grid)
    # f(x) really is f(grid[x]) where grid is from the power2grid function
    unirootGrid(function(x){ x - .37}, power2=10, power2grid=power2gridRatio,
      print.steps=TRUE, pos.side=TRUE)

[Package exact2x2 version 1.6.3 Index]