python - Slowing down a physics defying spring -
i doing tutorial physics class.
i made program should have cart on box moved spring , should slow down stop, however, when run it, appears spring accelerating cart (no matter things negate.)
i have heard might issue vpython rounding numbers resulting in acceleration, if true, can make numbers 1000 times larger , fix it?
thanks!
from visual import * visual.graph import * length=1.0 track=box(pos=vector(0,-0.05,0), size=(length, 0.05, 0.10), material=materials.bricks,) # creates track "length"meters in # x direction, 0.05m tall, , 1m deep start=-0.5*length+0.05 cart=box(pos=vector(start+0.01,0,0), size=(0.1,0.05,0.1), color=color.green) k=-4 #spring constant sprl=(start-0.05)-0.1 #sets position of left end of spring spring=helix(pos=(sprl,0,0), axis=((cart.x-0.05)-sprl,0,0), radius=0.02, color=color.yellow) cart.m=0.70 #mass of cart cart.vel=vector(0,0,0) #initial velocity of cart cart.force = k*(cart.x)*vector(1,0,0) #force of spring cart.accel=cart.force/cart.m #acceleration of cart taking account fan t=0 deltat=0.01 end=0.5*length-0.05 #defining end of track gdisplay(x=100, y=500, xtitle='time (sec)', ytitle='x (cyan), px (red)') xcurve = gcurve(color=color.cyan) pcurve= gcurve (color=color.red) while cart.x<end+0.01 , (cart.x>(start-0.01)): #we include -0.01 cart not fail upon start... cart.pos = cart.pos + cart.vel*deltat+(0.5)*(cart.accel)*deltat**2 #x equals x naught plus v times delta t plus 1 half delta t squared #note ** means "to power of" xcurve.plot(pos=(t,cart.x)) pcurve.plot(pos=(t,cart.vel.x)) cart.vel=cart.vel+cart.accel*deltat #new velocity old velocity plus acceleration times time cart.force=k*(cart.x)*vector(1,0,0) cart.accel=cart.force/cart.m spring.axis=((cart.x-0.05)-sprl,0,0) t=t+deltat #increments time rate(100) #rate means no more 100 loops per second
you don't have dissipative force in system (one leaks energy out of it). equation f = -kx
conserves energy (this equation have circuitously encoded , represents force spring exerts on object). note equation there doesn't mean force negative, points in opposite direction of cart.pos
. how sinusoidal motion.
a dissipative force needed object slow down. quintessential example of represented f = -kx -bv
constant b
, v
velocity of object. represents spring being slowed down fluid (air/water/whatever like).
the minimal change code in case inside loop:
cart.force=(k*(cart.x)-0.1*cart.vel.x)*vector(1,0,0)
this produces underdamped system, try out overdamped system can set 0.1 10 instead.
Comments
Post a Comment