import matplotlib.pyplot as plt from numpy import * def f(t,x): return t + x def x_exact(t): return -1-t + exp(t) a = 0 b = 3 # Number of steps N = 60 # Step length h = (b - a)/float(N) x = zeros(N+1) t = zeros(N+1) x[0] = 0 # Initial condition t[0] = a for k in range(N): x[k+1] = x[k] + h*f(t[k], x[k]) t[k+1] = t[k] + h t_fine = linspace(a,b, 101) plt.figure() plt.plot(t, x, label='Euler') plt.plot(t_fine, x_exact(t_fine), label='x(t)') plt.xlabel('t') plt.legend() plt.show()