用序列设置数组元素

2024-09-29 01:36:48 发布

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

运行以下代码:Python模拟来计算棒的电位差

设置语句:

from __future__ import division
from visual import *
from visual.graph import *
scene = display(x=0, y=0, width=600, height = 600)
graph = gdisplay(x=600, y=0, width=400, height=300)

代码主体:

#Objects
Vgraph=gcurve(color=color.cyan)
marker = sphere(pos=vector(0,.15,0), radius=.05, color=color.red)
marker.v = vector(0,.5,0)

#Constants
e = 1.6e-19
oofpez = 9e9
scalefactor = 8e-3

dt = 0.001

#Initial values
L = 2  ##length of rod
N = 100 ##Number of point charges
Q = 3e-8
dQ = Q/N
dx = L/N
t = 0
x = (dx-L)/2
dVtotal = 0
Enet = vector(0,0,0)

##Calculations
while x < L/2:
    sphere(pos=(x,0,0), radius =1/6, color=color.cyan)
    x += dx
while marker.pos.y < 1.15:
    x=(dx-L)/2
    Enet=vector(0,0,0)
    while x < L/2:
        r=marker.pos - vector(x,0,0)
        rmag=mag(r)
        rhat=r/rmag
        E=oofpez*dQ/rmag**2*rhat
        Enet+=E
        x+=dx
    dV=-dot(Enet,.0005)
    dVtotal=dVtotal+dV
    t=t+dt
    marker.pos=marker.pos+marker.v*dt
    Vgraph.plot(pos=(t,dVtotal))
print ("dVtotal =", dVtotal, "volts")

产生此错误:

Traceback (most recent call last):
  File "F:/Purdue/2nd semester/PHYS 272/Labs/lab5.py", line 49, in <module>
    Vgraph.plot(pos=(t,dVtotal))
  File "C:\Python32\lib\site-packages\vis\graph.py", line 742, in plot
    pos,c = primitiveargs(self,args)
  File "C:\Python32\lib\site-packages\vis\graph.py", line 708, in primitiveargs
    pos = array(arguments['pos'], float)
ValueError: setting an array element with a sequence.

我该怎么修?你知道吗


Tags: fromposimportplotdtmarkergraphcolor