Learn R Programming

animation (version 0.2-0)

newton.method: Demonstration of the Newton-Raphson Method for Root-finding

Description

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.

Usage

newton.method(FUN = function(x) x^2 - 4, init = 10, 
    rg = c(-1, 10), tol = 0.001, interact = FALSE, 
    control = ani.control(interval = 2), ...)

Arguments

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?
control
control parameters for the animation; see ani.control
...
other arguments passed to ani.control

Value

  • A list containing
  • rootthe root found by the algorithm
  • valuethe value of FUN(root)
  • iternumber 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

Details

The iteration goes on in this way: $$x_{k + 1} = x_{k} - \frac{FUN(x_{k})}{FUN'(x_{k})}$$ From the starting value x0, blue vertical lines and red points are plotted to show the location of the sequence of iteration values x1, x2, ...; red tangent lines are drawn to illustrate the relationship between successive iterations; the iteration values are in the right margin of the plot.

References

http://en.wikipedia.org/wiki/Newton's_method

See Also

optim

Examples

Run this code
op = par(pch = 19) 
# default example 
xx = newton.method() 
xx$root  # solution

# 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.start()
newton.method(function(x) exp(-x) * x, rg = c(0, 10), 
    init = 2, saveANI = TRUE, width = 600, height = 500)
ani.stop()
par(op)

Run the code above in your browser using DataLab