我正在为我的物理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集成开发环境)时,它给出一条错误消息,提示“错误:属性‘轴’必须是一个向量”,我确信这意味着在这段代码中赋值给Enet.axis的值有问题。我已经检查了我生成的代码,但似乎找不到错误。
我们正在学习python,以补充我们的常规课程工作,所以除了这些作业外,我没有任何python背景。我不需要帮助找到电场,只需要错误消息弹出的原因。任何正确方向的帮助或提示都将不胜感激!
谢谢
发布于 2018-11-16 01:50:25
在第31行,您使用^而不是**进行求幂。
如果你使用Chrome浏览器,你可以得到错误的行号。
我通过在代码中的关键位置插入打印语句发现了这个问题,这表明rPlusMag是0,而不是一个向量。
https://stackoverflow.com/questions/52690129
复制相似问题