newton.root <- function(f, x0 = 0, tol = 1e-9, max.iter = 100) { # initialise x <- x0 fx <- ftn(f, x) iter <- 0 # continue iterating until stopping conditions are met while ((abs(fx$f) > tol) && (iter < max.iter)) { x <- x - fx$f/fx$fgrad fx <- ftn(f, x) iter <- iter + 1 cat("At iteration", iter, "value of x is:", x, "\n") } # output depends on success of algorithm if (abs(fx$f) > tol) { cat("Algorithm failed to converge\n") return(NULL) } else { cat("Algorithm converged\n") return(x) } } # f <- function(x) x^2 - 5 # f <- expression(abs(x) ^ 0.5) # f <- function(x) x*exp(-x^2) - 0.4*(1/(exp(x)+1)) - 0.2 # f <- function(x) x^3 -2*x^2 - 11*x + 12 f <- function(x) 2*x^3+ 3*x^2 + 5 ftn <- function(f, x){ df <- deriv(f, 'x', func = TRUE) dfx <- df(x) f <- dfx[1] fgrad <- attr(dfx, 'gradient')[1,] return(list(f = f, fgrad = fgrad)) } newton.root(body(f), 2.35284172) curve(f,-5,5) abline(h = 0, col=2, lty = 3)