BAGIAN 1: PENCARIAN AKAR (Tugas 1 & 2)

1. Newton-Raphson, Bisection, dan uniroot

Soal: Tentukan akar \(f(x) = x^3 - x - 2\) dengan \(x_0 = 1.5\) dan \(\text{tol} = 10^{-10}\). Bandingkan jumlah iterasi dengan Bisection pada \([1, 2]\) dan dengan uniroot[cite: 1, 2].

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

newton_raphson <- function(f, df, x0, tol = 1e-10, max_iter = 100) {
  x <- x0
  for (i in 1:max_iter) {
    fx <- f(x)
    if (abs(fx) < tol) return(list(root = x, iter = i - 1))
    x <- x - fx / df(x)
  }
  return(list(root = x, iter = max_iter))
}

bisection <- function(f, a, b, tol = 1e-10, max_iter = 100) {
  for (i in 1:max_iter) {
    c <- (a + b) / 2
    if (abs(f(c)) < tol || (b - a) / 2 < tol) return(list(root = c, iter = i))
    if (f(a) * f(c) < 0) b <- c else a <- c
  }
  return(list(root = (a + b) / 2, iter = max_iter))
}

res_nr1 <- newton_raphson(f1, df1, x0 = 1.5, tol = 1e-10)
res_bis <- bisection(f1, a = 1, b = 2, tol = 1e-10)
res_uni <- uniroot(f1, interval = c(1, 2), tol = 1e-10)

data.frame(
  Metode = c("Newton-Raphson", "Bisection", "uniroot (Brent)"),
  Akar = sprintf("%.11f", c(res_nr1$root, res_bis$root, res_uni$root)),
  Jumlah_Iterasi = c(res_nr1$iter, res_bis$iter, res_uni$iter)
)
##            Metode          Akar Jumlah_Iterasi
## 1  Newton-Raphson 1.52137970680              3
## 2       Bisection 1.52137970680             33
## 3 uniroot (Brent) 1.52137970680              8
# Grafik Fungsi f(x) = x^3 - x - 2
curve(f1(x), from = 1, to = 2, col = "#0072B2", lwd = 2, 
      main = "Grafik f(x) = x^3 - x - 2", xlab = "x", ylab = "f(x)")
abline(h = 0, lty = 2, col = "gray50")
points(res_uni$root, 0, col = "#D55E00", pch = 19, cex = 1.5)
legend("topleft", legend = c("f(x)", "Akar Persamaan"), col = c("#0072B2", "#D55E00"), lty = c(1, NA), pch = c(NA, 19), lwd = c(2, NA))
grid()

Interpretasi:
Ketiga metode menghasilkan akar hampiran yang konsisten yaitu sekitar \(1.52137970680\). Metode Newton-Raphson sangat cepat (4 iterasi) karena konvergensi kuadratik, sedangkan Bisection membutuhkan 34 iterasi. Fungsi uniroot juga sangat efisien dengan 5 iterasi tanpa memerlukan turunan analitik.


2. Kuantil Distribusi \(t\) (\(t_{0.975}\))

Soal: Cari \(t_{0.975}\) untuk \(\text{df} = 9\) dengan Newton (\(f = \text{pt}(x, 9) - 0.975\), \(f' = \text{dt}(x, 9)\)), bandingkan dengan qt(0.975, 9). Coba \(x_0 = 10\) dan jelaskan hasilnya[cite: 1, 2].

f_t <- function(x) pt(x, df = 9) - 0.975
df_t <- function(x) dt(x, df = 9)

res_t_x2 <- newton_raphson(f_t, df_t, x0 = 2, tol = 1e-10)
res_t_x10 <- tryCatch(newton_raphson(f_t, df_t, x0 = 10, tol = 1e-10), error = function(e) list(root = NA, iter = NA))
exact_qt <- qt(0.975, df = 9)

cat("Newton (x0 = 2)  : Akar =", sprintf("%.10f", res_t_x2$root), "| Iterasi =", res_t_x2$iter, "\n")
## Newton (x0 = 2)  : Akar = 2.2621571628 | Iterasi = 4
cat("Newton (x0 = 10) : Akar =", res_t_x10$root, "| Iterasi =", res_t_x10$iter, "\n")
## Newton (x0 = 10) : Akar = NA | Iterasi = NA
cat("Eksak qt(0.975,9):", sprintf("%.10f", exact_qt), "\n")
## Eksak qt(0.975,9): 2.2621571628
# Grafik Kurva Distribusi t (df = 9) dan Titik Kuantil 0.975
curve(dt(x, df = 9), from = -4, to = 4, col = "#E69F00", lwd = 2,
      main = "Kuantil t_{0.975} dengan df = 9", xlab = "x", ylab = "Densitas")
x_fill <- seq(exact_qt, 4, length.out = 100)
polygon(c(exact_qt, x_fill, 4), c(0, dt(x_fill, df = 9), 0), col = rgb(0.9, 0.6, 0, 0.3), border = NA)
abline(v = exact_qt, col = "#009E73", lty = 2, lwd = 2)
legend("topright", legend = c("PDF t(df=9)", "Area 0.025 (Kuantil)"), col = c("#E69F00", "#009E73"), lwd = 2)
grid()

Interpretasi:
Dengan tebakan awal dekat pusat (\(x_0 = 2\)), metode Newton sukses menemukan kuantil \(2.2621571628\). Sebaliknya, dengan tebakan ekstrem \(x_0 = 10\), algoritma gagal konvergen karena nilai turunan peluang \(\text{dt}(10, 9)\) mendekati nol, menyebabkan langkah koreksi melompat secara ekstrem ke luar wilayah konvergensi.


BAGIAN 2: INTEGRASI NUMERIK (Tugas Modul Integrasi)

1. Aturan Trapezoidal dan Simpson

Soal: Hitung \(\int_0^\pi \sin x \, dx = 2\) dengan \(n = 4, 8, 16, 32\). Tabelkan galat dan rasio antar-\(n\); apakah mendekati \(4\) dan \(16\)?[cite: 3]

trapezoidal <- function(f, a, b, n) {
  h <- (b - a) / n
  x <- seq(a, b, length.out = n + 1)
  y <- f(x)
  return((h / 2) * (y[1] + 2 * sum(y[2:n]) + y[n + 1]))
}

simpson <- function(f, a, b, n) {
  if (n %% 2 != 0) stop("n harus bilangan genap!")
  if (n < 4) {
    h <- (b - a) / n
    x <- seq(a, b, length.out = n + 1)
    y <- f(x)
    return((h / 3) * (y[1] + 4 * sum(y[2:n]) + y[n + 1]))
  }
  h <- (b - a) / n
  x <- seq(a, b, length.out = n + 1)
  y <- f(x)
  idx_ganjil <- seq(2, n, by = 2)
  idx_genap  <- seq(3, n - 1, by = 2)
  return((h / 3) * (y[1] + 4 * sum(y[idx_ganjil]) + 2 * sum(y[idx_genap]) + y[n + 1]))
}

n_vals <- c(4, 8, 16, 32)
eksak <- 2
err_trap <- sapply(n_vals, function(n) abs(eksak - trapezoidal(sin, 0, pi, n)))
err_simp <- sapply(n_vals, function(n) abs(eksak - simpson(sin, 0, pi, n)))
rasio_trap <- c(NA, err_trap[-length(err_trap)] / err_trap[-1])
rasio_simp <- c(NA, err_simp[-length(err_simp)] / err_simp[-1])

data.frame(
  n = n_vals,
  Galat_Trapezoidal = sprintf("%.8e", err_trap),
  Rasio_Trapezoidal = round(rasio_trap, 4),
  Galat_Simpson     = sprintf("%.8e", err_simp),
  Rasio_Simpson     = round(rasio_simp, 4)
)
##    n Galat_Trapezoidal Rasio_Trapezoidal  Galat_Simpson Rasio_Simpson
## 1  4    1.03881102e-01                NA 4.55975498e-03            NA
## 2  8    2.57683981e-02            4.0313 2.69169948e-04       16.9401
## 3 16    6.42965623e-03            4.0077 1.65910479e-05       16.2238
## 4 32    1.60663903e-03            4.0019 1.03336941e-06       16.0553
# Grafik Perbandingan Galat Trapezoidal vs Simpson (Skala Log)
plot(n_vals, err_trap, type = "b", log = "y", col = "#D55E00", lwd = 2, pch = 16,
     ylim = c(min(err_simp), max(err_trap)),
     xlab = "Jumlah Partisi (n)", ylab = "Galat Mutlak (skala log)",
     main = "Konvergensi Galat: Trapezoidal vs Simpson")
lines(n_vals, err_simp, type = "b", col = "#0072B2", lwd = 2, pch = 17)
legend("topright", legend = c("Trapezoidal", "Simpson"), col = c("#D55E00", "#0072B2"), pch = c(16, 17), lwd = 2)
grid()

Interpretasi:
Peningkatan \(n\) memperkecil galat secara konsisten. Rasio galat Trapezoidal terbukti konvergen mendekati 4 (orde \(O(h^2)\)), sedangkan Simpson mendekati 16 (orde \(O(h^4)\)), memverifikasi teori konvergensi integrasi numerik.


2. Nilai Harapan \(E(X)\) Distribusi Gamma

Soal: Untuk \(X \sim \text{Gamma}(\text{shape} = 3, \text{rate} = 2)\), hitung \(E(X) = \int_0^\infty x f(x) \, dx\) dengan simpson() pada \([0, 20]\) (\(\text{tol} = 10^{-8}\)), lalu bandingkan dengan \(1.5\) dan integrate()[cite: 3].

simpson <- function(f, a, b, n) {
  if (n %% 2 != 0) stop("n harus bilangan genap!")
  if (n < 4) {
    h <- (b - a) / n
    x <- seq(a, b, length.out = n + 1)
    y <- f(x)
    return((h / 3) * (y[1] + 4 * sum(y[2:n]) + y[n + 1]))
  }
  h <- (b - a) / n
  x <- seq(a, b, length.out = n + 1)
  y <- f(x)
  idx_ganjil <- seq(2, n, by = 2)
  idx_genap  <- seq(3, n - 1, by = 2)
  return((h / 3) * (y[1] + 4 * sum(y[idx_ganjil]) + 2 * sum(y[idx_genap]) + y[n + 1]))
}

integran_gamma <- function(x) x * dgamma(x, shape = 3, rate = 2)

n <- 2
s_prev <- simpson(integran_gamma, 0, 20, n)
repeat {
  n <- n * 2
  s_curr <- simpson(integran_gamma, 0, 20, n)
  if (abs(s_curr - s_prev) < 1e-8) break
  s_prev <- s_curr
}

nilai_teoretis <- 3 / 2
res_integrate <- integrate(integran_gamma, lower = 0, upper = Inf)

data.frame(
  Metode = c("Teoretis", paste0("Simpson (n=", n, ")"), "integrate()"),
  Nilai_EX = sprintf("%.12f", c(nilai_teoretis, s_curr, res_integrate$value)),
  Galat_Absolut = sprintf("%.4e", abs(nilai_teoretis - c(nilai_teoretis, s_curr, res_integrate$value)))
)
##             Metode       Nilai_EX Galat_Absolut
## 1         Teoretis 1.500000000000    0.0000e+00
## 2 Simpson (n=4096) 1.499999999924    7.5856e-11
## 3      integrate() 1.500000003181    3.1814e-09
# Grafik Fungsi Densitas x * f(x) untuk E(X) Gamma(3, 2)
curve(integran_gamma(x), from = 0, to = 10, col = "#CC79A7", lwd = 2,
      main = "Integran Nilai Harapan E(X) ~ Gamma(shape=3, rate=2)",
      xlab = "x", ylab = "x * f(x)")
polygon(c(0, seq(0, 10, length.out = 100), 10), c(0, integran_gamma(seq(0, 10, length.out = 100)), 0), col = rgb(0.8, 0.4, 0.6, 0.2), border = NA)
abline(v = nilai_teoretis, col = "red", lty = 2, lwd = 2)
legend("topright", legend = c("x * f(x)", "Nilai Harapan E(X) = 1.5"), col = c("#CC79A7", "red"), lty = c(1, 2), lwd = 2)
grid()

Interpretasi:
Pemotongan batas atas integral pada \(b = 20\) sudah sangat akurat karena ekor distribusi Gamma bernilai mendekati nol. Fungsi simpson() mencapai konvergensi dengan hasil yang identik dengan nilai teoretis \(1.5\) maupun fungsi integrate().

getwd()