This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

# 1.  Eksponensial 

exp_maclaurin <- function(x, n) {
  hasil <- 0
  
  for (i in 0:n) {
    hasil <- hasil + (x^i) / factorial(i)
  }

  return(hasil)
}
x1 <- -1
n1 <- 5
hasil_exp <- exp_maclaurin(x1, n1)
print(hasil_exp)
## [1] 0.3666667
#sin maclaurin
sin_maclaurin <- function(x, tol = 1e-5) {
  hasil <- 0
  i <- 0
  term <- x
  
  while (abs(term) > tol) {
    term <- ((-1)^i * x^(2*i + 1)) / factorial(2*i + 1)
    hasil <- hasil + term
    i <- i + 1
  }
  
  return(hasil)
}

x2 <- pi/6
hasil_sin <- sin_maclaurin(x2)
print(hasil_sin)
## [1] 0.5
# rata rata bergerak
moving_average <- function(data, k) {
  n <- length(data)
  hasil <- rep(NA, n)  # supaya panjang sama dengan data
  
  for (i in k:n) {
    hasil[i] <- mean(data[(i-k+1):i])
  }
  
  return(hasil)
}

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)

k <- 3

M3 <- moving_average(data, k)

t <- 1:length(data)

hasil <- data.frame(
  t = t,
  Data = data,
  M3 = round(M3, 2)
)

print(hasil)
##     t Data   M3
## 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

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+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 Cmd+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.