回上方

第15堂課

單擺程式碼:

 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
from vpython import *

g = 9.8

scene = canvas(center=vector(0,-1.0,0), background=vector(0.5,0.5,0))
ceiling = box(length=1, width=1, height = 0.01, color=color.blue)
ball = sphere(radius=0.1, color=color.red, make_trail=True)

ball.pos = vector(1,-1, 0)
ball.m = 0.5
ball.v = vector(0,0,0)

line = cylinder(radius = 0.01)
line.pos = ceiling.pos
line.axis = ball.pos - ceiling.pos
line.L = line.length
K = 100000.0

dt = 0.001

while True:
    rate(100)
    F = - K * (line.length-line.L) * line.axis.norm()
    ball.a = vector(0,-g,0) + F/ball.m
    ball.v = ball.v + ball.a * dt
    ball.pos = ball.pos + ball.v * dt
    line.axis = ball.pos - line.pos

作業:

(簡易牛頓擺)請修改以下程式碼,運用彈性碰撞讓右方單擺撞擊左方單擺,看看兩顆球運動情形。

 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
from vpython import *
g = 9.8

scene = canvas(center=vector(0,-1.0,0), background=vector(0.5,0.5,0))
ceiling = box(length=1, width=1, height=0.01, color=color.blue)

ball = sphere(radius=0.1, color=color.red, make_trail=True)
ball.pos = vector(0.6,-0.8, 0)
ball.m = 0.5
ball.v = vector(0,0,0)

ball2 = sphere(radius=0.1, color=color.red, make_trail=True)
ball2.pos = vector(-0.2,-1, 0)
ball2.m = 0.5
ball2.v = vector(0,0,0)

line = cylinder(radius = 0.01)
line.pos = ceiling.pos
line.axis = ball.pos - line.pos
line.L = line.length

line2 = cylinder(radius=0.01)
line2.pos = vector(-0.2,0,0)
line2.axis = ball2.pos - line2.pos
line2.L = line2.length

K = 100000.0

dt = 0.001

while True:
    rate(100)
    F = - K * (line.length-line.L) * line.axis.norm()
    ball.a = vector(0,-g,0) + F/ball.m
    ball.v = ball.v + ball.a * dt
    ball.pos = ball.pos + ball.v * dt
    line.axis = ball.pos - line.pos

本單元課程自2018.7.5日起已被瀏覽 250