Professional Documents
Culture Documents
Komstat S2 - Tugas M6 - Kel 5 - Devi-Diego-Djihan-Wulaida
Komstat S2 - Tugas M6 - Kel 5 - Devi-Diego-Djihan-Wulaida
Oleh :
Kelompok 5
Devi Azarina Manzilir Rohmah (206090500011001)
Diego Irsandy (206090500011006)
Djihan Wahyuni (2046000216)
Wulaida Rizky Fitrilia (2046000219)
{
, 1≤ x ≤ 4
12
f ( x )= 9 − x
,4≤ x≤9
20
0 , selainnya
JAWABAN
1. Perhitungan Manual
2. Software R
Script Software R
library(dplyr)
#Memasukkan Fungsi
fungsi5 <- function(x){
bb <<- 1
ba <<- 9
d <<- 4
y <- NULL
for (i in 1:length(x)){
if (x[i] >= bb & x[i] < d ){
y[i] <- (x[i] - 1)/12
} else if (x[i] >= d & x[i] <= ba){
y[i] <- (9 - x[i])/20
} else {
y[i] <- 0
}
}
return(y)
}
#==================================================================#
#Plot XY dan Menentukan Nilai M
{
x <- seq(from = 0, to = 10 , by = 0.5)
y <- fungsi5(x)
plot(x = x, y = y, type = "l",
col = "dark red", lwd = 2, xlab = "x", ylab = "f(x)")
M <- max(y); M
}
#==================================================================#
#Membangkitkan Peubah Acak
df <- data.frame(); count <- 0
while (count < 10000) {
Kep <- ""
R <- runif(2,0,1)
xstar <- bb + (ba - bb) * R[1]
f.xstar <- fungsi5(xstar)
Kep <- ifelse(R[2] <= f.xstar / M, "Accepted", "Rejected")
nrow(df)
head(df,10)
df %>%
filter(Keputusan == "Accepted") %>%
head(10)
df %>%
filter(Keputusan == "Accepted") %>%
pull(xstar) %>%
hist(main = "Histogram Data Bangkitan", breaks = 0:10)
Penjelasan Script Software R
No. Source Code Penjelasan
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
Output Software R
> library(dplyr)
>
> #Memasukkan Fungsi
> fungsi5 <- function(x){
+ bb <<- 1
+ ba <<- 9
+ d <<- 4
+ y <- NULL
+ for (i in 1:length(x)){
+ if (x[i] >= bb & x[i] < d ){
+ y[i] <- (x[i] - 1)/12
+ } else if (x[i] >= d & x[i] <= ba){
+ y[i] <- (9 - x[i])/20
+ } else {
+ y[i] <- 0
+ }
+ }
+ return(y)
+ }
> #==================================================================#
> #Plot XY dan Menentukan Nilai M
> {
+ x <- seq(from = 0, to = 10 , by = 0.5)
+ y <- fungsi5(x)
+ plot(x = x, y = y, type = "l",
+ col = "dark red", lwd = 2, xlab = "x", ylab = "f(x)")
+ M <- max(y); M
+ }
[1] 0.25
> #==================================================================#
> #Membangkitkan Peubah Acak
> df <- data.frame(); count <- 0
> while (count < 10000) {
+ Kep <- ""
+ R <- runif(2,0,1)
+ xstar <- bb + (ba - bb) * R[1]
+ f.xstar <- fungsi5(xstar)
+ Kep <- ifelse(R[2] <= f.xstar / M, "Accepted", "Rejected")
+
+ x <- data.frame(R1 = R[1], R2= R[2],
+ xstar = xstar, f.xstar = f.xstar,
+ Keputusan = Kep)
+ df <- rbind(df, x)
+ count <- df %>%
+ filter(Keputusan == "Accepted") %>%
+ nrow()
+ }
>
> nrow(df)
[1] 20056
> head(df,10)
R1 R2 xstar f.xstar Keputusan
1 0.6340730 0.6278747 6.072584 0.14637082 Rejected
2 0.2399080 0.3037764 2.919264 0.15993867 Accepted
3 0.1078404 0.4393013 1.862723 0.07189361 Rejected
4 0.4284939 0.8549380 4.427951 0.22860243 Accepted
5 0.3604782 0.2858800 3.883825 0.24031879 Accepted
6 0.7307603 0.4823934 6.846083 0.10769587 Rejected
7 0.5910222 0.9747402 5.728178 0.16359111 Rejected
8 0.8611306 0.8731174 7.889045 0.05554777 Rejected
9 0.8911275 0.4120257 8.129020 0.04354901 Rejected
10 0.7192885 0.2887222 6.754308 0.11228461 Accepted
>
>
> df %>%
+ filter(Keputusan == "Accepted") %>%
+ head(10)
R1 R2 xstar f.xstar Keputusan
1 0.23990800 0.3037764 2.919264 0.15993867 Accepted
2 0.42849392 0.8549380 4.427951 0.22860243 Accepted
3 0.36047819 0.2858800 3.883825 0.24031879 Accepted
4 0.71928847 0.2887222 6.754308 0.11228461 Accepted
5 0.51128960 0.3359755 5.090317 0.19548416 Accepted
6 0.03853952 0.1023425 1.308316 0.02569302 Accepted
7 0.34775414 0.1612560 3.782033 0.23183609 Accepted
8 0.38618372 0.9077515 4.089470 0.24552651 Accepted
9 0.57836369 0.6517089 5.626909 0.16865453 Accepted
10 0.42976167 0.5825213 4.438093 0.22809533 Accepted
>
> df %>%
+ filter(Keputusan == "Accepted") %>%
+ pull(xstar) %>%
+ hist(main = "Histogram Data Bangkitan", breaks = 0:10)
>