# Exponential growth model solve.ivp import numpy as np import matplotlib.pyplot as plt from scipy.integrate import solve_ivp # n - population size def ExponentialModel(t, n): r = 1.2 return r*n n0 = 2.0 t0 = 0 tf = 10 steps = 1*tf sol = solve_ivp(ExponentialModel, [t0, tf], [n0], method='RK45', rtol=1e-6, atol=1e-9, dense_output=True) t = np.linspace(t0,tf,steps+1) n = sol.sol(t) # Display data for i in range(steps+1): print( f"{t[i]:.1f} \t {n[0][i]:.4f}") # plot results plt.plot(t, n[0]) plt.xlabel('Time') plt.ylabel('Population size') plt.savefig('ExpSolveIVP.svg') # Save results to a file f = open('ExpODE_SolveIVP.txt', "w") for i in range(0, steps+1, 1): print("%5.1f, %6.4f" % (t[i], n[0][i]), file=f) f.close()