library(deSolve) library(ggplot2) # Load the outflow data from a CSV file outflow_data <- read.csv("outflow_data.csv", header = TRUE) # Extract the times and outflow rates times <- outflow_data$Time outflow <- outflow_data$Outflow # Define the differential equation tank_eqn <- function(time, state, parameters) { with(as.list(c(state, parameters)), { inflow_rate <- 2 # Inflow rate in liters per hour outflow_rate <- outflow[which(times == time)] # Outflow rate in liters per hour dV_dt <- (inflow_rate - outflow_rate) # Rate of change of volume in liters per second return(list(dV_dt)) }) } # Set initial conditions V0 <- 1000 # Initial volume of the tank in liters # Set parameters params <- NULL # Set time interval times <- seq(0, max(times), by = 1) # Time interval from 0 to the maximum time in the outflow data # Solve the differential equation out <- ode(y = V0, times = times, func = tank_eqn, parms = params) # Plot the results plot <- ggplot(data = data.frame(time = out[, 1], volume = out[, 2]), aes(x = time, y = volume)) + geom_line() + xlab("Time (s)") + ylab("Volume (L)") + ggtitle("Volume of Tank Over Time") + theme_minimal() # Save the plot as a PDF file ggsave("tank_volume.pdf", plot, width = 8, height = 6, dpi = 300)