Aug-20-2018, 01:09 PM
This is my first time in this forum sorry if I posted in the wrong section. I just started using python 2 days ago and Im stuck on doing the cross product of a vector field I created, the code works fine when I remove the last line that does the cross product at the end. I keep getting the error:
Output:TypeError: only length-1 arrays can be converted to Python scalars
import scipy from scipy import constants import math import numpy as np from numpy.polynomial import Laguerre from scipy import special from scipy import misc import sympy from sympy import * import matplotlib.pyplot as plt from matplotlib.colors import LightSource from matplotlib.cbook import get_sample_data # set up co-ordinate grid x = np.linspace(-10, 10, 100) y = np.linspace(-10, 10, 100) z = np.linspace(-10, 10, 100) X, Y, Z = np.meshgrid(x, y, z) # wave number lamda = 500*10**(-9) PI = scipy.pi k = 2*PI/lamda # add pi alter # laguerre l = 0 p = 6 C = np.sqrt(2*math.factorial(p)/(PI*math.factorial(p + l))) # Laguerre constan def f(x, y, z): r = np.sqrt(x**2 + y**2) ZR = 1 # find value w = np.sqrt((2/k)*(ZR**2 + z**2)/(ZR)) L = scipy.special.assoc_laguerre(2*(r**2)/w**2, n=p, k=l) return C*(1/w)*((r**(abs(l)))*np.sqrt(2)/w)*np.exp((-r**2)/w**2)*L # calculus stuff dx = 1*10**(-9) dy = 1*10**(-9) dz = 1*10**(-9) u = f(X, Y, Z) udy = (f(X,Y+dy,Z)-f(X,Y,Z)) / dy # y partial derivative udx = (f(X+dx,Y,Z)-f(X,Y,Z)) / dx # x partial derivative # fields # E,B Ex = 1j*k*u*np.exp(1j*k*Z) Ey = 0 Ez = -udx*np.exp(1j*k*Z) E = [Ex,Ey,Ez] Bx = 0 By = 1j*k*u*np.exp(1j*k*Z) Bz = -udy*np.exp(1j*k*Z) B =[Bx,By,Bz] #real poynting np.cross(E,B)Thank you in advance for the help, I can not figure out what to do.