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