Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.4 kB
3
Indexable
Never
Web VPython 3.2
scene.background = color.white

ground = box(pos=vec(0,0.2,0), size = vec(3,0.02,0.4), color = color.green)

h = 5
g = 9.81
v0 = 4
alpha = 135 * pi/180
x0 = -1
m = 1
offset = 0

ball = sphere(pos=vec(x0,h,0), mass = m, 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.mass * ball.v

stone = sphere(pos=vec(x0,h,0), mass = m, angle = alpha, radius = 0.01, color = color.blue, make_trail = True)
stone.v = v0*vec(cos(stone.angle), sin(stone.angle), 0)
stone.p = stone.mass * stone.v

stone2 = sphere(pos=vec(x0,h,offset), mass = m, angle = alpha, radius = 0.01, color = color.cyan, make_trail = True) 

w_graph = graph(title = "Work", xtitle = "t", ytitle = "Joules")

k_graph = graph(title = "Change in Kinetic Energy", xtitle = "t", ytitle = "k(t)")

w_b_x = gcurve(graph = w_graph, color = color.red)
w_b_y = gcurve(graph = w_graph, color = color.orange)

w_s_x = gcurve(graph = w_graph, color = color.blue)
w_s_y = gcurve(graph = w_graph, color = color.cyan)

k_b_x = gcurve(graph = k_graph, color = color.red)
k_b_y = gcurve(graph = k_graph, color = color.orange)

k_s_x = gcurve(graph = k_graph, color = color.blue)
k_s_y = gcurve(graph = k_graph, color = color.cyan)

myrate = 200

t = 0
dt = 0.001

work_ball = 0
work_stone = 0

K_ball = 1/2 * ball.mass*mag2(ball.v)
K_stone = 1/2 * ball.stone*mag2(stone.v)
K_ball_old = 1/2 * ball.mass*mag2(ball.v)
K_stone_old = 1/2 * ball.stone*mag2(stone.v)

scene.waitfor("click")

while stone.pos.y > 0:
    rate(myrate)
    
    F_ball = vec(0,0,0)
    
    ball.p = ball.p + F_ball * dt
    ball.v = ball.p/ball.mass
    ball.pos = ball.pos + ball.v * dt
    
    F_stone = -stone.mass * g * vec(0,1,0)
    stone.p = stone.p + F_stone * dt
    stone.v = stone.p/stone.mass
    stone.pos = stone.pos + stone.v * dt
    
    stone2.pos = vec(x0 + v0 * cos(alpha)*t, h + v0 * sin(alpha) * t - 0.5 * g * t**2, offset)
    
    delta_K_ball = K_ball-K_ball_old
    delta_K_stone = K_stone-K_stone_old
    
    w_b_x.plot(pos=(t, work_ball))
    w_b_y.plot(pos=(t, work_ball))
    
    k_b_x.plot(pos=(t, K_ball))
    k_b_y.plot(pos=(t, K_ball))
    
    w_s_x.plot(pos=(t, work_stone))
    w_s_y.plot(pos=(t, work_stone))
    
    k_s_x.plot(pos=(t, K_stone))
    k_s_y.plot(pos=(t, K_stone))
    
 
    t = t + dt
    
    






Leave a Comment