Use of numerical integral method to calculate the length of the path has traversed the car during the time recording below
Don't you have more informations concerning the method of interpolation of the velocities ? Is this linear interpolation ?
Well, assuming linear interpolation of velocities, you can write: \[V(t) = V0 + (V1-V0) t\]\[D(t) = \int\limits\limits_{0}^{t}v(x).x.dx\] Here, V0 and V1 are 2 consecutive values in your array. Like 124 and 134 for the first 2 values. D(t) is the distance you went through during time t. For this particular example of yours, we're going to always use t=6 because your time steps are constant and always equal 6 seconds. I used http://integrals.wolfram.com/index.jsp?expr=a+x+%2B+b+x%5E2&random=false to perform the integral (although it's very easy to write it yourself) so we find that: \[D(t) = \int\limits\limits_{0}^{t}v(x).x.dx\ = \left[ \frac{ V0 }{ 2 } t^2 + \frac{ V1-V0 }{ 18 } t ^{3} \right]\] When solving for D(6) (t varying in [0,6]) we get: \[D(6) = 18 V0 + 12(V1-V0) = 12 V1 + 6 V0\] All you need to do next is to write a loop that sums \[12 V1 + 6 V0\] for all you time intervals. It goes like: float SumDistance = 0; for ( int TimeStepIndex=0; TimeStepIndex < TimeValuesCount-1; TimeStepIndex++ ) { float TimeStepDistance = 6 * Velocities[TimeStepIndex] + 12 * Velocities[TimeStepIndex+1]; SumDistance += TimeStepDistance; }
Sorry, my first equation should read: \[V(t) = V0 + \frac{ V1 - V0 }{ 6 } t\] because starting from V0 at t=0 second, we only reach V1 after 6 seconds...
If the actual assignment was to perform numerical integration using many small time steps then I suppose my solution is wrong, instead you should go all code and write something like: int IntegrationStepsCount = 1000; // The more you use, the more precise it will get, but never as precise as an analytical integration obviously... float SumDistance = 0; for ( int TimeStepIndex=0; TimeStepIndex < TimeValuesCount-1; TimeStepIndex++ ) { float TimeStepDistance = 0; float StartVelocity = Velocities[TimeStepIndex]; float EndVelocity = Velocities[TimeStepIndex+1]; float IntegrationStepSize = 6.0 / IntegrationStepsCount; for ( int i=0; i < IntegrationStepsCount ; i++ ) { float CurrentVelocity = StartVelocity + (EndVelocity - StartVelocity) * i / IntegrationStepsCount; TimeStepDistance += CurrentVelocity * IntegrationStepSize; // Distance += Velocity * DeltaTime } SumDistance += TimeStepDistance; } This is a purely numerical integration...
Join our real-time social learning platform and learn together with your friends!