I have a matrix with shape (x,y,z) and in each matrix element I store a 3D array with the components of the magnetic field in that point. But when I try to plot the points at y=0 the quiver representation is weird.
For example bx at z=7 (in the middle): print(bx[:,7])
gives this output [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
But the quiver plot at z=0 gives non zero x values as you can see
The code:
import numpy as np
from scipy import constants as cst
import matplotlib.pyplot as plt
#UDS SI
prefactor = cst.mu_0/4/np.pi
N=10#discretizacion de espira
I=-1#corriente
R=0.5#rad esp
def biotsav(i,dl,r):
return prefactor*i*np.cross(dl,r)/np.linalg.norm(r)**3
#espira en x,y discretizada
#por defecto en centro
def createSpiral(r,n_intervals,center = [0,0,0]):
theta = np.linspace(0,2*np.pi,n_intervals)
return np.array([center[0]+r*np.cos(theta),
center[1]+r*np.sin(theta),center[2]+np.zeros(n_intervals)]).T
def calculateDirVect(dl,dlant):
u=[]
for i in range(3):
u.append(dl[i]-dlant[i])
return u
def calculateRefPoint(dl,dlant):
ref = []
for i in range(3):
ref.append((dl[i]+dlant[i])/2)
return ref
def calculateDispVect(dl,p):
r=[]
for i in range((3)):
r.append(p[i]-dl[i])
return r
spiral= createSpiral(R,N,center=[1.5,1.5,1.5])
#Fijamos el punto z para el plano xy
totalB=0
zs=np.linspace(0,3,15)
xs=np.linspace(0,3,15)
ys=np.linspace(0,3,15)
b = np.empty((len(xs),len(ys),len(zs)),dtype=object)
for i in range(len(xs)):
for j in range(len(ys)):
for k in range(len(zs)):
totalB=np.array([0.,0.,0.])
dlant = spiral[-1]
p=[xs[i],ys[j],zs[k]]
for dl in spiral:
u = calculateDirVect(dl,dlant)
ref = calculateRefPoint(dl,dlant)
r = calculateDispVect(dl,p)
dlant=dl
totalB+=(biotsav(I,u,r))
b[i,j,k] = totalB
bx=np.zeros((len(xs),len(zs)),dtype='float')
bz=np.zeros((len(xs),len(zs)),dtype='float')
for i in range(len(xs)):
for k in range(len(zs)):
bx[i,k]=(b[i,0,k][0])
bz[i,k]=(b[i,0,k][2])
# Trazar los vectores del campo magnético con quiver
X, Z = np.meshgrid(xs, zs)
print(bx[:,7])
plt.quiver(X, Z, bx, bz)
plt.xlabel('x')
plt.ylabel('z')
plt.title('Campo Magnético')
plt.show()
Where am I wrong, as far as I know the 3rd and 4th attribute in quiver are the x and y direction of the arrows