#====================================================
# FUNGSI NEWTON-RAPHSON
#====================================================

newtonraphson <- function(ftn, x0, tol = 1e-9, max.iter = 100) {
  
  x <- x0
  fx <- ftn(x)
  iter <- 0
  
  while ((abs(fx[1]) > tol) && (iter < max.iter)) {
    
    x <- x - fx[1]/fx[2]
    fx <- ftn(x)
    iter <- iter + 1
    
    cat("At iteration", iter, "value of x is:", x, "\n")
  }
  
  if(abs(fx[1]) > tol){
    
    cat("Algorithm failed to converge\n")
    return(NULL)
    
  }else{
    
    cat("Algorithm converged\n")
    return(x)
    
  }
  
}

#====================================================
# FUNGSI SECANT
#====================================================

secant <- function(ftn, x0, x1, tol = 1e-9, max.iter = 100){
  
  f0 <- ftn(x0)
  f1 <- ftn(x1)
  iter <- 0
  
  while((abs(f1) > tol) && (iter < max.iter)){
    
    if(f1 == f0){
      
      cat("error: pembagi nol\n")
      return(NULL)
      
    }
    
    x2 <- x1 - f1*(x1-x0)/(f1-f0)
    
    x0 <- x1
    f0 <- f1
    
    x1 <- x2
    f1 <- ftn(x1)
    
    iter <- iter + 1
    
    cat("At iteration", iter, "value of x is:", x1, "\n")
    
  }
  
  if(abs(f1) > tol){
    
    cat("Algorithm failed to converge\n")
    return(NULL)
    
  }else{
    
    cat("Algorithm converged\n")
    return(x1)
    
  }
  
}

#====================================================
# LATIHAN 1
#====================================================

f_lat1 <- function(x){
  
  c(x^3-x-2,
    3*x^2-1)
  
}

newtonraphson(f_lat1,1.5,tol=1e-10)
## At iteration 1 value of x is: 1.521739 
## At iteration 2 value of x is: 1.52138 
## At iteration 3 value of x is: 1.52138 
## Algorithm converged
## [1] 1.52138
hasil <- uniroot(function(x)x^3-x-2,
                 c(1,2),
                 tol=1e-10)

hasil
## $root
## [1] 1.52138
## 
## $f.root
## [1] -3.153033e-13
## 
## $iter
## [1] 8
## 
## $init.it
## [1] NA
## 
## $estim.prec
## [1] 5.000067e-11
hasil$iter
## [1] 8
#====================================================
# LATIHAN 2
#====================================================

ft <- function(x){
  c(pt(x,9)-0.975,
    dt(x,9))
}

# Newton-Raphson
newtonraphson(ft,1,tol=1e-10)
## At iteration 1 value of x is: 1.640325 
## At iteration 2 value of x is: 2.047073 
## At iteration 3 value of x is: 2.229612 
## At iteration 4 value of x is: 2.261325 
## At iteration 5 value of x is: 2.262157 
## At iteration 6 value of x is: 2.262157 
## Algorithm converged
## [1] 2.262157
# Pembanding
qt(0.975,9)
## [1] 2.262157
# Tebakan awal jauh dari akar
tryCatch(
  newtonraphson(ft, 10, tol = 1e-10),
  error = function(e) cat("Algorithm failed to converge\n")
)
## At iteration 1 value of x is: -16776.42 
## At iteration 2 value of x is: 7.514743e+37 
## At iteration 3 value of x is: -Inf 
## At iteration 4 value of x is: NaN 
## Algorithm failed to converge
#====================================================
# LATIHAN 3
#====================================================

f3 <- function(x){
  cos(x)-x
}

# Secant
secant(f3,0,1,tol=1e-10)
## At iteration 1 value of x is: 0.6850734 
## At iteration 2 value of x is: 0.736299 
## At iteration 3 value of x is: 0.7391194 
## At iteration 4 value of x is: 0.7390851 
## At iteration 5 value of x is: 0.7390851 
## Algorithm converged
## [1] 0.7390851
# Newton-Raphson
newtonraphson(function(x)
  c(cos(x)-x,
    -sin(x)-1),
  1,
  tol=1e-10)
## At iteration 1 value of x is: 0.7503639 
## At iteration 2 value of x is: 0.7391129 
## At iteration 3 value of x is: 0.7390851 
## At iteration 4 value of x is: 0.7390851 
## Algorithm converged
## [1] 0.7390851
# Pembanding
uniroot(f3,c(0,1))$root
## [1] 0.7390911
#====================================================
# LATIHAN 4
#====================================================

library(MASS)

x <- c(12,18,25,31,42,47,58,66,79,95)

s <- log(mean(x))-mean(log(x))

fg <- function(a){
  c(log(a)-digamma(a)-s,
    1/a-trigamma(a))
}

a0 <- 1/(2*s)

# Taksir MLE dengan Newton-Raphson
newtonraphson(fg,a0,tol=1e-10)
## At iteration 1 value of x is: 2.963366 
## At iteration 2 value of x is: 2.971936 
## At iteration 3 value of x is: 2.971962 
## Algorithm converged
## [1] 2.971962
# Pembanding
fitdistr(x,"gamma")
##      shape         rate   
##   2.97196831   0.06283230 
##  (1.26107188) (0.02903995)