TypeError - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: TypeError (/thread-23836.html) |
TypeError - Robb - Jan-19-2020 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( ) RE: TypeError - Gribouillis - Jan-19-2020 Replace line 20 with fvector = np.array([0] * (2))Explanation: Robb Wrote:after literally hours looking at it, I still have no clue how to fix it.The zen of python says import this Wrote:In the face of ambiguity, refuse the temptation to guess. The error traceback tells you many things: It tells you that the error is at line 6 in rk4Algor(), which is called at line 30. Also the error involves dividing a list by a float. There are 2 division operators at line 6 but by inserting for example k1 / 2 before that line, it turns out that the faulty operation is k1 / 2 because k1 is a list. Now where does k1 come from? It was returned by f() at line 5. What does f return? It returns fVector which is defined at line 20, as a list!! Replace this list by an array and it works.Moral of the story: don't stare at the code for hours looking for a bug. Get the information from the python interpreter. RE: TypeError - Robb - Jan-20-2020 Hey, thanks a lot for the help. Next time I ll keep your words in mind and try ll to take an more organized approach on the problem:D |