Learn R Programming

OOmisc (version 1.2)

secant: A function to find roots of univariate functions.

Description

Finds the roots of univariate functions by using the Secant method.

Usage

secant(fun, x0, x1, eps, maxit = 20, silent = FALSE)

Arguments

fun
a function for which the root is searched.
x0
a numeric value to be used to start the algorithm (first initial).
x1
a numeric value to be used to start the algorithm (second initial).
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.

Value

Returns a numeric result.

References

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

Examples

Run this code
## Example-1
f1=function(x) x^3+sqrt(x)-1
secant(f1,0.5,0.55,10^-10,silent=FALSE)

## Example-2
f2=function(x) x^3-sinh(x)+4*x^2+6*x+9
# searching for reasonable initials
x0=seq(-10,10,,100)
plot(x0,f2(x0),type="n")
lines(x0,f2(x0))
x0=seq(6,8,,100)
plot(x0,f2(x0),type="n")
lines(x0,f2(x0))
abline(h=0,lty=2)

secant(f2,7,7.2,10^-10,maxit=30,silent=FALSE)

Run the code above in your browser using DataLab