import numpy as np import matplotlib.pyplot as plt t_final = 10.0 h = 0.1 t_vec = np.arange(0.0, t_final/h) * h t_steps = len(t_vec) - 1 # print the times print t_vec x0 = 1.0 y0 = 0.0 a = 1.0 b = 0.5 x = x0 y = y0 x_vec = np.zeros((t_steps + 1,)) y_vec = np.zeros((t_steps + 1,)) x_vec[0]=x y_vec[0]=y for k in range(t_steps): x_vec[k+1] = x_vec[k] + h * (-a * x_vec[k]) y_vec[k+1] = y_vec[k] + h * (a * x_vec[k] - b * y_vec[k]) # x = x_vec[k] # y = y_vec[k] # # x_new = x + h *( -a * x) # y_new = y + h *( a * x - b * y) # # x = x_new # y = y_new # x_vec[k+1] = x # y_vec[k+1] = y #print x, y fig = plt.figure() plt.plot(t_vec, x_vec) plt.plot(t_vec, y_vec) plt.legend(['x(t)', 'y(t)']) plt.xlabel('tid') plt.show()