# The quadratic equation x^2-5*x+6=0 has two roots, 2 and 3
f0 <- function(x) return(x^2-5*x+6)
# First derivative
f1 <- function(x) return(2*x-5)
# Find root 2, starting from a single point close to 2
x0 <- 1
x <- roots_newton(f0,f1,x0=1)
print(x)
# Find root 3 (no message printing)
x <- roots_newton(f0,f1,x0=4,message=FALSE)
print(x)
# Function with a parameter f(x) = exp(kx) - 2
f0 <- function(x,k=2) return(exp(k*x)-2)
# First derivative (it includes the parameter)
f1 <- function(x,k=2) return(k*exp(k*x))
# Solution of exp(2x)-2=0 is log(2)/2
x <- roots_newton(f0,f1,k=2)
print(log(2)/2)
Run the code above in your browser using DataLab