The aim of my program is to do the near field diffraction simulation. I feel like my code should work and expected to get a 2D intensity plot, but I keep receiving this error message:
And my code is like this
Thanks!
Error:---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-d5475b0ad90f> in <module>()
95 '''
96 print(r(x,x0,y,y0))
---> 97 n = n + g * 0.001 * np.exp( 1j * k * r(x,x0,y,y0) ) / r(x,x0,y,y0) * E0(x0,y0)
98 '''
99 n = n + list(map(lambda x,y: g * 0.001 * np.exp( 1j * k * r(x,x0,y,y0,z) ) / r(x,x0,y,y0,z) * E0(x0,y0),x,y)) # Problem comes from here. Seems like I cannot multiply by complex number
TypeError: can't multiply sequence by non-int of type 'complex'
I know I line 97 has lots of mistakes especially about this complex number(maybe I also cannot use iteration like this way) but actually whatever I modified it, there are still some issues.And my code is like this
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import numpy as np from scipy import integrate from scipy.integrate import quad, dblquad import scipy.special as special #Here are some parameters I will use. They can also be regarded as variables. N = 500 # number of samples to generate wl = 1000 # wave length (nm) of the laser. since lambda is a function in python, I don't know what is the best way to represent this parameter. r0 = 5 # r adius (cm) of the aperture Z0 = 100 # fixed distance (cm) a = 50 # boundary (cm) #Some important variables ''' E0 E I r x y z x0 y0 k ''' fig = plt.figure() ax = fig.gca(projection='3d') # make data x = np.linspace(-a,a,N) y = np.linspace(-a,a,N) X, Y = np.meshgrid(x, y) # Fresnel diffraction z = Z0 k = 2 ** np.pi / wl g = 1 / ( 1j * wl ) ''' def r(x,x0,y,y0,z): return np.sqrt( ( x - x0 ) ** 2 + ( y - y0 ) ** 2 + z ** 2 ) ''' def r(x,x0,y,y0): return list(map(lambda x,y: np.sqrt( ( x - x0 ) ** 2 + ( y - y0 ) ** 2 + z ** 2 ),x,y)) def E0(x0,y0): if (x0 ** 2 + y0 ** 2) < r0: return 1 else: return 0 # Aperture function ''' def bounds(): return [-np.inf,np.inf] '''''' def f(x,x0,y,y0): return list(map(lambda x,y: np.exp( 1j * k * r(x,x0,y,y0,z) ) / r(x,x0,y,y0,z) * E0(x0,y0),x,y)) def E(x,y): return g * dblquad(lambda x0, y0: f, -np.inf, np.inf, lambda x0: -np.inf, lambda x0: np.inf) Since there are 4 variables, I'm not sure whether I can do integration like this way. ''' ''' Here I try to use for loop to do the integration. Although the integral is defined on a bounded domain, it is more convenient to change the aperture function if we regard it as an improper integral ''' x0 = -50 y0 = -50 n = 0 for i in range(100000): x0 = x0 + 0.001 * i for j in range(100000): y0 = y0 + 0.001 * j ''' print(g) print(E0(x0,y0)) print(list(r(x,x0,y, y0,z))) ''' n = n + g * 0.001 * np.exp( 1j * k * r(x,x0,y,y0) ) / r(x,x0,y,y0) * E0(x0,y0) ''' n = n + list(map(lambda x,y: g * 0.001 * np.exp( 1j * k * r(x,x0,y,y0,z) ) / r(x,x0,y,y0,z) * E0(x0,y0),x,y)) # Problem comes from here. Seems like I cannot multiply by complex number ''' def I(x,y): _E = E(x,y) print(_E) return list(map(lambda x: x ** 2 / 2, _E)) # Intensity ''' Problem occurred here since E is a list and cannot use power function. I use map function instead. it seems to be fixed now. ''' Z = I(X,Y) # Plot the surface. surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False) # Customize the z axis. ax.set_zlim(-1.01, 1.01) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Set labels ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Intensity') # Add a color bar which maps values to colors. fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()Does anyone know what should I get rid of this error?
Thanks!