# レポート # シンプソンの公式 # 絶対値を使う方法 # 被積分関数の定義 f <- function(x) { return(abs(-x ^ 4 - x ^ 3 + 10 * x ^ 2 + x - 2)) } # 区間数 n <- 1000 # 区間 a <- -3.63423216189401010467 b <- 2.71669197614524948747 # 幅 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)