1. Newton-Raphson

f1 <- function(x) x^3 - x - 2
df1 <- function(x) 3*x^2 - 1

# Newton-Raphson
newton1 <- function(x0 = 1.5, tol = 1e-10, max_iter = 100) {
  x <- x0
  for (i in 1:max_iter) {
    x_new <- x - f1(x)/df1(x)
    if (abs(x_new - x) < tol) return(list(akar = x_new, iterasi = i))
    x <- x_new
  }
}

# Bisection
bisection1 <- function(a = 1, b = 2, tol = 1e-10, max_iter = 100) {
  for (i in 1:max_iter) {
    c <- (a + b) / 2
    if (abs(b - a)/2 < tol || f1(c) == 0) return(list(akar = c, iterasi = i))
    if (f1(a) * f1(c) < 0) b <- c else a <- c
  }
}

res_nr1 <- newton1()
res_bis1 <- bisection1()
res_uni1 <- uniroot(f1, interval = c(1, 2), tol = 1e-10)

data.frame(
  Metode = c("Newton-Raphson", "Bisection", "uniroot"),
  Akar = c(res_nr1[["akar"]], res_bis1[["akar"]], res_uni1[["root"]]),
  Iterasi = c(res_nr1[["iterasi"]], res_bis1[["iterasi"]], res_uni1[["iter"]])
)
##           Metode    Akar Iterasi
## 1 Newton-Raphson 1.52138       4
## 2      Bisection 1.52138      34
## 3        uniroot 1.52138       8

2. Kuantil t

f2 <- function(x) pt(x, df = 9) - 0.975
df2 <- function(x) dt(x, df = 9)

newton_t <- function(x0, tol = 1e-10, max_iter = 100) {
  x <- x0
  for (i in 1:max_iter) {
    fx <- f2(x)
    dfx <- df2(x)
    if (dfx == 0 || is.nan(dfx)) return(list(akar = NA, iterasi = i, status = "Gagal (turunan nol/NaN)"))
    x_new <- x - fx / dfx
    if (is.nan(x_new) || is.infinite(x_new)) return(list(akar = NA, iterasi = i, status = "Divergen"))
    if (abs(x_new - x) < tol) return(list(akar = x_new, iterasi = i, status = "Konvergen"))
    x <- x_new
  }
  return(list(akar = x, iterasi = max_iter, status = "Maksimum iterasi"))
}

res_x2 <- newton_t(x0 = 2)
res_x10 <- newton_t(x0 = 10)
eksak_qt <- qt(0.975, df = 9)

list(Tebakan_2 = res_x2, Tebakan_10 = res_x10, Nilai_qt = eksak_qt)
## $Tebakan_2
## $Tebakan_2$akar
## [1] 2.262157
## 
## $Tebakan_2$iterasi
## [1] 5
## 
## $Tebakan_2$status
## [1] "Konvergen"
## 
## 
## $Tebakan_10
## $Tebakan_10$akar
## [1] NA
## 
## $Tebakan_10$iterasi
## [1] 3
## 
## $Tebakan_10$status
## [1] "Gagal (turunan nol/NaN)"
## 
## 
## $Nilai_qt
## [1] 2.262157

3. Secant

f3 <- function(x) cos(x) - x
df3 <- function(x) -sin(x) - 1

# Metode Secant
secant3 <- function(x0 = 0, x1 = 1, tol = 1e-10, max_iter = 100) {
  fx0 <- f3(x0)
  fx1 <- f3(x1)
  eval_fungsi <- 2
  
  for (i in 1:max_iter) {
    x2 <- x1 - fx1 * (x1 - x0) / (fx1 - fx0)
    if (abs(x2 - x1) < tol) return(list(akar = x2, iterasi = i, eval = eval_fungsi))
    x0 <- x1; fx0 <- fx1
    x1 <- x2; fx1 <- f3(x1)
    eval_fungsi <- eval_fungsi + 1
  }
}

# Metode Newton-Raphson
newton3 <- function(x0 = 1, tol = 1e-10, max_iter = 100) {
  x <- x0
  eval_fungsi <- 0
  for (i in 1:max_iter) {
    fx <- f3(x)
    dfx <- df3(x)
    eval_fungsi <- eval_fungsi + 2
    x_new <- x - fx / dfx
    if (abs(x_new - x) < tol) return(list(akar = x_new, iterasi = i, eval = eval_fungsi))
    x <- x_new
  }
}

res_sec3 <- secant3()
res_nr3 <- newton3()

data.frame(
  Metode = c("Secant", "Newton-Raphson"),
  Akar = c(res_sec3[["akar"]], res_nr3[["akar"]]),
  Iterasi = c(res_sec3[["iterasi"]], res_nr3[["iterasi"]]),
  Total_Evaluasi_Fungsi = c(res_sec3[["eval"]], res_nr3[["eval"]])
)
##           Metode      Akar Iterasi Total_Evaluasi_Fungsi
## 1         Secant 0.7390851       6                     7
## 2 Newton-Raphson 0.7390851       5                    10

4. Proyek Mini (MLE Gamma)

library(MASS)

set.seed(123)
data_anda <- rgamma(100, shape = 3.5, rate = 1.2)

n <- length(data_anda)
x_bar <- mean(data_anda)
s <- log(x_bar) - mean(log(data_anda))

alpha0 <- 1 / (2 * s)

f_mle <- function(a) log(a) - digamma(a) - s
df_mle <- function(a) (1 / a) - trigamma(a)

newton_gamma <- function(a0, tol = 1e-10, max_iter = 100) {
  a <- a0
  for (i in 1:max_iter) {
    a_new <- a - f_mle(a) / df_mle(a)
    if (abs(a_new - a) < tol) return(list(shape_mle = a_new, iterasi = i))
    a <- a_new
  }
}

hasil_nr_gamma <- newton_gamma(alpha0)
hasil_fitdistr <- fitdistr(data_anda, densfun = "gamma")

cat("Tebakan Awal (alpha_0):", alpha0, "\n")
## Tebakan Awal (alpha_0): 4.197514
cat("MLE Newton-Raphson    :", hasil_nr_gamma[["shape_mle"]], "(Iterasi:", hasil_nr_gamma[["iterasi"]], ")\n")
## MLE Newton-Raphson    : 4.357246 (Iterasi: 4 )
cat("MLE MASS::fitdistr    :", hasil_fitdistr[["estimate"]]["shape"], "\n")
## MLE MASS::fitdistr    : 4.357265