# Eulers metode for harmonisk oscillator import numpy as np import matplotlib.pyplot as plt x0 = 2 v0 = 0 dt = 0.05 tMax = 50 # a,v,x er akselerasjon, fart, posisjon n = int(tMax/dt) t = np.zeros(n) a = np.zeros(n) v = np.zeros(n) x = np.zeros(n) x[0] = x0 v[0] = v0 for i in range(n-1): t[i+1] = (i+1)*dt a[i] = -x[i] v[i+1] = v[i] + a[i]*dt x[i+1] = x[i] + v[i]*dt plt.plot(t,x,linewidth=2, label='Eulers metode') plt.plot(t,x0*np.cos(t),linewidth=2, label='Analytisk l?sning') plt.legend(loc='upper left') plt.xlabel('t') plt.ylabel('x(t)') plt.savefig('HOEuler.pdf') plt.show()