gpt4 book ai didi

python - 为什么 3d 箭袋图的箭头指向错误的方向?

转载 作者:行者123 更新时间:2023-12-04 04:20:38 25 4
gpt4 key购买 nike

我一直致力于为研究建模磁场。下面的代码允许我计算任何给定点的字段的正确值(x,y,z);然而,当我通过代码传递一个 np.meshgrid 对象时,结果开始变得不稳定。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d


def normal_vector(u):
return u/np.linalg.norm(u)
class Path:
"""
This defines the Path class which allows for the calculations of the magnetic field.
"""

def __init__(self, xs, ys, zs):
self.points = zip(*[xs, ys, zs]) # defines the points
self.x = xs
self.y = ys
self.z = zs
self.path_vectors = [(self.points[i + 1][0] - self.points[i][0],
self.points[i + 1][1] - self.points[i][1],
self.points[i + 1][2] - self.points[i][2]) for i in range(len(self.x) - 1)]
def get_length(self):
"""
Calculates the path length
:return: returns float length
"""
return sum([np.sqrt(((self.x[i + 1] - self.x[i]) ** 2) + ((self.y[i + 1] - self.y[i]) ** 2) + (
(self.z[i + 1] - self.z[i]) ** 2)) for i in
range(len(self.x) - 1)])

def get_magnetlic_function(self,axes,current=1.0,magnetic_constant = 1.25663706212e-6):
magnetic_parameter = (current*magnetic_constant)/(4*np.pi)
field_function = lambda x,y,z: sum([magnetic_parameter*np.cross(self.path_vectors[j],normal_vector(np.stack([x-self.x[j],y-self.y[j],z-self.z[j]],axis=-1)))/(np.linalg.norm(np.stack([x-self.x[j],y-self.y[j],z-self.z[j]],axis=-1))**2) for j in range(len(self.x)-1)]).swapaxes(0,-1)
return field_function

n = 200
r = 1
h = 5
grid_x,grid_y,grid_z = np.meshgrid(np.linspace(-10,10,5),
np.linspace(-10,10,5),
np.linspace(-10,10,5))
c = h / (2 * n * np.pi)
t = np.linspace(0,2*np.pi, 5000)
xp = 3*np.cos(t)
yp = 3*np.sin(t)
zp = 0*t
p = Path(list(xp), list(yp), list(zp))
func = p.get_magnetlic_function([grid_x,grid_y,grid_z])
u,v,w = func(grid_x,grid_y,grid_z)
r = np.sqrt(u**2+v**2+w**2)
print func(-10.0,00.0,0.0)
ax1 = plt.subplot(111,projection='3d')
ax1.plot(xp,yp,zp,'r-')
ax1.plot([-10],[0],[0],'ro')
ax1.quiver(grid_x,grid_y,grid_z,u/r,v/r,w/r,length=1)
plt.show()

在底部附近很明显,如果运行代码,-10.0,00.0,0.0 处的矢量方向与打印的值不同。为什么?从代码中,我在这里收到箭袋图: My code.

它应该是这样的: enter image description here

最佳答案

当试图找到由电流分布引起的磁场时,我发现考虑成对交互通常要清楚得多(尽管 lambda 函数更像 pythonic)。考虑这种方法

class Path:
# ...
def mag_func(self, x, y, z, current = 1.0, mag_const = 1.25663706212e-6):
mag_param = current * mag_const / (4 * np.pi)
s = x.shape
res = np.zeros((s[0],s[1],s[2],3))
for i in range(s[0]):
for j in range(s[1]):
for k in range(s[2]):
for idx, (xc, yc, zc) in enumerate(zip(self.x, self.y, self.z)):
res[i,j,k,:] += mag_param * \
np.cross(self.path_vectors[idx], [x[i,j,k] - xc,
y[i,j,k] - yc, z[i,j,k] - zc]) / \
np.linalg.norm([x[i,j,k] - xc, y[i,j,k] - yc,
z[i,j,k] - zc])**2
return res[:,:,:,0], res[:,:,:,1], res[:,:,:,2]
#...
u, v, w = p.mag_func(grid_x, grid_y, grid_z)
r = np.sqrt(u**2+v**2+w**2)
ax1 = plt.subplot(111,projection='3d')
ax1.plot(xp, yp, zp, 'r-')
ax1.quiver(grid_x, grid_y, grid_z, u/r, v/r, w/r,length=1)
plt.show()

哪个会给出

enter image description here

这是载流导线周围磁场的正确表示。

至于为什么 lambda 一开始不起作用的问题,我认为是由于通过 np.meshgrid 创建网格造成的,例如外部 sum 对比应有的更多点求和。以上述方式迭代解决该问题。可以使用 lambda 函数,但我认为您仍然需要遍历 grid_xgrid_y grid_z 以所示方式。

关于python - 为什么 3d 箭袋图的箭头指向错误的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com