newtonRaphson.basic {VGAMextra} | 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 (a, b)
where the root(s) of These vectors are subject to be recycled if |
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 ( |
nmax |
Maximum number of iterations. Default is 15. |
... |
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] - 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[1], a[2], …, a[n])
and
b = c(b[1], b[2],…, 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 ]).
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, …, 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.
# 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)!