mathtimedistancevelocityacceleration

How do I apply velocity and acceleration to match the result of math formulas


So I have an initial velocity iv a final velocity fv (that is always 0) a time t and an acceleration variable a

I use these variables to calculate final distance fd

Note: language used here is Kotlin

Note: Formula used for calculating fd and a are not something I came up with

    var iv = 10.0 // initial velocity
    var fv = 0.0  // final velocity
    var t = 8.0   // time
    var a = ((fv - iv)/t) // acceleration
    var fd: Double = ((iv*t) + (a/2.0*Math.pow(t,2.0)))

I get the result that fd = 40.0

when I try to model this the way I would try to apply it in code.

    var d = 0.0 // current distance traveled
    var i = 0   // current time elapsed 
    
    while (i < t) {      
        d += v
        v += a
            
        i++
    }

I end up with the result of d = 45.0 when d should equal fd at the end.

what am I doing wrong in applying velocity and acceleration to velocity so that my results differ from what the mathematical formulas show they should be?


Solution

  • Don't worry about "formulas" - think about the physics.

    If you have ever studied calculus and physics you know that:

    a = dv/dt // a == acceleration; v == velocity; t == time
    v = ds/dt // v == velocity; s == distance; t == time
    

    If you know calculus well enough you can integrate the equation for acceleration twice to get the distance traveled as a function of time:

    a(t) = dv/dt = a0
    v(t) = ds/dt = a0*t + v0
    s(t) = (a0/2)*t^2 + v0*t + s0
    

    You can calculate the constants:

    a0 = -1.25 m/sec^s
    v0 = 10 m/s
    s0 = 0 m
    

    Substituting:

    a(t) = -1.25
    v(t) = 10 - 1.25*t
    s(t) = -0.625*t^2 + 10*t = (10 - 0.625*t)*t
    

    You can also calculate the answer numerically. That's what you're doing with Kotlin.

    If you know the initial conditions

    a(0), v(0), and s(0)
    

    you can calculate the value at the end of a time increment dt like this:

    a(t+dt) = f(t+dt)
    v(t+dt) = v(t) + a(t)*dt
    s(t+dt) = s(t) + v(t)*dt
    

    Looks like you are assuming that acceleration is constant throughout the time you're interested in.

    You don't say what units you're using. I'll assume metric units: length in meters and time in seconds.

    You decelerate from an initial velocity of 10 m/sec to a final velocity of 0 m/second over 8 seconds. That means a constant acceleration of -1.25 m/sec^2.

    You should be able to substitute values into these equations and get the answers you need.

    Do the calculations by hand before you try to code them.