Learn R Programming

OOmisc (version 1.2)

newton: A function to find the roots of univariate functions.

Description

Finds roots of univariate functions by the usual Newton-Raphson (N-R) method.

Usage

newton(fun, derf, x0, eps, maxit = 20, silent = TRUE, tun=1)

Arguments

fun
a function for which the root is searched.
derf
a function which is the first derivative of the function to be solved.
x0
a numeric value to be used to start the algorithm.
eps
a numeric value to be considered as the tolerance for convergence of the algorithm.
maxit
a numeric value which denotes maximum number of iterations to be consumed.
silent
a logical statement which decides whether the iterations should be printed.
tun
a numeric value to decrease the steps

Value

Returns a numeric result of the root.

Details

tun is used to decrease the N-R steps, since it sometimes might miss the root value by taking large steps. tun=1 corresponds to usual N-R.

References

Ilk, O. (2011). R Yazilimina Giris [Introduction to R Language]. ODTU Yayincilik [METU Press].

Examples

Run this code
# function and the derivative
f1=function(x) x^3+sqrt(x)-1
df1=function(x) 3*x^2+(1/2)*x^(-1/2)
# searching for a reasonable initial
x0=seq(0,2,,100)
plot(x0,f1(x0),type="n")
lines(x0,f1(x0))
abline(h=0,lty=2)
newton(f1,df1,0.5,10**-10,silent=FALSE)

Run the code above in your browser using DataLab