From 7b48a4a5e29bb6439e3cc2af32fe2d8f5bd9c015 Mon Sep 17 00:00:00 2001 From: noah Date: Wed, 15 Feb 2023 19:39:47 -0600 Subject: [PATCH] Decomposition Modeling --- Schrick-Noah_Homework-3.R | 49 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Schrick-Noah_Homework-3.R b/Schrick-Noah_Homework-3.R index e9d6a3e..6daeec3 100644 --- a/Schrick-Noah_Homework-3.R +++ b/Schrick-Noah_Homework-3.R @@ -329,7 +329,54 @@ sir.ode.sol.b <- ode45(f = function(t,y){sir.f(t,y,k=sir.params.b)}, plot.sir(sir.ode.sol.b, "ODE45 with a=3") ## 6. Decomp of dinitrogen pentoxygen into nitrogen dioxide and molecular oxygen +decomp.f <- function(t, y, k) { + dA <- -k[1]*y[1] + k[2]* y[2]*y[4] + dB <- k[1]*y[1] - k[2]*y[2]*y[4] + 2*k[4]*y[4]*y[5] + dC <- k[3]*y[2]*y[4] + dD <- k[1]*y[1] - k[2]*y[2]*y[4] - k[3]*y[2]*y[4] - k[4]*y[4]*y[5] + dE <- k[3]*y[2]*y[4] - k[4]*y[4]*y[5] + return(as.matrix(c(dA, dB, dC, dD, dE))) +} # a) k1=1.0, k2=0.5, k3=0.2,k4=1.5, [N2O5]o=1, all other IC's=0, t=0 -> 10 +decomp.params <- c(1.0, 0.5, 0.2, 1.5) +A0 <- 1 +B0 <- 0; C0 <- 0; D0 <- 0; E0 <- 0 +tmin <- 0 +tmax <- 10 -# b) Increase k4 to make the intermediate species -> 0 \ No newline at end of file +decomp.ode.sol <- ode45(f = function(t,y){decomp.f(t,y,k=decomp.params)}, + y = c(A0, B0, C0, D0, E0), + t0 = tmin, tfinal = tmax) + +# plot +plot.decomp <- function(sol, method){ + # - pred/prey columns melted to 1 column called "value" + sol.melted <- melt(data.frame(time=sol$t, + N2O5=sol$y[,1], + NO2=sol$y[,2], + O2=sol$y[,3], + NO3=sol$y[,4], + NO=sol$y[,5]), + id = "time") # melt based on time + # New column created with variable names, called “variable” + colnames(sol.melted)[2] <- "Group" # used in legend + + g <- ggplot(data = sol.melted, + aes(x = time, y = value, color = Group)) + g <- g + geom_point(size=2) + g <- g + xlab("time [seconds]") + ylab("Concentration") + g <- g + ggtitle(paste("Decomposition Model Using", method)) + show(g) +} + +plot.decomp(decomp.ode.sol, "ODE45") + +# b) Increase k4 to make the intermediate species -> 0 +k4 <- 10 +decomp.params.b <- c(1.0, 0.5, 0.2, k4) +decomp.ode.sol.b <- ode45(f = function(t,y){decomp.f(t,y,k=decomp.params.b)}, + y = c(A0, B0, C0, D0, E0), + t0 = tmin, tfinal = 100) +plot.decomp(decomp.ode.sol.b, paste("ODE45 Using k4 =", k4)) +decomp.ode.sol.b$y[,5]