#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Basic physics program, please help!


    t = 0
    dt = 0.04
    y = -0.03
    m = 0.2
    k = 20

    while (t < 1.00):
    a = -k * y / m
    vi = a * dt / 2
    v = vi + a * dt
    y = y + v * dt
    print (round(t,3), round(y,3))
    t = t + dt


    This program is supposed to model the path of a spring at every 0.04 seconds using a leapfrog method (the v is calculated at dt / 2, and that v is used to calculate the position at every 0.04 sec).

    Some formulas that are used are vf = vi + a*dt; yf = yi + vf*dt; and a=(-k*y)/m.

    I keep tinkering with the code with no desired outcome. Any and all help will be appreciated!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Code:
    import math
    
    t = 0     # time 0
    dt = 0.04 # time step
    y = -0.03 # spring stretch. -0.03 is displacement from relaxed length at time 0.
    m = 0.2   # mass
    k = 20    # sprint constant
    c = -k/m  # related to frequency
    v = 0     # velocity of mass at end of spring at start.
    
    # insert here a computation to set v to the velocity at t=dt/2
    #
    #
    #
    
    while (t < 1.00):
        a = c * y
        v += a * dt
        y += v * dt
        print(round(t,3), round(y,3))
        t += dt
    
    frequency = math.sqrt(abs(c))/(2*math.pi)
    print('# ',round(frequency,3))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Interesting experiments.

    Insert a driving force, perhaps such that the other end of the spring undergoes periodic displacement.

    Use friction, drag proportional to the negative of velocity, or perhaps limit large displacements.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo