# Exponential ODE with Euler's method import numpy as np import matplotlib.pyplot as plt k = 1.3 tfinal = 10 dt = 1.0 steps = int(tfinal / dt) N = np.zeros(steps+1) t = np.zeros(steps+1) N[0] = 2.0 t[0] = 0.0 for i in range(steps): dN = k * N[i] * dt N[i+1] = N[i] + dN t[i+1] = t[i] + dt # Display data for i in range(steps+1): print( f"{t[i]:.1f} \t {N[i]:.2f}") # plot results plt.plot(t,N) plt.xlabel('Time') plt.ylabel('N') plt.show() """ # Save results to a file np.savetxt('ExpODEEulers.txt', np.transpose([t,N]), fmt='%4.1f, %8.4f', newline='\n') """