【发布时间】:2018-10-07 15:38:12
【问题描述】:
我正在为我的物理 2 课做 Vpython 作业,要求对偶极子的电场进行编程。我写了以下代码:
## constants
oofpez = 9e9 # stands for One Over Four Pi Epsilon-Zero
qe = 1.6e-19 # postive charge value
s = 4e-11 # charge separation
R = 3e-10 # display Enet on a circle of radius R
scalefactor = 3e-20 # for scaling arrows to represent electric field
## objects
## Represent the two charges of the dipole by red and blue spheres:
plus = sphere(pos=vector(s/2,0,0), radius=1e-11, color=color.red)
qplus = qe # charge of positive particle
neg = sphere(pos=vector(-s/2,0,0), radius=1e-11, color=color.blue)
qneg = -qplus # charge of negative particle
## calculations
## You will complete the lines required to make a loop calculate and display the net dipole electric field
## at equally spaced angles on a circle radius R around the dipole. The dipole is centered at the origin.
theta = 0
while theta < 2*pi:
rate(2) # tell computer to go through loop slowly
## Calculate observation location (tail of arrow) using current value of theta:
Earrow = arrow(pos=R*vector(cos(theta),sin(theta),0), axis=vector(1e-10,0,0), color=color.orange)
## assign the name TestLocation to be the observation location on the circle radius R
TestLocation=R*vector(cos(theta),sin(theta),0)
## write instructions below to tell the computer how to calculate the correct
## net electric field Enet at the observation location (the position of Earrow):
rPlus=TestLocation-plus.pos
rPlusMag=((R*cos(theta)-(s/2))^2+(R*sin(theta))^2)^0.5
rPlusHat=rPlus/rPlusMag
Eplus=oofpez*qplus/(rPlusMag)^2*rPlusHat
rNeg=TestLocation-neg.pos
rNegMag=((R*cos(theta)-(-s/2))^2+(R*sin(theta))^2)^0.5
rNegHat=rNeg/rNegMag
Eneg=oofpez*qneg/(rNegMag)^2*rNegHat
Etotal=Eplus+Eneg
Enet=arrow(pos=TestLocation,axis=Etotal*scalefactor, color=color.green)
## change the axis of Earrow to point in the direction of the electric field at that location
## and scale it so it looks reasonable
## Efield = arrow(pos=R*vector(cos(theta),sin(theta),0), axis=Etotal*scalefactor, color=color.blue)
Earrow.axis=Etotal*scalefactor
## Assign a new value to theta
theta = theta + pi/6
分配有一个使用 cmets 创建的预制模板,声明了正确的变量,并且所述变量分配了正确的值,因此理论上如果我正确输入其余代码,它应该可以正确运行。我编写的代码从“rPlus=...”开始,以“Enet=...”结束。但是,当我运行它(使用 GlowScript IDE)时,它会给出一条错误消息,指出“错误:属性‘轴’必须是矢量”,我确定这意味着在代码的该部分中分配给 Enet.axis 的值有问题。我查看了我生成的代码,似乎找不到错误。
我们正在学习 Python 作为常规课程的补充,所以除了这些作业之外,我没有任何 Python 方面的背景。我不需要帮助找到电场,只是为什么会弹出错误消息。任何正确方向的帮助或提示将不胜感激!
谢谢
【问题讨论】:
-
你能发布你的完整错误吗?确切地知道错误的来源。我认为错误可能在
Earrow.axis=Etotal*scalefactor线上。 VPython 有vectorobject 需要使用而不是数组。 -
完整的错误只说“错误:属性'轴'必须是一个向量。”不幸的是,它没有说明它在哪一行,但我会回去查看那行代码以仔细检查。