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:
#Tugas 12
#Muhammad Fahrillah Haekal Yahya
#3338250043, 3B
library(rlang)
## Warning: package 'rlang' was built under R version 4.5.2
library(rmarkdown)
library(rsconnect)
trapezoid <- function(ftn, a, b, n = 100) {
h <- (b - a) / n
x.vec <- seq(a, b, by = h)
f.vec <- sapply(x.vec, ftn)
T <- h * (f.vec[1]/2 + sum(f.vec[2:n]) + f.vec[n+1]/2)
return(T)
}
simpson_n <- function(ftn, a, b, n = 100) {
n <- max(c(2*(n %/% 2), 4))
h <- (b - a) / n
x.vec1 <- seq(a + h, b - h, by = 2*h)
x.vec2 <- seq(a + 2*h, b - 2*h, by = 2*h)
f.vec1 <- sapply(x.vec1, ftn)
f.vec2 <- sapply(x.vec2, ftn)
S <- h/3 * (ftn(a) + ftn(b) + 4*sum(f.vec1) + 2*sum(f.vec2))
return(S)
}
simpson <- function(ftn, a, b, tol = 1e-8, verbose = FALSE) {
n <- 4; h <- (b - a) / 4
fx <- sapply(seq(a, b, by = h), ftn)
S <- sum(fx * c(1, 4, 2, 4, 1)) * h / 3
S.diff <- tol + 1
while (S.diff > tol) {
S.old <- S; n <- 2*n; h <- h/2
fx[seq(1, n+1, by = 2)] <- fx
fx[seq(2, n, by = 2)] <- sapply(seq(a+h, b-h, by = 2*h), ftn)
S <- h/3 * (fx[1] + fx[n+1] + 4*sum(fx[seq(2, n, by = 2)]) +
2*sum(fx[seq(3, n-1, by = 2)]))
S.diff <- abs(S - S.old)
}
if (verbose) cat("partition size", n, "\n")
return(S)
}
#Soal 1
f1 <- function(x) sin(x)
ns <- c(4, 8, 16, 32)
T_n <- sapply(ns, function(n) trapezoid(f1, 0, pi, n))
S_n <- sapply(ns, function(n) simpson_n(f1, 0, pi, n))
galT <- abs(T_n - 2)
galS <- abs(S_n - 2)
rasioT <- c(NA, galT[-4] / galT[-1])
rasioS <- c(NA, galS[-4] / galS[-1])
hasil1 <- data.frame(n = ns, Trapezoidal = T_n, Galat_T = galT, Rasio_T = rasioT,
Simpson = S_n, Galat_S = galS, Rasio_S = rasioS)
print(hasil1, digits = 7)
## n Trapezoidal Galat_T Rasio_T Simpson Galat_S Rasio_S
## 1 4 1.896119 0.103881102 NA 2.004560 4.559755e-03 NA
## 2 8 1.974232 0.025768398 4.031337 2.000269 2.691699e-04 16.94006
## 3 16 1.993570 0.006429656 4.007741 2.000017 1.659105e-05 16.22381
## 4 32 1.998393 0.001606639 4.001930 2.000001 1.033369e-06 16.05529
#soal 2
f_gamma <- function(x) x * dgamma(x, shape = 3, rate = 2)
E_simpson <- simpson(f_gamma, 0, 20, tol = 1e-8, verbose = TRUE)
## partition size 4096
i_b <- integrate(f_gamma, 0, 20)
i_inf <- integrate(f_gamma, 0, Inf)
print(c(Simpson = E_simpson, integrate_0_20 = i_b$value,
integrate_0_Inf = i_inf$value, Teoretis = 3/2), digits = 13)
## Simpson integrate_0_20 integrate_0_Inf Teoretis
## 1.499999999924 1.500000000000 1.500000003181 1.500000000000
E_simpson - 1.5 # selisih terhadap 1,5
## [1] -7.585577e-11
(3/2) * pgamma(20, 4, 2, lower.tail = FALSE) # bagian E(X) yang terpotong di b = 20
## [1] 7.333297e-14
#soal 3
f5 <- function(x) 5 * x^4
galat_T <- function(n) abs(trapezoid(f5, 0, 1, n) - 1)
galat_S <- function(n) { # Simpson dengan bobot 1,4,2,...,4,1 (n = 2 boleh)
h <- 1 / n; x <- (0:n) / n
w <- c(1, rep(c(4, 2), length.out = n - 1), 1)
abs(h / 3 * sum(w * f5(x)) - 1)
}
k <- 2:16
eps <- 2^(-k)
n_T <- sapply(eps, function(e) { n <- 2; while (galat_T(n) > e) n <- n + 1; n })
n_S <- sapply(eps, function(e) { n <- 2; while (galat_S(n) > e) n <- n + 2; n })
data.frame(k, eps, n_T, n_S)
## k eps n_T n_S
## 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, n_T, log = "xy", type = "b", pch = 16, col = "blue",
ylim = range(c(n_T, n_S)), xlab = expression(epsilon), ylab = "n terkecil",
main = "n terkecil terhadap epsilon (skala log)")
lines(eps, n_S, type = "b", pch = 17, col = "red")
legend("topright", c("Trapezoidal", "Simpson"), col = c("blue", "red"),
pch = c(16, 17), lty = 1, bty = "n")
idx <- k >= 8
coef(lm(log(n_T[idx]) ~ log(eps[idx])))[2] # kemiringan Trapezoidal
## log(eps[idx])
## -0.4959511
coef(lm(log(n_S[idx]) ~ log(eps[idx])))[2] # kemiringan Simpson
## log(eps[idx])
## -0.2331518
#soal 4
set.seed(2026)
pos <- rnorm(100, mean = 1, sd = 1)
neg <- rnorm(100, mean = 0, sd = 1)
ambang <- c(Inf, sort(unique(c(pos, neg)), decreasing = TRUE))
tpr <- sapply(ambang, function(t) mean(pos >= t))
fpr <- sapply(ambang, function(t) mean(neg >= t))
auc_trap <- sum(diff(fpr) * (head(tpr, -1) + tail(tpr, -1)) / 2)
d <- outer(pos, neg, "-")
p_pos_gt_neg <- mean(d > 0) + 0.5 * mean(d == 0)
auc_teori <- pnorm(1 / sqrt(2))
c(AUC_trapezoidal = auc_trap, P_pos_gt_neg = p_pos_gt_neg, Teoretis = auc_teori)
## AUC_trapezoidal P_pos_gt_neg Teoretis
## 0.7127000 0.7127000 0.7602499
plot(fpr, tpr, type = "s", lwd = 2, col = "blue",
xlab = "FPR", ylab = "TPR", main = "Kurva ROC")
abline(0, 1, lty = 2)
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.