Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.2 kB
4
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 = 0.5
g = 9.81
v0 = 4
alpha = 45 * pi/180
x0 = -1
m = 0.5
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 = gcurve(graph = w_graph, color = color.red)

w_s = gcurve(graph = w_graph, color = color.blue)

k_b = gcurve(graph = k_graph, color = color.red)

k_s = gcurve(graph = k_graph, color = color.blue)

myrate = 200

t = 0
dt = 0.001

work_ball = 0
work_stone = 0

K_ball_old = 1/2 * ball.mass*mag2(ball.v)
K_stone_old = 1/2 * stone.mass*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

    work_ball = work_ball + dot(F_ball, 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
    
    work_stone = work_stone + dot(F_stone, stone.v) * dt
    
    stone2.pos = vec(x0 + v0 * cos(alpha)*t, h + v0 * sin(alpha) * t - 0.5 * g * t**2, offset)
    K_ball = 1/2 * ball.mass*mag2(ball.v)
    K_stone = 1/2 * stone.mass*mag2(stone.v)
        
    delta_K_ball = K_ball-K_ball_old
    delta_K_stone = K_stone-K_stone_old
        
    w_b.plot(pos=(t, delta_K_ball))
    w_s.plot(pos=(t, delta_K_stone))
    
    k_b.plot(pos=(t, work_ball))
    k_s.plot(pos=(t, work_stone))
        
 
    t = t + dt
    
    






Leave a Comment