I wrote a program in Fortran and now I'm trying to write it in Python. However, it is giving an error:
(i + 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 can go wrong? Here 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 writing it in matrix form, with a symmetric tridiagonal 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)