1
I wrote a program in Fortran and now I’m trying to write it in Python. However, it’s giving an error:
D[i][j+1] = - u_med[i+1]/(delta_r[i]*delta_r[i+1])
Indexerror: list index out of range
I’m a beginner in Python, what could possibly go wrong? This is the code:
D = []
u_med = []
delta_r = []
for i in range(2,n):
for j in range(2,n):
if i == j:
D[i][j+1] = - u_med[i+1]/(delta_r[i]*delta_r[i+1])
t1 = u_med[i+1]/(delta_r[i]*delta_r[i])
t2 = u_med[i]/(delta_r[i]*delta_r[i])
D[i][j]= t1 + t2 + V[i]
D[i+1][j]= - u_med[i+1]/(delta_r[i]*delta_r[i+1])
t1 = u_med[2]/(delta_r[1]*delta_r[1])
t2 = 0
D[1][1]= t1 + t2 + V[1]
D[1][2]= - u_med[2]/(delta_r[1]*delta_r[2])
D[2,1]= - u_med[2]/(delta_r[2]*delta_r[1])
t1 = 0
t2 = u_med[n]/(delta_r[n]*delta_r[n])
D[n][n]= t1 + t2 + V[n]
Edit: I’m working with the one-dimensional Schrödinger equation on a variable grid... Anyway, I’m trying to write it in matrix form, with a symmetric N x N matrix.
Same code in Fortran:
do i=2,n-1
do j=2,n-1
if (i.eq.j) then
D(i,j+1)=-u_med(i+1)/(delta_r(i)*delta_r(i+1))
t1 =u_med(i+1)/(delta_r(i)*delta_r(i))
t2 = u_med(i)/(delta_r(i)*delta_r(i))
D(i,j)= t1 + t2 + V(i)
D(i+1,j)=-u_med(i+1)/(delta_r(i)*delta_r(i+1))
end if
end do
end do
t1 =u_med(2)/(delta_r(1)*delta_r(1))
t2 = 0.d0
D(1,1)= t1 + t2 + V(1)
D(1,2)= -u_med(2)/(delta_r(1)*delta_r(2))
D(2,1)= -u_med(2)/(delta_r(2)*delta_r(1))
t1 =0.d0
t2 = u_med(n)/(delta_r(n)*delta_r(n))
D(n,n)= t1 + t2 + V(n)
All your lists are empty, so no index you have used will exist. What exactly do you need to do? Can describe with words, editing the question?
– Woss
Hi, thanks for the answer, but I don’t understand.. How come all the lists are empty? In Python I first need to create an empty vector, then add the information in the loop?
– oklucas