0

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?

Sys
  • 3
  • 2
  • Your first code solves the actual physical equation and calculates the actual distance. Your second code first discretizes time into timesteps, then calculates the distance traveled under this model. Time in the real world doesn't happen in `i++` increments. It happens continuously. Thus the result you get is an approximation of the real physical result. Try replacing `i++` with `i += 0.5` or `i += 0.1` and you should get a better approximation (but at the cost of a longer computing time). – Stef Apr 12 '22 at 15:58
  • oh, I see, that seems so obvious to me now. My problem is that I need time to be a whole number. thanks for the comment. I actually remember now having the same issue when dealing with gravity. – Sys Apr 12 '22 at 16:24
  • You can use a whole number of "smaller time units". Or if the problem is the use of floating-point, you could use a fixed-point type (I'm not familiar enough with Kotlin to suggest one). – Stef Apr 12 '22 at 16:32

1 Answers1

1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Sorry it has been a while since I've had Calculus or Physics. I keep getting stuck on "integrate the equation for acceleration twice" when I tried looking it up, I run into derivatives which sound familiar but absolutely stump to the point of I don't think that that is what I'm looking for – Sys Apr 12 '22 at 18:58
  • See my edits. I've done the integrations for you. Simple integration of a function of one independent variable. – duffymo Apr 12 '22 at 19:31
  • Found an arithmetic error: -10/8 m/sec^2 acceleration should -1.25 m/sec^2. Sorry. – duffymo Apr 12 '22 at 22:24
  • I was able to come to the right answer using the formulas so The arithmetic error didn't come up for me – Sys Apr 13 '22 at 22:42