【问题标题】:Slowing down a physics defying spring放慢物理学挑战的春天
【发布时间】:2014-05-17 15:08:12
【问题描述】:

我正在为我的物理课做一个教程。

我编写了一个程序,该程序应该在一个盒子上放一个推车,该推车由弹簧移动并且应该减速到停止,但是,当我运行它时,弹簧似乎正在加速推车(无论我做什么事情否定。)

我听说可能是 vPython 舍入数字导致加速的问题,如果这是真的,我可以将所有数字放大 1000 倍并解决它吗?

谢谢!

from visual import *
from visual.graph import *

length=1.0

track=box(pos=vector(0,-0.05,0),
          size=(length, 0.05, 0.10),
          material=materials.bricks,)

# creates the track which is "length"meters in the
# x direction, 0.05m tall, and 1m deep
start=-0.5*length+0.05

cart=box(pos=vector(start+0.01,0,0),
         size=(0.1,0.05,0.1),
         color=color.green)
k=-4
#spring constant

sprL=(start-0.05)-0.1

#sets position of left end of spring
spring=helix(pos=(sprL,0,0),
             axis=((cart.x-0.05)-sprL,0,0),
             radius=0.02,
             color=color.yellow)

cart.m=0.70
#mass of cart
cart.vel=vector(0,0,0)
#initial velocity of cart
cart.force = k*(cart.x)*vector(1,0,0) 
#force of the spring
cart.accel=cart.force/cart.m
#acceleration of the cart taking into account the fan
t=0
deltat=0.01
end=0.5*length-0.05
#defining the end of the track
gdisplay(x=100,
         y=500,
         xtitle='time (sec)',
         ytitle='X (cyan), Px (red)')

xcurve = gcurve(color=color.cyan)
pcurve= gcurve (color=color.red)

while cart.x<end+0.01 and (cart.x>(start-0.01)):
    #we include -0.01 so the cart does not fail immediately upon start...
    cart.pos = cart.pos + cart.vel*deltat+(0.5)*(cart.accel)*deltat**2
    #x equals x naught plus v times delta t plus one half a delta t squared
    #note that ** means "to the power of"
    xcurve.plot(pos=(t,cart.x))
    pcurve.plot(pos=(t,cart.vel.x))
    cart.vel=cart.vel+cart.accel*deltat
    #new velocity is old velocity plus acceleration times time
    cart.force=k*(cart.x)*vector(1,0,0)
    cart.accel=cart.force/cart.m
    spring.axis=((cart.x-0.05)-sprL,0,0)    
    t=t+deltat
    #increments time
    rate(100)
    #rate means no more than 100 loops per second

【问题讨论】:

  • 我不明白,你在代码的什么地方设置了耗散力?没有一个盒子永远不会失去能量。
  • 我将弹簧常数 (k) 设为负数,因此 cart.force 为负数,导致 cart.accel 为负数,导致 cart.pos 由于 cat.acceleration 减少(在 while 循环中)从中减去。使弹簧常数为正,导致框向左移动一小段时间,然后程序停止。
  • 您在寻找damped harmonic oscillator吗?

标签: python physics vpython


【解决方案1】:

您的系统中没有耗散力(会从中泄漏能量)。方程F = -kx 是能量守恒的(这个方程你有些迂回地编码,代表弹簧施加在物体上的力)。请注意,那里的等式并不意味着力总是负的,它只是指向cart.pos 的相反方向。这就是你获得正弦运动的方式。

物体真正减速需要耗散力。这方面的典型示例将由 F = -kx -bv 表示,对于某个常数 bv 是对象的速度。这代表你的春天被流体(空气/水/任何你喜欢的东西)减慢了速度。

在这种情况下,您的代码的最小更改将在您的循环内:

    cart.force=(k*(cart.x)-0.1*cart.vel.x)*vector(1,0,0)

这会产生一个欠阻尼系统,要尝试一个过阻尼系统,您可以将 0.1 设置为 10。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2010-11-10
    • 2022-06-14
    相关资源
    最近更新 更多