# newtonRaphson.basic: Newton-Raphson algorithm In VGAMextra: Additions and Extensions of the 'VGAM' Package

## Description

Newton–Raphson algorithm to approximate the roots of univariate real–valued functions.

This function is vectorized.

## Usage

 ```1 2 3``` ```newtonRaphson.basic(f, fprime, a, b, tol = 1e-8, n.Seq = 20, nmax = 15, ...) ```

## Arguments

 `f` A univariate function whose root(s) are approximated. This is the target function. Must return a vector. `fprime` A function. The first derivative of `f`. Must return a vector. `a, b` Numeric vectors. Upper and lower real limits of the open interval (a, b) where the root(s) of `f` will be searched. Notice, entries `Inf`, `-Inf`, `NA` and `NaN` are not handled. These vectors are subject to be recycled if `a` and `b` lenghts differ. `tol` Numeric. A number close to zero to test whether the approximate roots from iterations k and (k + 1) are close enough to stop the algorithm. `n.Seq` Numeric. The number of equally spaced initial points within the interval (`a`, `b`) to internally set up initial values for the algorithm. `nmax` Maximum number of iterations. Default is 15. `...` Any other argument passed down to functions `f` and `fprime`.

## Details

This is an implementation of the well–known Newton–Raphson algorithm to find a real root, r, a < r < b, of the function f.

Initial values, r say, for the algorithm are internally computed by drawing '`n.Seq`' equally spaced points in (a, b). Then, the function `f` is evaluated at this sequence. Finally, r results from the closest image to the horizontal axis.

At iteration k, the (k + 1)^th approximation given by

r[k + 1] = r[k] - f(r[k], ... ) / fprime(r[k], ...)

is computed, unless the approximate root from step k is the desired one.

`newtonRaphson.basic` approximates this root up to a relative error less than `tol`. That is, at each iteration, the relative error between the estimated roots from iterations k and k + 1 is calculated and then compared to `tol`. The algorithm stops when this condition is met.

Instead of being single real values, arguments `a` and `b` can be entered as vectors of length n, say a = c(a, a, …, a[n]) and b = c(b, b,…, b[n]) . In such cases, this function approaches the (supposed) root(s) at each interval (a[ j ], b[ j ]), j = 1, …, n. Here, initial values are searched for each interval (a[ j ], b[ j ]).

## Value

The approximate roots in the intervals (a[ j ], b[ j ]). When j = 1, then a single estimated root is returned, if any.

## Note

The explicit forms of the target function `f` and its first derivative `fprime` must be available for the algorithm.

`newtonRaphson.basic` does not handle yet numerically approximated derivatives.

A warning is displayed if no roots are found, or if more than one root might be lying in (a[ j ], b[ j ]), for any j = 1, …, n.

If `a` and `b` lengths differ, then the recyling rule is applied. Specifically, the vector with minimum length will be extended up to match the maximum length by repeating its values.

## Author(s)

V. Miranda.

`bisection.basic`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16``` ```# Find the roots in c(-0.5, 0.8), c(0.6, 1.2) and c(1.3, 4.1) for the # f(x) = x * (x - 1) * (x - 2). Roots: r1 = 0, and r2 = 1, r3 = 2. f <- function(x) x * (x - 1) * (x - 2) fprime <- function(x) 3 * x^2 - 6 * x + 2 # Three roots. newtonRaphson.basic(f = f, fprime = fprime, a = c(-0.5, 0.6, 1.3), b = c(0.8, 1.2, 4.1)) ## 0.0, 1.0 and 2.0 # Recycling rule. Intervals analysed are (-0.5, 1.2) and (0.6, 1.2) newtonRaphson.basic(f = f, fprime = fprime, a = c(-0.5, 0.6), b = c(1.2)) ## Warning: There is more than one root in (-0.5, 1.2)! ```

### Example output

```Loading required package: stats4

=====    VGAMextra 0.0-2    =====

Additions and extensions of the package VGAM.
For more on VGAMextra, visit
https://www.stat.auckland.ac.nz/~vmir178/

For a short description, fixes/bugs, and new
features type vgamextraNEWS().
 -4.417621e-25  1.000000e+00  2.000000e+00
 1 1
Warning message:
In newtonRaphson.basic(f = f, fprime = fprime, a = c(-0.5, 0.6),  :
Either no roots or more than one root between the
given limits 'a' and 'b'.
```

VGAMextra documentation built on May 24, 2021, 9:07 a.m.