Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError
#1
Hey there, Im absolutly new to Python, and I just cant find the mistake in this code. I took it from another source to illustrate an quantum mechanical Problem(Schrödinger Wave Equation for Hydrogen) and solving it numerical using Runge Kutta, but thats not really the issue. Its not working due to some TypeError. It says "unsupported operand type(s) for /: 'list' and 'float'" but I just cant figure out, which Variable is the one causing trouble. If anyone could check the code, it would be highly appreciated, after literally hours looking at it, I still have no clue how to fix it. So please, if anyone would take a look at the code, id be very grateful.
Thanks
Robb
import numpy as np

def rk4Algor(t, h, N, y, f):
    k1=np.zeros(N); k2=np.zeros(N); k3=np.zeros(N); k4=np.zeros(N)
    k1 = h * f(t , y)
    k2 = h * f(t + h / 2. , y + k1 / 2.)
    k3 = h * f(t + h / 2. , y + k2 / 2.)
    k4 = h * f(t + h, y + k3)
    y = y + (k1 + 2 * (k2 + k3) + k4) / 6.
    return y


#Hdensity.py : Hydrogen Radial dens i ty c a l l i n g rk4Algor
import matplotlib.pylab as plt


n = 5; el = 2; dr = 1 # n = npr+e l+1
rVec = np.zeros ((2500) ,float) # array for p l o t
RhoVec = np.zeros ((2500) ,float) # Density array
fvector = [0] * (2)
y = [0]*(2) ; y[0] = (10**-8) ; y[1] = 0
def f(r ,y) : # RHS of ODE
    fvector[0] = y[1]
    fvector[1] = -(2/r-1)*y[1]-((n-1)/r-el*(el+1)/r**2)*y[0]
    return fvector
f(0.001 ,y) # f ( t= 0)
i = 0
for r in np.arange(0.001 ,25 ,dr) :
    rVec[i] = r
    y = rk4Algor(r , dr , 2 , y , f) # c a l l rk4 algor i thm
    RhoVec[i] = 4*3.141593*(y[0]*np.exp(-0.5*r))**2 *r**2
    i = i+1
plt.figure()
plt.plot(rVec ,RhoVec)
plt.title( "Hydrogen Radial Density n = 5")
plt.xlabel("Radius r")
plt.ylabel("Density")
plt.show( )
Reply


Messages In This Thread
TypeError - by Robb - Jan-19-2020, 09:54 PM
RE: TypeError - by Gribouillis - Jan-19-2020, 10:48 PM
RE: TypeError - by Robb - Jan-20-2020, 06:23 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020