我正在为我的物理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
该分配具有一个预制的模板,该模板使用注释创建,声明了适当的变量,并且所述变量分配了正确的值,因此,从理论上讲,如果我正确输入其余代码,它应该可以正确运行。我编写的代码以“ rPlus = ...”开头,以“ Enet = ...”结尾,但是,当我运行它(使用GlowScript IDE)时,它给出一条错误消息,提示“错误:属性'axis'必须是向量”,我确信这意味着在代码的该部分中分配给Enet.axis的值有问题。我浏览了我生成的代码,但似乎找不到错误。
我们正在学习python作为常规课程的补充,因此除这些作业外,我没有python的背景知识。我不需要寻找电场的帮助,而是为什么会弹出错误消息。在正确方向上的任何帮助或提示,将不胜感激!
谢谢
答案 0 :(得分:0)
在第31行,您使用^而不是**进行求幂。
如果您使用的是Chrome浏览器,则可以获得错误的行号。
我通过在代码的关键位置插入打印语句发现了问题,这发现rPlusMag为0,而不是向量。