'What's wrong with my Verlet Velocity integration code?
Not sure why the code fails on the verlet part. Would appreciate some help! The error that comes up is "IndexError: list index out of range" (the line under the else statement)
from numpy import cos, array
import matplotlib.pyplot as plt
import numpy as np
#constants
N=500
m=1
k=1
dt=0.1
omega=(k/m)**0.5
#initial conditions
x = [0]
v = [0]
t = [0]
## Velocity Verlet
n=0
while t[-1]<=N:
a=-(k/m)*x[n-1]
a2=-(k/m)*x[n]
if n==0:
v_next=v[n-1]+a*dt
x_next=x[n-1]+v_next*dt
else:
x_next= x[n-1] + dt*v[n-1] + 0.5*(dt**2)*a
v_next = v[n-1] + 0.5*(a+a2)*dt
x.append(x_next)
t.append(t[n-1]+dt)
n=n+1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
