LAB8 3/4 CLOSE
unknown
python
9 months ago
2.0 kB
7
Indexable
Web VPython 3.2
from visual import *
scene = canvas(background = color.white)
ground = box(pos = vec(0,-0.02,0), size = vec(3,0.02,0.4), color = color.green)
v0 = 2.5
g = 9.81
h = 0
x0 = 0
alpha = 35 * pi/180
ball = sphere(pos = vec(x0,h,0), mass = 0.5, angle = alpha, radius = 0.01, color = color.red, make_trail = True)
ball.v = v0 * vec(cos(ball.angle), sin(ball.angle), 0)
ball.p = ball.v * ball.mass
ball.K = 0.5 * ball.mass * mag(ball.v)**2
stone = sphere(pos = vec(x0,h,0), mass = 0.5, angle = alpha, radius = 0.01, color = color.black, make_trail = True)
stone.v = v0 * vec(cos(stone.angle), sin(stone.angle), 0)
stone.K = 0.5 * stone.mass * mag(stone.v)**2
stone.p = stone.v * stone.mass
t = 0
dt = 0.001
KE_graph = graph(title='Kinetic Energy vs Time', xtitle='t (s)', ytitle='K (J)')
KE_ball_curve = gcurve(graph=KE_graph, color=color.red)
KE_stone_curve = gcurve(graph=KE_graph, color=color.black)
W_graph = graph(title='Work Done vs Time', xtitle='t (s)', ytitle='W (J)')
W_ball_curve = gcurve(graph=W_graph, color=color.blue)
W_stone_curve = gcurve(graph=W_graph, color=color.green)
while stone.pos.y >= ground.pos.y :
rate(200)
F_ball = ball.mass * vec(0,0,0)
ball.p = ball.p + F_ball*dt
ball.v = ball.p/ball.mass
ball.pos = ball.pos + ball.v*dt
W_ball = mag(F_ball) * (ball.v.y * dt) + (W_ball)
F_stone = stone.mass * vec(0,-g,0)
stone.p = stone.p + F_stone*dt
stone.v = stone.p/stone.mass
stone.pos = stone.pos + stone.v*dt
W_stone = mag(F_stone) * (stone.v.y * dt) + (W_stone)
K_ball_new = 0.5 * ball.mass * mag(ball.v) ** 2
K_stone_new = 0.5 * stone.mass * mag(stone.v) ** 2
ball.K = K_ball_new
stone.K = K_stone_new
KE_ball_curve.plot(t, K_ball_new)
KE_stone_curve.plot(t, K_stone_new)
W_ball_curve.plot(t, W_ball)
W_stone_curve.plot(t, W_stone)
t += dt
Editor is loading...
Leave a Comment