问题
Recently, by the kind response of a stackoverflow member, I learnt how to perform operations on meshgrids with functions that weren't contiguous with with numpy using a for-loop trick to iterate over each of the elements in the grid. I found that it worked on most occasions however, below, for some reason it seems to be failing:
Emag is a long function that I created.
import numpy as np
X, Y = np.mgrid[0.1:0.15:3j, 0.1:0.15:3j]
Z = np.zeros_like(Y)
po = np.sqrt(X**2 + Y**2)
phio = np.arctan2(Y,X)
print po
print phio
for i in range(po.shape[0]):
for j in range(po.shape[1]):
print po[i,j]
print phio[i,j]
Z[i,j] = Emag(po[i,j], phio[i,j], 0)
print Z[i,j]
Below is the response
%run C:/Users/Nigel/Desktop/Nigel07112014.py
[[ 0.14142136 0.16007811 0.18027756] <--- po
[ 0.16007811 0.1767767 0.19525624]
[ 0.18027756 0.19525624 0.21213203]]
[[ 0.78539816 0.89605538 0.98279372] <--- phio
[ 0.67474094 0.78539816 0.87605805]
[ 0.5880026 0.69473828 0.78539816]]
0.141421356237 <--- po[0,0]
0.785398163397 <--- phio[0,0]
732.46186213 <--- Z[0,0]
0.160078105936 <--- po[0,1]
0.896055384571 <--- phio[0,1]
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
It seems that for the second element of the meshgrid, the computer has trouble producing an output for Z. However, when I execute Emag for the second elements of po and phio separately - it works just fine (see below):
Emag(0.160078105936, 0.896055384571, 0)
> 589.541876301
which should be the second element of the Z meshgrid.
So where's it going wrong? I'll paste the much longer error message if requested
Full error message below:
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
C:\Users\Nigel\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195 else:
196 filename = fname
--> 197 exec compile(scripttext, filename, 'exec') in glob, loc
198 else:
199 def execfile(fname, *where):
C:\Users\Nigel\Desktop\Nigel07112014.py in <module>()
220 print po[i,j]
221 print phio[i,j]
--> 222 Z[i,j] = Emag(po[i,j], phio[i,j], 0)
223 print Z[i,j]
224
C:\Users\Nigel\Desktop\Nigel07112014.py in Emag(p, phi, z)
149 def Emag(p, phi, z):
150 if z >=0:
--> 151 VE1p = (p * (z-z0) * ((MuZ)/(4 * pi * Eps0 * Eps1)) * exp(j * k1 * R0 / (R0**3)) * ((3/(R0**2))- (3 * j * k1 / R0)-(k1**2)) ) - ( j * ((MuZ)/(4 * pi * Eps0 * Eps1)) * INTEGRATEZI( lambda kp: J1(kp * p) * A1(kp) * kp * k1z(kp) * (exp(1j * k1z(kp) * (z + z0))) ) )
152
153 VE1phi = 0
OverflowError: math range error
回答1:
Change exp( j ... in your Emag to exp( 1j ... if you're dealing with exponentials of imaginary argument. When it's executed inside the loop j can receive bad values from j the index of the loop.
Should I be right with respect to imaginary arguments, I would also triple check the results of your Emag function.
来源:https://stackoverflow.com/questions/27091037/numpy-iterating-calculations-in-a-meshgrid