More Related Content
Similar to R_note_ODE_ver1.0
Similar to R_note_ODE_ver1.0 (12)
More from Satoshi Kume (20)
R_note_ODE_ver1.0
- 1. Ordinary Differential Equation
(ODE) on R
Satoshi Kume
141021 ver 1.0
1. ODEパッケージのインストール& library
> install.packages("deSolve") # パッケージのインストール
> library(deSolve) # 適用(R立ち上げ毎に行うこと)
2. animationパッケージのインストール& library
> install.packages("animation")
> library(animation)
- 2. 一次反応モデル
速度式の連立方程式
[A]=1, [B]=0, k1=0.05
Rate <- function(t, c, parms){
k1 <- parms$k1
r <- rep(0, length(c))
r[1] = -k1*c["A"]; r[2] = k1*c["A"]
return(list(r))}
cinit <- c(A=1, B=0); t <- seq(0, 8, by=0.1); parms <- list(k1=0.8)
out <- ode(y=cinit, times=t, func=Rate, parms=parms)
head(out)
saveGIF({for(n in 1:nrow(out)){
matplot(out[,1][1:n], data.frame(out[,2][1:n], out[,3][1:n]), type="l", lty=1, lwd=3, col=c("black", "red"), xlab="Time (min)", ylab="", xlim=c(0,8), ylim=c(0,1),
cex.lab=2, cex.axis=2)
legend("right", legend=c("A", "B"), col=c("black", "red"), pch="", lty=1, lwd=3, cex=2)
matpoints(out[,1][n], out[,2][n], pch=16, col="black", cex=2)
matpoints(out[,1][n], out[,3][n], pch=16, col="red", cex=2)
}}, movie.name = "motion01.gif", interval = 0.1, nmax = nrow(out), ani.width = 600, ani.height = 600)