如何在可视化python中停止n个耦合质量的动画中的框重叠



这个问题可能更像是一个物理问题,而不是vpython问题,因为我不知道是代码还是物理问题出了问题。我正在尝试使用python的可视化模块编写一个关于n耦合弹簧-质量系统动画的程序。我看不出哪里出了问题,但质量(即使质量的数量只有两个(经常重叠,我怎么能改变边界条件或其他东西,使它们不重叠(就像在现实中一样(?代码为:

from visual import *
from math import cos
m=[1,2]#list of masses
k=[1,2,1]#list of spring constants
R=len(m)#number of masses
floor=box(pos=(0,-0.5,0),size=(10,0.01,10),color=color.blue,material=materials.wood)#resting table
boxlist=[]#for creation of the masses
for z in range(0,R):
boxlist.append(box(pos=(4*z,0,0),size=(0.5,0.5,0.5),color=color.red,material=materials.wood))
#a is the matrix whose eigenvalues are the square of angular frequencies,and eigenvectors are the amplitudes
a=[[0 for r in range(R)]for c in range(R)]
for i in range(0,R):
if i>0:
a[i][i-1]=-k[i]/float(m[i])
if i<R-1:
a[i][i+1]=-k[i+1]/float(m[i])
a[i][i]=k[i]/float(m[i])+k[i+1]/float(m[i])
val=eig(a)[0]
vec=eig(a)[1]
t=0
dt=0.01
s=0
S=[]
while True:
rate(100)
for i in range(0,R):
for j in range(0,R):
s+=vec[j][i]*cos(((val[j])**0.5)*t)#x=Acos(wt),allowing phase angle to be zero
S.append(s)
s=0
for z in range(0,R):
boxlist[z].pos.x=S[z]
S=[]
t+=dt

您提供了一个不会运行的程序,因为"eig"是未定义的。这样就不可能探究你的问题。

以下是一个具有许多弹簧和质量的程序示例:https://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/program/AtomicSolid-VPython

单击"查看此程序"查看代码。

我注意到您使用的是非常旧版本的VPython。有关如何使用当前版本的信息,请参阅vpython.org。

最新更新