第14堂課
兩自由落體球間的彈性碰撞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # -*- coding: utf8 -*-
# 匯入視覺化套件
from vpython import *
# 1. 參數設定
# 重力加速度
g = -9.8
# 時間間隔
dt = 0.0001
# 經過時間
t=0
# 2. 畫面設定
# 畫布
scene = canvas(width=600, height=600, background=vector(0.5,0.6,0.5), center=vector(0,2,0))
# 地板
floor = box(pos=vector(0,-0.2,0), length=5, height=0.1, width=5, texture=textures.wood)
# 兩球高度時間圖
gd = graph(title='y vs t', xtitle='t', ytitle='y', ymax=13, xmax=10, background=vector(0.3,0.3,0.3))
yt1 = gcurve(graph=gd, color=color.cyan)
yt2 = gcurve(graph=gd, color=color.red)
# 3. 球的設定
ball1 = sphere(radius=0.05, color=color.white, v=0, m=0.2)
ball2 = sphere(radius=0.1, color=color.white, v=0, m=0.6)
ball2.pos = vector(0,2,0)
ball1.pos=vector(0,2+ball1.radius+ball2.radius+0.05,0)
# 4. 函數定義
def collide(v1,v2,m1,m2):
v1f = v1*(m1-m2)/(m1+m2) + v2*2*m2/(m1+m2)
v2f = v1*2*m1/(m2+m1) + v2*(m2-m1)/(m2+m1)
return v1f, v2f
# 5. 運動部分
while t<=10:
rate(2000)
t = t + dt
ball1.v = ball1.v + g * dt
ball1.pos.y = ball1.pos.y + ball1.v * dt
if ball1.pos.y <= 0:
ball1.v = -ball1.v
yt1.plot( pos = (t, ball1.pos.y))
ball2.v = ball2.v + g * dt
ball2.pos.y = ball2.pos.y + ball2.v * dt
if ball2.pos.y <= 0:
ball2.v = -ball2.v
yt2.plot( pos = (t, ball2.pos.y))
# 兩球碰撞
if mag(ball1.pos-ball2.pos) <= ball1.radius + ball2.radius :
ball1.v, ball2.v = collide(m1=ball1.m, v1=ball1.v, v2=ball2.v, m2=ball2.m)
|
作業:
三顆球從高空自由落下互相追撞。
本單元課程自2018.7.5日起已被瀏覽 241 次