I am trying to run the following script, but get the error
IndexError: list index out of range
I have read that this is because when you create a list it is initially empty so you need to assign to it some value which I have done by doing the following
q.append(0)
but I still get the error. Can someone point out what I an doing wrong? Thank you!
import numpy
from numpy import *
import matplotlib.pyplot as plt
pfa = [] #Create lists that will hold pf,qf values
qfa = []
pf = []
qf = []
p = []
q = []
pf.append(0)
qf.append(0)
p.append(0)
q.append(0)
q[0] = -0.5 # initial p and q values
p[0] = 0
h = 0.001
for i in range(10):
k1 = -h*sin(q[i])
j1 = h*(p[i])
k2 = -h*sin(q[i]+(1/2)*j1)
j2 = h*p[i]*(q[i]+(1/2)*k1)
k3 = -h*sin(q[i]+(1/2)*j2)
j3 = h*p[i]*(q[i]+(1/2)*k2)
k4 = -h*sin(q[i]+(1/2)*j3)
j4 = h*p[i]*(q[i]+(1/2)*k3)
pf[i+1] = p[i] +(h/6.0)*(k1+2*k2+2*k3+k4)
qf[i+1] = q[i] +(h/6.0)*(j1+2*j2+2*j3+j4)
pfa.append(pf) #append lists
qfa.append(qf)
plt.plot(qfa,pfa)
plt.show()
the trace back and error
Traceback (most recent call last):
File "C:\Documents and Settings\My Documents\Symplectic Integrators\RK4_2.py", line 23, in <module>
j1 = h*(p[i])
IndexError: list index out of range