Numerical differentiation of electric potential with python

I used the following code to get the quadrupole potential electric field.

for n in range (nx-1):
  for m in range (ny-1):
   for k in range (nz-1):
     Ex[n,m,k] =-( (T[n+1,m,k]-T[n,m,k]) )/((x[n+1]-x[n]));
     Ey[n,m,k] =-( (T[n,m+1,k]-T[n,m,k]) )/((y[m+1]-y[m]));
     Ez[n,m,k] =-( (T[n,m,k+1]-T[n,m,k]) )/((z[k+1]-z[k]));
     return Ex,Ey,Ez, T

quad_potential and electric field here T is the potential in 3D obtained by solving the Laplace equation numerically, as can be seen from the figure, the red electrode (positive) has the wrong direction of the electric field vector (right upper side and right lower side of the upper electrode), the same thing happens with other electrodes . that is, the negative electrode has an output vector of the electric field, which should be in the opposite direction.

I also used the central difference method, but I get the same figure. Could you tell me what is wrong with my differentiation?

+4
source share
1

, matplotlib , , , , (, ). , , - . , , , x y . , ( ) . , , x y . ( , ) , ( ) .

, 2D- , , ( quiver contour). meshgrid my x y, meshgrid . , , while .

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-3,3,0.2)
y = np.arange(-5,5,0.2)
z = np.arange(-5,5,0.2)

def grad(f):
    gx = np.zeros_like(f)
    gy = np.zeros_like(f)
    gz = np.zeros_like(f)
    for n in range(1,len(x)-1):
        for m in range(1,len(y)-1):
            for k in range(1,len(z)-1):
                gx[n,m,k] = (T[n+1,m,k]-T[n-1,m,k])/(x[n+1]-x[n-1]);
                gy[n,m,k] = (T[n,m+1,k]-T[n,m-1,k])/(y[m+1]-y[m-1]);
                gz[n,m,k] = (T[n,m,k+1]-T[n,m,k-1])/(z[k+1]-z[k-1]);
    return gx, gy, gz

T = np.zeros((len(x), len(y), len(z)))
for n in range(len(x)):
    for m in range(len(y)):
        for k in range(len(z)):
            T[n,m,k] = np.sin((x[n] - y[m])/3.0) + 0.3*np.cos(y[m]) + z[k]**2

gx,gy,gz = grad(T)

Y, X= np.meshgrid(y,x)

plt.figure('WRONG with x and y')
plt.contour(y, x, T[:,:,round(len(z)/2)], 64)
plt.colorbar()
plt.quiver(y, x, 10*gx[:,:,round(len(z)/2)], 10*gy[:,:,round(len(z)/2)])
plt.xlabel("x")
plt.ylabel("y")
plt.axes().set_aspect('equal')


plt.figure('with X and Y')
plt.contour(X, Y, T[:,:,round(len(z)/2)], 64)
plt.colorbar()
plt.quiver(X, Y, 10*gx[:,:,round(len(z)/2)], 10*gy[:,:,round(len(z)/2)])
plt.xlabel("X")
plt.ylabel("Y")
plt.axes().set_aspect('equal')

plt.figure('with Y and X')
plt.contour(Y, X, T[:,:,round(len(z)/2)], 64)
plt.colorbar()
plt.quiver(Y, X, 10*gy[:,:,round(len(z)/2)], 10*gx[:,:,round(len(z)/2)])
plt.xlabel("Y")
plt.ylabel("X")
plt.axes().set_aspect('equal')

plt.show()

, , - nx != ny. , , , () .

+1
source

All Articles