| newton.method {animation} | R Documentation |
Newton's method (also known as the Newton-Raphson method or the Newton-Fourier method) is an efficient algorithm for finding approximations to the zeros (or roots) of a real-valued function f(x). This function provides an illustration of the iterations in Newton's method.
newton.method(FUN = function(x) x^2 - 4, init = 10, rg = c(-1, 10),
tol = 0.001, interact = FALSE, col.lp = c("blue", "red", "red"),
main, xlab, ylab, ...)
FUN |
the function in the equation to solve (univariate) |
init |
the starting point |
rg |
the range for plotting the curve |
tol |
the desired accuracy (convergence tolerance) |
interact |
logical; whether choose the starting point by cliking on the curve (for 1 time) directly? |
col.lp |
a vector of length 3 specifying the colors of: vertical lines, tangent lines and points |
main, xlab, ylab |
titles of the plot; there are default values for them (depending on the form of the function FUN) |
... |
other arguments passed to curve |
The iteration goes on in this way:
x[k + 1] = x[k] - FUN(x[k]) / FUN'(x[k])
From the starting value x_0, vertical lines and points are plotted to show the location of the sequence of iteration values x1, x2, ...; tangent lines are drawn to illustrate the relationship between successive iterations; the iteration values are in the right margin of the plot.
A list containing
root |
the root found by the algorithm |
value |
the value of FUN(root) |
iter |
number of iterations; if it is equal to control$nmax, it's quite likely that the root is not reliable because the maximum number of iterations has been reached |
The algorithm might not converge – it depends on the starting value. See the examples below.
Yihui Xie
http://en.wikipedia.org/wiki/Newton's_method
oopt = ani.options(interval = 1, nmax = 50)
op = par(pch = 20)
# default example
xx = newton.method()
xx$root # solution
# take a long long journey
newton.method(function(x) 5 * x^3 - 7 * x^2 - 40 *
x + 100, 7.15, c(-6.2, 7.1))
## Not run:
# another function
xx = newton.method(function(x) exp(-x) * x, rg = c(0,
10), init = 2)
# not converge!
xx = newton.method(function(x) atan(x), rg = c(-5,
5), init = 1.5)
xx$root # Inf
# interaction: use your mouse to select the starting point
xx = newton.method(function(x) atan(x), rg = c(-2,
2), interact = TRUE)
# HTML animation pages
ani.options(ani.height = 500, ani.width = 600, outdir = getwd(), nmax = 100,
interval = 1, title = "Demonstration of the Newton-Raphson Method",
description = "Go along with the tangent lines and iterate.")
ani.start()
par(mar = c(3, 3, 1, 1.5), mgp = c(1.5, 0.5, 0), pch = 19)
newton.method(function(x) 5 * x^3 - 7 * x^2 - 40 *
x + 100, 7.15, c(-6.2, 7.1), main = "")
ani.stop()
## End(Not run)
par(op)
ani.options(oopt)