[英]Solving ODE in R with multiple sets of initial conditions
I would like to run solve & plot my ode solution to the following, three different times, chaning a single initial condition each time. 我想对以下三个不同的时间运行我的ode解决方案并将其绘制出来,每次都改变一个初始条件。 I would like to solve for q.0 = 16, q.0 = 20, q.0 = 18.
我想解决q.0 = 16,q.0 = 20,q.0 = 18。
(It would be nice if this plotted all in a matrix..! Thanks in advance) (如果将其全部绘制在一个矩阵中,那就太好了。
rm(list = ls())
par(mfrow = c(1,1))
D = 6 ; A = pi* D^2 / 4 ;
# ODE solver
library(deSolve)
parameters <- c(A,
k = (16/sqrt(5)),
q.0 = 16,
h.0 = 5
)
state <- c(h = 5)
Model <- function(t, state, parameters) {
with(as.list(c(state, parameters)),{
# rate of change
dh <- q.0/A - k/A * h^(1/2)
# return the rate of change
list(c(dh))
}) # end with(as.list ...
}
times <- seq(0, 100, 1)
out <- ode(y = state, times = times, func = Model, parms = parameters)
par(oma = c(0, 0, 3, 0))
plot(out, xlab = "time", ylab = "-")
plot(out[, "time"], out[, "h"], pch = ".",
xlab = "time [h]",
ylab = "Height [ft]")
mtext(outer = TRUE, side = 3, "Variable Holdup Tank Model", cex = 1.5)
write.table(out, "mydataCSTR.txt", sep="\t")
或者,并且仍然很简单:以q.0的第一个值运行它,但使用lines
绘制其他两个运行图,以便您可以在同一图形上看到所有三个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.