# オイラー法 # 関数 f <- function(x, t) { return(4.9 - 9.8 * t) } # tの範囲 a <- 0 b <- 1 # 初期値 x0 <- 0 t0 <- a # 区間数 n <- 10 # 刻み幅 h <- (b - a) / n # x,tのベクトル x <- c(x0) t <- c(t0) # オイラー法による繰り返し xi <- x0 ti <- t0 for (i in 1 : n) { xi <- xi + f(xi, ti) * h ti <- a + i * h x <- c(x, xi) t <- c(t, ti) } # 結果の出力 print(sprintf("h = %f", h), quote = FALSE) print(sprintf("t = %f, x(t) = %f", t[n + 1], x[n + 1]), quote = FALSE) # 解との比較 plot(t, x, type = "o", pch = 18) curve(4.9 * t - 0.5 * 9.8 * t ^ 2, xname = "t", add = TRUE, col = "red")