2015-10-04 52 views
0

我想運行求解&將我的ode解決方案繪製爲以下三個不同的時間,每次只改變一個初始條件。我想解決q.0 = 16,q.0 = 20,q.0 = 18用多組初始條件解決R中的ODE

(這將是很好,如果這都繪製在矩陣..!在此先感謝)

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") 
+1

而問題是......? – nicola

+0

我想運行這三次而不必手動更改q.0變量。就個人而言,我會通過在q.0 = 16,18和20的實例中循環包裝來強制它,但我想有人會有更優雅的解決方案...使用apply?也許.. – deposition

回答

-1

或者說,還是有點簡單:運行它爲q.0的第一個值,但使用lines來繪製另外兩個運行,以便您可以在同一個圖上看到所有三個解決方案。