library(deSolve) library(ggplot2) # Define the differential equation function bank_account <- function(time, state, parameters) { with(as.list(c(state, parameters)), { # Define the differential equation dBalance <- InterestRate * Balance # Return the derivatives return(list(c(dBalance))) }) } # Set the initial conditions and parameters initial_balance <- 1000 # Initial balance of the bank account interest_rate <- 0.05 # Annual interest rate # Create the parameter list parameters <- list( InterestRate = interest_rate ) # Create the initial state vector initial_state <- c( Balance = initial_balance ) # Set the time span for the simulation time_span <- c(0, 10) # Simulation for 10 years # Solve the differential equation solution <- ode( y = initial_state, times = time_span, func = bank_account, parms = parameters ) # Access the results time <- solution[, 1] balance <- solution[, 2] # Create a data frame with the time and balance data <- data.frame(time = time, balance = balance) # Create the plot using ggplot2 plot <- ggplot(data, aes(x = time, y = balance)) + geom_line() + labs(x = "Time", y = "Bank Account Balance") + theme_minimal() # Save the plot as a PDF file ggsave("bank_account_plot.pdf", plot, width = 8, height = 6)