# 台形公式 # 被積分関数の定義 f <- function(x) { return(4 * sqrt(1 - x ^ 2)) } # 区間数 n <- 1000 # 区間 a <- 0 b <- 1 # 幅 h <- (b - a) / n s <- 0.5 * (f(a) + f(b)) for (i in 1 : (n - 1)) { x <- a + i * h s <- s + f(x) } s <- s * h print(sprintf("n = %d", n), quote = FALSE) print(sprintf("h = %f", h), quote = FALSE) print(sprintf("S = %f", s), quote = FALSE)