import numpy as np import matplotlib.pyplot as plt A = 0.3 # The A constant in the ODE t0 = 0. # Starting time tend = 10. # End time nt = 10 # Nr of time steps t = np.linspace(t0,tend,nt) y = np.zeros(nt) y[0] = 2. # Intial condition for it in range(0,nt-1): y[it+1] = y[it] - A * y[it] * ( t[it+1] - t[it] ) yana = y[0]*np.exp(-A*t) plt.figure() plt.plot(t,yana,label='Analytic') plt.plot(t,y,'o',label='Numeric') plt.xlabel('t') plt.ylabel('y(t)') plt.legend() plt.savefig('fig_ode_euler_1_1.pdf') plt.show()