Python中利用quivalfunction实现磁场可视化

2024-10-05 12:54:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用振颤函数来形象化导线的磁场。在

#Calculation of a magnetic field of a wire

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from pylab import *

I = 100000000000
constant = 1e-7/(4*np.pi)

# wire elements; always lenght one
coord = [(10,10,0), (11,10,0), (12,10,0), (13,10,0), (14,10,0), (15,10,0), (16,10,0), (17,10,0), (18,10,0),
         (19,10,0), (19,11,0), (19,12,0), (19,13,0)]

xwidth = 3
ywidth = 3
z = 1
b = np.zeros((xwidth,ywidth))

# calculate the b-field
def bfield(x,y,z,c):
    for x in range(xwidth):
        for y in range(ywidth):
            # number of wire elements
            for i in range(1,12):
                rx = x-(coord[i][0]+coord[i+1][0])/2.
                ry = y-(coord[i][1]+coord[i+1][1])/2.
                rz = z * 1.0 # = z-0
                r = (rx**2+ry**2+rz**2)**0.5 # distance r between field and middle of the wire
                dl = np.array([(coord[i+1][0]-coord[i][0]), (coord[i+1][1]-coord[i][1]), 0])
                bb = np.cross(dl, np.array([rx,ry,rz]))
                e = constant*I*bb/r**3
                print e
                #print e[0], e[1]
                b[x,y] += e[c]  # EDIT
    return b

X,Y = meshgrid(arange(0,xwidth,1),arange(0,ywidth,1))
U = bfield(X,Y,z,0)
V = bfield(X,Y,z,1)
quiver(X,Y,U,V)
xlim(0,xwidth)
ylim(0,ywidth)
show()

编辑2:如何在绘图中绘制坐标线? 编辑三:我想用箭袋,但不管用。在


Tags: ofinimportfieldfornprangerx
2条回答

看起来quiver现在只支持二维打印,但是可以通过将多个二维图层打印到一个三维打印中使其成为三维打印。你可以跟随我的example来看看如何做这些层。在

ValueError: too many values to unpack

这意味着你在右手边的值比左手边的变量多

相关问题 更多 >

    热门问题