secant <- function(ftn, x, tol = 1e-9, max.iter = 100) {
  #secant algorithm for solving ftn(x) == 0
  #x0 is starting point
  
  iter <- 0
  xold <- x
  fxold <- ftn(x)
  x <- xold + 10*tol
  
  while (abs(x - xold) > tol) {
    iter <- iter + 1
    if(iter > max.iter) {
      stop("rishe nadarad")
    }
    fx <- ftn(x)
    xnew <- x - fx * ((x - xold)/(fx - fxold))
    xold <- x
    fxold <- fx
    x <- xnew
  }
  return(x)
  }