# Eulers metode for \dot x = x cos t. Vi sammenlikner numerisk og analytisk losning. import numpy as np import matplotlib.pyplot as plt x0 = 1 dt = 0.1 tMax = 20 n = int(tMax/dt) t = np.zeros(n) dx = np.zeros(n) x = np.zeros(n) x[0] = x0 t[0] = 0 for i in range(n-1): t[i+1] = t[i] + dt dx[i] = np.cos(t[i])*x[i] x[i+1] = x[i] + dx[i]*dt plt.plot(t,x,linewidth=2, label='Eulers metode') plt.plot(t,x0*np.exp(np.sin(t)),linewidth=2, label='Analytisk l?sning') plt.legend(loc='upper right') plt.xlabel('t') plt.ylabel('x(t)') plt.savefig('euler.pdf') plt.show()