View source: R/newtonRaphson.basic.R

newtonRaphson.basic | R Documentation |

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

This function is vectorized.

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

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

`a, b` |
Numeric vectors.
Upper and lower real limits of the open interval These vectors are subject to be recycled if |

`tol` |
Numeric. A number close to zero to test whether the
approximate roots from iterations |

`n.Seq` |
Numeric. The number of equally spaced initial points within
the interval ( |

`nmax` |
Maximum number of iterations. Default is |

`...` |
Any other argument passed down to functions |

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

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

results from the closest image to
the horizontal axis.

At iteration `k`

, the `(k + 1)^{th}`

approximation
given by

```
r^{(k + 1)} = r^{(k)} -
{\tt{f}}(r^{(k), ...)} / {\tt{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
`{\tt{a}} = c(a_1, a_2, \ldots, a_n)`

and
`{\tt{b}} = c(b_1, b_2,\ldots, b_n)`

.
In such cases, this function approaches the (supposed) root(s)
at each interval `(a_j, b_j)`

,
`j = 1, \ldots, n`

. Here, initial values are searched
for each interval `(a_j, b_j)`

.

The approximate roots in the intervals
`(a_j, b_j)`

.
When `j = 1`

, then a single estimated root is returned, if any.

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, \ldots, 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.

V. Miranda.

`bisection.basic`

```
# 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)!
```

VGAMextra documentation built on Nov. 2, 2023, 5:59 p.m.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.