I'm writing a script to plot a 3D representation of some thermodynamic property, Z = f(pr, Tr). pr and Tr are created with [numpy.]arange() and then they are mapped with [numpy.]meshgrid() the following way:
Tr = arange(1.0, 2.6, 0.10)
pr = arange(0.5, 9.0, 0.25)
Xpr, YTr = meshgrid(pr, Tr)
Xpr and YTr are then passed to the function that calculates the aforementioned property:
Z = function(Xpr, YTr)
("function" is just a generic name that is later replaced by the actual function name).
The values stored in Z are finally plotted:
fig = plt.figure(1, figsize=(7, 6))
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(Xpr, YTr, Z, cmap=cm.jet, linewidth=0, antialiased=False)
Everything works fine when "function" is something quite straightforward like:
def zshell(pr_, Tr_):
A = -0.101 - 0.36*Tr_ + 1.3868*sqrt(Tr_ - 0.919)
B = 0.021 + 0.04275/(Tr_ - 0.65)
E = 0.6222 - 0.224*Tr_
F = 0.0657/(Tr_ - 0.85) - 0.037
G = 0.32*exp(-19.53*(Tr_ - 1.0))
D = 0.122*exp(-11.3*(Tr_ - 1.0))
C = pr_*(E + F*pr_ + G*pr_**4)
z = A + B*pr_ + (1.0 - A)*exp(-C) - D*(pr_/10.0)**4
return z
But it fails when the function is something like this:
def zvdw(pr_, Tr_):
A = 0.421875*pr_/Tr_**2 # 0.421875 = 27.0/64.0
B = 0.125*pr_/Tr_ # 0.125 = 1.0/8.0
z = 9.5e-01
erro = 1.0
while erro >= 1.0e-06:
c2 = -(B + 1.0)
c1 = A
c0 = -A*B
f = z**3 + c2*z**2 + c1*z + c0
df = 3.0e0*z**2 + 2.0e0*c2*z + c1
zf = z - f/df
erro = abs((zf - z)/z)
z = zf
return z
I strongly suspect that the failure is caused by the iterative method inside function zvdw(pr_, Tr_) (zvdw has been previously tested and works perfectly well when float arguments are passed to it). That is the error message I get:
Traceback (most recent call last):
File "/home/fausto/.../TestMesh.py", line 81, in <module>
Z = zvdw(Xpr, YTr)
File "/home/fausto/.../TestMesh.py", line 63, in zvdw
while erro >= 1.0e-06:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Yet the error message doesn't seem (to me) to be directly related to the while statement.
Any ideas?

zfandzare 2 dimensional arrays inside while loop. aftererro = abs((zf - z)/z)errois also 2 dimensional. after one looperro >= 1.0e-06will be a 2 dimensional array with truth values, possibly bothTrueandFalse. Thats were the error message comes from. What should be the condition to end the while loop? Every value equalsFalse, most of them or is one enough?Falseshould be enough, but maybe I'm being too hasty in my judgement. What should I do, supposing I'm right?while all(erro >= 1.0e-06):whereallshould be thenumpy.allfunction, not the python function. If you want to work withnumpyyou should look intonp.allandnp.any. To avoid confusion you should use the common way to importnumpywithimport numpy as np.while (erro >= 1.0e-06).all():also works. But first I had to redefine the initial value oferrofromerro = 1.0toerro = array(1.0). Thanks a lot for your help!