【问题标题】:Slinky in VPythonVPython 中的 Slinky
【发布时间】:2015-10-08 01:43:33
【问题描述】:

下面的 VPython 代码的目标是使用 14 个球对 slinky 进行建模,以表示 slinky 的分解组件。但是我的代码面临一些问题。例如,

R[i] = ball[i].pos - ball[i+1].pos

加注

'int' 对象没有属性 'pos'

上面的错误是什么意思?

这是我的程序:

from __future__ import print_function, division
from visual import *

ceiling = box(pos=(0,0,0), size=(0.15, 0.015, 0.15), color=color.green)

n = 14 #number of "coils" (ball objects)
m = 0.015 #mass of each ball; the mass of the slinky is therefore m*n

L = 0.1  #length of the entire slinky
d = L/n #a starting distance that is equal between each

k = 200 #spring constant of the bonds between the coils


t = 0
deltat = 0.002
g = -9.8
a = vector(0,g,0)


ball=[28]
F=[]
R=[]

#make 28 balls, each a distance "d" away from each other

for i in range(n):
    ball = ball+[sphere(pos=vector(0,-i*d,0), radius = 0.005, color=color.white)]
    #ball[i].p = vector(0,0,0)
for i in range(n-1):
    F.append(vector(0,0,0))
    R.append(vector(0,0,0))


#calculate the force on each ball and update its position as it falls

while t < 5:
    rate(200)
    for i in range (n-1):
        R[i]=ball[i].pos-ball[i+1].pos
        F[i]=200*(mag(R[i]) - d)*norm(R[i])
        #r[i] = ball[i].pos - ball[i+1].pos
        #F[i] = 200*((r[i].mag - d)*((r[i])/(r[i].mag)))
    for i in range(n-1):
        ball[i+1].p = ball[i+1].p + (F[i] - F[i+1] + (0.015*a))*deltat
        ball[i+1].pos = ball[i+1].pos + ((ball[i+1].p)/0.015)*deltat
        t = deltat + t

【问题讨论】:

    标签: python spring vpython


    【解决方案1】:

    您似乎已将 int 值分配给 ball[i] 对象。考虑到这一点,错误消息非常清楚:整数对象没有属性.pos(属于sphere 类。

    如果您希望 ball 是球体对象的列表,您需要做一些不同的事情。目前你已经初始化了ball = [28],它是一个list,只有一个元素,整数值为28。

    所以,i = 0,ball[i] 返回28,显然没有球体的任何此类属性。

    这可能会做到:

    ball=[]  ### initialize ball as an empty list
    F=[]
    R=[]
    
    #make 28 balls, each a distance "d" away from each other
    """
        You said you want only 14 balls, but the comment above says 28...
    """
    
    for i in range(n):
        ball.append(sphere(pos=vector(0,-i*d,0), radius = 0.005, color=color.white))
    

    【讨论】:

      猜你喜欢
      • 2015-10-02
      • 1970-01-01
      • 2019-12-25
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多