1. Trapezoidal dan Simpson

f1 <- function(x) sin(x)
eksak1 <- 2

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

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

n_vals <- c(4, 8, 16, 32)
galat_trap <- sapply(n_vals, function(n) abs(trapezoidal(f1, 0, pi, n) - eksak1))
galat_simp <- sapply(n_vals, function(n) abs(simpson(f1, 0, pi, n) - eksak1))

rasio_trap <- c(NA, galat_trap[1:3] / galat_trap[2:4])
rasio_simp <- c(NA, galat_simp[1:3] / galat_simp[2:4])

data.frame(
  n = n_vals,
  Galat_Trapezoidal = galat_trap,
  Rasio_Trapezoidal = rasio_trap,
  Galat_Simpson = galat_simp,
  Rasio_Simpson = rasio_simp
)
##    n Galat_Trapezoidal Rasio_Trapezoidal Galat_Simpson Rasio_Simpson
## 1  4       0.103881102                NA  4.559755e-03            NA
## 2  8       0.025768398          4.031337  2.691699e-04      16.94006
## 3 16       0.006429656          4.007741  1.659105e-05      16.22381
## 4 32       0.001606639          4.001930  1.033369e-06      16.05529

2. Nilai Harapan

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

simpson_tol <- function(f, a, b, tol = 1e-8) {
  n <- 2
  prev <- simpson(f, a, b, n)
  repeat {
    n <- n * 2
    curr <- simpson(f, a, b, n)
    if (abs(curr - prev) < tol) {
      return(list(nilai = curr, n_akhir = n))
    }
    prev <- curr
  }
}

res_simp2 <- simpson_tol(f2, 0, 20, tol = 1e-8)
res_int2 <- integrate(f2, lower = 0, upper = Inf)

cat("Nilai Simpson         :", res_simp2[["nilai"]], "(n =", res_simp2[["n_akhir"]], ")\n")
## Nilai Simpson         : 1.5 (n = 4096 )
cat("Nilai integrate()     :", res_int2[["value"]], "\n")
## Nilai integrate()     : 1.5
cat("Nilai Eksak (3/2)     :", 1.5, "\n")
## Nilai Eksak (3/2)     : 1.5
cat("Selisih vs Eksak      :", abs(res_simp2[["nilai"]] - 1.5), "\n")
## Selisih vs Eksak      : 7.585599e-11

3. Memilih n

f3 <- function(x) 5 * x^4
eksak3 <- 1

k_vals <- 2:16
eps_vals <- 2^(-k_vals)

n_T <- numeric(length(eps_vals))
n_S <- numeric(length(eps_vals))

for (i in seq_along(eps_vals)) {
  eps <- eps_vals[i]
  
  n <- 1
  while (abs(trapezoidal(f3, 0, 1, n) - eksak3) > eps) {
    n <- n + 1
  }
  n_T[i] <- n
  
  n <- 2
  while (abs(simpson(f3, 0, 1, n) - eksak3) > eps) {
    n <- n + 2
  }
  n_S[i] <- n
}

hasil_n <- data.frame(k = k_vals, epsilon = eps_vals, n_Trapezoidal = n_T, n_Simpson = n_S)
print(hasil_n)
##     k      epsilon n_Trapezoidal n_Simpson
## 1   2 2.500000e-01             3         2
## 2   3 1.250000e-01             4         2
## 3   4 6.250000e-02             6         2
## 4   5 3.125000e-02             8         4
## 5   6 1.562500e-02            11         4
## 6   7 7.812500e-03            15         4
## 7   8 3.906250e-03            21         4
## 8   9 1.953125e-03            30         6
## 9  10 9.765625e-04            42         6
## 10 11 4.882812e-04            59         8
## 11 12 2.441406e-04            83         8
## 12 13 1.220703e-04           117        10
## 13 14 6.103516e-05           166        12
## 14 15 3.051758e-05           234        14
## 15 16 1.525879e-05           331        16
plot(eps_vals, n_T, log = "xy", type = "b", pch = 16, col = "blue",
     ylim = range(c(n_T, n_S)),
     xlab = "Toleransi Galat (epsilon, skala log)",
     ylab = "Jumlah Subinterval Minimum (n, skala log)",
     main = "Perbandingan n Minimum: Trapezoidal vs Simpson")
lines(eps_vals, n_S, type = "b", pch = 17, col = "red")
legend("topright", legend = c("Trapezoidal (n_T)", "Simpson (n_S)"),
       col = c("blue", "red"), pch = c(16, 17), lty = 1)

4. Proyek Mini: AUC ROC

set.seed(2026)
skor_pos <- rnorm(100, mean = 1, sd = 1)
skor_neg <- rnorm(100, mean = 0, sd = 1)

thresholds <- sort(unique(c(-Inf, skor_pos, skor_neg, Inf)), decreasing = TRUE)
tpr <- sapply(thresholds, function(t) mean(skor_pos >= t))
fpr <- sapply(thresholds, function(t) mean(skor_neg >= t))

ord <- order(fpr, tpr)
fpr_sorted <- fpr[ord]
tpr_sorted <- tpr[ord]

auc_trap <- sum(diff(fpr_sorted) * (tpr_sorted[-1] + tpr_sorted[-length(tpr_sorted)]) / 2)
prob_empiris <- mean(outer(skor_pos, skor_neg, ">")) + 0.5 * mean(outer(skor_pos, skor_neg, "=="))
prob_teoretis <- pnorm(1 / sqrt(2))

cat("AUC Metode Trapezoidal :", auc_trap, "\n")
## AUC Metode Trapezoidal : 0.7127
cat("P(skor+ > skor-) Empiris:", prob_empiris, "\n")
## P(skor+ > skor-) Empiris: 0.7127
cat("P(skor+ > skor-) Teori  :", prob_teoretis, "\n")
## P(skor+ > skor-) Teori  : 0.7602499