# Kode R tugas 
# 1. NEWTON-RAPHSON: f(x) = x^3 - x - 2
f <- function(x) x^3 - x - 2
df <- function(x) 3*x^2 - 1

newton_root <- function(x0, tol = 1e-10, maxit = 100) {
  x <- x0
  riwayat <- data.frame(iterasi = 0, x = x, f_x = f(x))
  
  for (i in 1:maxit) {
    x_baru <- x - f(x)/df(x)
    riwayat <- rbind(
      riwayat,
      data.frame(iterasi = i, x = x_baru, f_x = f(x_baru))
    )
    
    if (abs(x_baru - x) < tol) break
    x <- x_baru
  }
  
  list(akar = x_baru, iterasi = i, tabel = riwayat)
}

hasil_newton <- newton_root(1.5)
hasil_newton$tabel
##   iterasi        x           f_x
## 1       0 1.500000 -1.250000e-01
## 2       1 1.521739  2.136928e-03
## 3       2 1.521380  5.893874e-07
## 4       3 1.521380  4.529710e-14
## 5       4 1.521380  0.000000e+00
# Bisection
bisection <- function(a, b, tol = 1e-10, maxit = 1000) {
  if (f(a)*f(b) > 0) {
    stop("f(a) dan f(b) harus berlainan tanda.")
  }
  
  for (i in 1:maxit) {
    m <- (a+b)/2
    
    if (abs(f(m)) < tol || abs(b-a)/2 < tol) break
    
    if (f(a)*f(m) < 0) {
      b <- m
    } else {
      a <- m
    }
  }
  
  list(akar = m, iterasi = i)
}

hasil_bisect <- bisection(1, 2)
hasil_uniroot <- uniroot(f, interval = c(1, 2), tol = 1e-10)

perbandingan_akar <- data.frame(
  metode = c("Newton-Raphson", "Bisection", "uniroot"),
  akar = c(hasil_newton$akar, hasil_bisect$akar, hasil_uniroot$root),
  iterasi = c(hasil_newton$iterasi, hasil_bisect$iterasi,
              hasil_uniroot$iter)
)
perbandingan_akar
##           metode    akar iterasi
## 1 Newton-Raphson 1.52138       4
## 2      Bisection 1.52138      33
## 3        uniroot 1.52138       8
# 2. KUANTIL t: t(0.975), df = 9
# Fungsi kuantil t
df_t <- 9
p_target <- 0.975

ft <- function(x) {
  pt(x, df = df_t) - p_target
}

dft <- function(x) {
  dt(x, df = df_t)
}

# Newton-Raphson dengan pengaman
newton_t <- function(x0 = 10, tol = 1e-10, maxit = 100) {
  
  a <- 0
  b <- 10
  x <- x0
  
  tabel <- data.frame(
    iterasi = 0,
    x = x,
    f_x = ft(x)
  )
  
  for (i in 1:maxit) {
    
    fx <- ft(x)
    dfx <- dft(x)
    
    # Menghitung langkah Newton
    x_baru <- x - fx/dfx
    
    # Jika langkah Newton tidak valid atau keluar interval,
    # gunakan Bisection
    if (!is.finite(x_baru) || x_baru <= a || x_baru >= b) {
      x_baru <- (a + b)/2
    }
    
    # Memperbarui interval akar
    if (ft(x_baru) < 0) {
      a <- x_baru
    } else {
      b <- x_baru
    }
    
    tabel <- rbind(
      tabel,
      data.frame(
        iterasi = i,
        x = x_baru,
        f_x = ft(x_baru)
      )
    )
    
    # Kriteria berhenti
    if (abs(ft(x_baru)) < tol ||
        abs(x_baru - x) < tol) {
      break
    }
    
    x <- x_baru
  }
  
  return(list(
    kuantil = x_baru,
    iterasi = i,
    tabel = tabel
  ))
}

# Menjalankan metode
hasil_t <- newton_t()

# Menampilkan tabel iterasi
hasil_t$tabel
##   iterasi         x           f_x
## 1       0 10.000000  2.499821e-02
## 2       1  5.000000  2.463052e-02
## 3       2  2.500000  8.069086e-03
## 4       3  2.209537 -2.243173e-03
## 5       4  2.260009 -8.792330e-05
## 6       5  2.262153 -1.508562e-07
## 7       6  2.262157 -4.461986e-13
# Menampilkan kuantil hasil Newton
hasil_t$kuantil
## [1] 2.262157
# Membandingkan dengan fungsi qt()
qt(0.975, df = 9)
## [1] 2.262157
# 3. SECANT DAN NEWTON: cos(x) = x
fs <- function(x) cos(x)-x
dfs <- function(x) -sin(x)-1

secant_method <- function(x0 = 0, x1 = 1,
                          tol = 1e-10, maxit = 100) {
  n_eval <- 2
  
  tabel <- data.frame(iterasi = 0, x = x0, f_x = fs(x0))
  tabel <- rbind(
    tabel,
    data.frame(iterasi = 1, x = x1, f_x = fs(x1))
  )
  
  for (i in 2:maxit) {
    f0 <- fs(x0)
    f1 <- fs(x1)
    
    x2 <- x1 - f1*(x1-x0)/(f1-f0)
    n_eval <- n_eval + 1
    
    tabel <- rbind(
      tabel,
      data.frame(iterasi = i, x = x2, f_x = fs(x2))
    )
    
    if (abs(x2-x1) < tol) break
    
    x0 <- x1
    x1 <- x2
  }
  
  list(root = x2, iterasi = i, evaluasi_f = n_eval, tabel = tabel)
}

newton_method <- function(x0 = 1, tol = 1e-10, maxit = 100) {
  n_eval_f <- 0
  n_eval_df <- 0
  
  tabel <- data.frame(iterasi = 0, x = x0, f_x = fs(x0))
  n_eval_f <- n_eval_f + 1
  x <- x0
  
  for (i in 1:maxit) {
    fx <- fs(x)
    dfx <- dfs(x)
    
    n_eval_f <- n_eval_f + 1
    n_eval_df <- n_eval_df + 1
    
    x_baru <- x - fx/dfx
    
    tabel <- rbind(
      tabel,
      data.frame(iterasi = i, x = x_baru, f_x = fs(x_baru))
    )
    n_eval_f <- n_eval_f + 1
    
    if (abs(x_baru-x) < tol) break
    x <- x_baru
  }
  
  list(
    root = x_baru,
    iterasi = i,
    evaluasi_f = n_eval_f,
    evaluasi_df = n_eval_df,
    tabel = tabel
  )
}

hasil_secant <- secant_method()
hasil_newton_cos <- newton_method()

hasil_secant$tabel
##   iterasi         x           f_x
## 1       0 0.0000000  1.000000e+00
## 2       1 1.0000000 -4.596977e-01
## 3       2 0.6850734  8.929928e-02
## 4       3 0.7362990  4.660039e-03
## 5       4 0.7391194 -5.728599e-05
## 6       5 0.7390851  3.529262e-08
## 7       6 0.7390851  2.667866e-13
## 8       7 0.7390851  0.000000e+00
hasil_newton_cos$tabel
##   iterasi         x           f_x
## 1       0 1.0000000 -4.596977e-01
## 2       1 0.7503639 -1.892307e-02
## 3       2 0.7391129 -4.645590e-05
## 4       3 0.7390851 -2.847206e-10
## 5       4 0.7390851  0.000000e+00
## 6       5 0.7390851  0.000000e+00
data.frame(
  metode = c("Secant", "Newton"),
  akar = c(hasil_secant$root, hasil_newton_cos$root),
  iterasi = c(hasil_secant$iterasi, hasil_newton_cos$iterasi),
  evaluasi_f = c(hasil_secant$evaluasi_f,
                 hasil_newton_cos$evaluasi_f),
  evaluasi_turunan = c(NA, hasil_newton_cos$evaluasi_df)
)
##   metode      akar iterasi evaluasi_f evaluasi_turunan
## 1 Secant 0.7390851       7          8               NA
## 2 Newton 0.7390851       5         11                5
# 4. MLE PARAMETER GAMMA DENGAN NEWTON-RAPHSON
# Data contoh; ganti dengan data pengamatan Anda
set.seed(2026)
x <- rgamma(100, shape = 3.5, scale = 2)

n <- length(x)
xbar <- mean(x)
s <- sd(x)

C <- log(xbar) - mean(log(x))

g <- function(alpha) {
  log(alpha) - digamma(alpha) - C
}
dg <- function(alpha) {
  1/alpha - trigamma(alpha)
}

alpha0 <- 1/(2*s)

newton_gamma <- function(alpha0, tol = 1e-10, maxit = 100) {
  alpha <- alpha0
  
  tabel <- data.frame(
    iterasi = 0,
    alpha = alpha,
    f_alpha = g(alpha)
  )
  for (i in 1:maxit) {
    alpha_baru <- alpha - g(alpha)/dg(alpha)
    
    if (!is.finite(alpha_baru) || alpha_baru <= 0) {
      stop("Iterasi menghasilkan alpha tidak positif; coba nilai awal lain.")
    }
    
    tabel <- rbind(
      tabel,
      data.frame(
        iterasi = i,
        alpha = alpha_baru,
        f_alpha = g(alpha_baru)
      )
    )
    if (abs(alpha_baru-alpha) < tol) break
    alpha <- alpha_baru
  }
  
  list(alpha = alpha_baru, iterasi = i, tabel = tabel)
}

hasil_gamma <- newton_gamma(alpha0)
hasil_gamma$tabel
##    iterasi     alpha      f_alpha
## 1        0 0.1567648 4.753995e+00
## 2        1 0.2901283 2.272524e+00
## 3        2 0.5273550 1.077377e+00
## 4        3 0.9397992 5.015186e-01
## 5        4 1.6190406 2.223261e-01
## 6        5 2.5959366 8.756114e-02
## 7        6 3.6450014 2.615932e-02
## 8        7 4.2826326 4.028572e-03
## 9        8 4.4198410 1.293084e-04
## 10       9 4.4245421 1.420654e-07
## 11      10 4.4245473 1.716405e-13
## 12      11 4.4245473 2.220446e-16
# Estimasi parameter skala
theta_newton <- xbar/hasil_gamma$alpha
theta_newton
## [1] 1.53058
# Pembanding dengan MASS::fitdistr()
if (!requireNamespace("MASS", quietly = TRUE)) {
  install.packages("MASS")
}
fit_gamma <- MASS::fitdistr(x, densfun = "gamma")

hasil_perbandingan_gamma <- data.frame(
  metode = c("Newton (alpha)", "MASS::fitdistr (alpha)"),
  alpha = c(
    hasil_gamma$alpha,
    unname(fit_gamma$estimate["shape"])
  ),
  scale = c(
    theta_newton,
    1/unname(fit_gamma$estimate["rate"])
  )
)
hasil_perbandingan_gamma
##                   metode    alpha    scale
## 1         Newton (alpha) 4.424547 1.530580
## 2 MASS::fitdistr (alpha) 4.424583 1.530568

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.