library(deSolve) library(ggplot2) # Define the ODE system function simple_ode <- function(time, state, parameters) { x <- state[1] # State variable k <- parameters[1] # Parameter # Condition where x is below 0.5 and dx/dt is negative if (x < 0.5 && k * x < 0) { dx <- 1 # Set dx to 1 } else { dx <- -k * x # Exponential decay } # Condition where x is below 0.9 if (x < 0.9) { dx <- 1 # Set dx to 1 } return(list(dx)) } # Initial state and parameter initial_state <- c(x = 1) # Initial value of x parameters <- c(k = 0.1) # Decay rate # Time range time <- seq(0, 100, by = 0.1) # Time range from 0 to 10 with a step size of 0.1 # Solve the ODE system solution <- ode(y = initial_state, times = time, func = simple_ode, parms = parameters) # Create a data frame from the solution df <- as.data.frame(solution) # Plot the state variable over time plot <- ggplot(df, aes(x = time, y = x)) + geom_line() + xlab("Time") + ylab("x") + ylim(0,5)+ ggtitle("Simple ODE System") + theme_minimal() # Save the plot as a PDF file ggsave("simple_ode_plot.pdf", plot, width = 8, height = 6)