Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+EThis is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code. nter.
#01
exp_maclaurin <- function(x, n){
hasil <- 0
for(k in 0:n){
hasil <- hasil + (x^k)/factorial(k)
}
return(hasil)
}
# Contoh:
exp_maclaurin(-1, 5)
[1] 0.3666667
sin_maclaurin <- function(x){
hasil <- 0
k <- 0
suku <- x # suku pertama
while(abs(suku) >= 1e-5){
hasil <- hasil + suku
k <- k + 1
suku <- (-1)^k * x^(2*k+1) / factorial(2*k+1)
}
return(hasil)
}
# Contoh:
sin_maclaurin(pi/6)
[1] 0.5000021
sin_detail <- function(x){
k <- 0
repeat{
suku <- ((-1)^k) * x^(2*k + 1) / factorial(2*k + 1)
cat("Suku ke-", k, ":", suku, "\n")
if(abs(suku) < 1e-5){
break
}
k <- k + 1
}
}
# Jalankan:
sin_detail(pi/6)
Suku ke- 0 : 0.5235988
Suku ke- 1 : -0.0239246
Suku ke- 2 : 0.0003279532
Suku ke- 3 : -2.14072e-06
#02
data <- c(4.1, 4.9, 6.2, 6.9, 6.8, 4.4, 5.7, 5.8, 6.9, 4.7, 6.0, 4.9)
moving_average <- function(data, k){
n <- length(data)
hasil <- rep(NA, n)
for(t in k:n){
hasil[t] <- mean(data[(t-k+1):t])
}
return(hasil)
}
Mt3 <- moving_average(data, 3)
hasil <- data.frame(
t = 1:length(data),
Data = data,
Mt3 = round(Mt3, 2)
)
print(hasil)
t Data Mt3
1 1 4.1 NA
2 2 4.9 NA
3 3 6.2 5.07
4 4 6.9 6.00
5 5 6.8 6.63
6 6 4.4 6.03
7 7 5.7 5.63
8 8 5.8 5.30
9 9 6.9 6.13
10 10 4.7 5.80
11 11 6.0 5.87
12 12 4.9 5.20
<!-- rnb-source-end -->
<!-- rnb-output-end -->
<!-- rnb-output-begin eyJkYXRhIjoiRXJyb3I6IGF0dGVtcHQgdG8gdXNlIHplcm8tbGVuZ3RoIHZhcmlhYmxlIG5hbWVcbiJ9 -->
Error: attempt to use zero-length variable name ```
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.