newton_raphson: Newton-Raphson Method

View source: R/newton-raphson.R

newton_raphsonR Documentation

Newton-Raphson Method

Description

The Newton-Raphson Method is an iterative algorithm for calculating the root (x-intercept) of an arbitrary function f(x).

Usage

newton_raphson(init, tol = 1e-04)

Arguments

init

The initial x value to begin Newton-Raphson steps.

tol

The tolerance for the error in the root. Used to stop the algorithm once the root estimate no longer changes by this much in x.

Details

The idea is to start with an initial guess which is reasonably close to the true root, then to approximate the function by its tangent line using calculus, and finally to compute the x-intercept of this tangent line by elementary algebra. This x-intercept will typically be a better approximation to the original function's root than the first guess, and the method can be iterated until some threshold tolerance is crossed.

Newton-Raphson Method for calculating next x1 has the equation:

x1 = x0 - f(x0) / f'(x0)

Value

The function generates a ggplot2 of the algorithm, as well as a list containing:

x_traj

The iterative "guesses" for the function root.

niter

The number of iterations required to find the root.

root

The estimate of the root (x-intercept).

Source

https://en.wikipedia.org/wiki/Newton%27s_method

Examples

newton_raphson(3)
newton_raphson(-3)

stufield/stuRpkg documentation built on April 2, 2022, 2:05 p.m.