newtonraphson: A function of the Newton-Raphson algorithm.

Description Usage Arguments Value References See Also Examples

View source: R/newtonraphson.R

Description

Applies the Newton-Raphson algorithm to find x such that ftn(x)[1] == 0.

Usage

1
newtonraphson(ftn, x0, tol = 1e-09, max.iter = 100)

Arguments

ftn

the function.

x0

is the initial guess at the fixed point.

tol

distance of successive iterations at which algorithm terminates.

max.iter

maximum number of iterations.

Value

Returns the value of x at which ftn(x)[1] == 0. If the function fails to converge within max.iter iterations, returns NULL.

References

Jones, O.D., R. Maillardet, and A.P. Robinson. 2009. An Introduction to Scientific Programming and Simulation, Using R. Chapman And Hall/CRC.

See Also

fixedpoint, bisection

Examples

1
2
3
4
5
6
7
ftn4 <- function(x) {
  # returns function value and its derivative at x
  fx <- log(x) - exp(-x)
  dfx <- 1/x + exp(-x)
  return(c(fx, dfx))
}
newtonraphson(ftn4, 2, 1e-6)

Example output

Loading required package: MASS
Loading required package: lattice
At iteration 1 value of x is: 1.12202 
At iteration 2 value of x is: 1.294997 
At iteration 3 value of x is: 1.309709 
At iteration 4 value of x is: 1.3098 
Algorithm converged
[1] 1.3098

spuRs documentation built on May 30, 2017, 4:58 a.m.