1

I have array of data points where x is speed and y is a timestamp, some data points are missing time, what would be correct formula to calculate interpolated time for certain speed based on other data point. Basically this is acceleration time up to certain speed. As I understand linear interpolation is not correct here since acceleration is not linear. Would be formula the same for deceleration(braking) time?

Example: [{ speed:9, time:1 }, {speed: 10, time: ???}, {speed: 10.5, time: 2,5}, ...., {speed: 20, time: ???}, {speed:21, time: 20}]

Clarification: What I am trying to do is calculate time for car needed to get from one speed to another speed. My input data looks like array of data points (speed, timestamp UTC). Speed comes in decimal values as 1.1, 1.9, 2.2 etc. So basically if I want to calculate time from 10 mph to 20 mph and my input data look like 9.5, 10.8 ..... 19.5, 20.9 I need to interpolate 10 and 20.

Kiryl Ch
  • 41
  • 4
  • 1
    This sounds like it would be more appropriate for [math.se], as it has nothing to do with programming. – SiKing Jul 18 '22 at 21:21
  • @SiKing "it has nothing to do with programming" comes across as unnecessarily antagonistic. More accurate to say "it is not a specific programming question" anyway. – Robert Dodier Jul 18 '22 at 22:38
  • do not mind having answer as method in any programming language since I am gonna code it anyway – Kiryl Ch Jul 18 '22 at 22:49

1 Answers1

1

As I understand linear interpolation is not correct here since acceleration is not linear.

The basic formula (including rearrangements) is:

acceleration = (end_velocity - start_velocity) / (end_time - start_time)
end_velocity = acceleration * (end_time - start_time) + start_velocity
end_time = (end_velocity - start_velocity) / acceleration + start_time

some data points are missing time

Then you need to use two "speed and time" data points to determine acceleration; then you can use one "acceleration and speed" to determine the missing time of a third data point.

Example (untested):

struct datapoint {
    double velocity;
    double time;
}


struct datapoint data[3] = {
    { 1.0, 2.0 },
    { 3.0, 4.0 },
    { 5.0, -1.0 }
}


void findTime(struct datapoint *result, struct datapoint *source1, struct datapoint *source2) {
    double acceleration;

    acceleration = (source2->velocity - source1->velocity) / (source2->time - source1->time);
    result->time = (result->velocity - source1->velocity) / acceleration + source1->time;
}


void test(void) {
    findTime(&data[2], &data[0], &data[1]);
}

Would be formula the same for deceleration(braking) time?

Yes, deceleration is merely "negative acceleration".

The problem you'll need to worry about is division by zero (and for programming and not mathematics, division by "small enough to be rounded to zero"). This will happen when an object's velocity doesn't change (or doesn't change enough). It indicates a problem like "if an object was moving at 1 meter per second an hour ago and is moving at 1 meter per second now, when was the object moving at 1 meter per second?" where no specific time could be considered the only correct answer.

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • as I understand you calculate velocity of third point based on previous two points but what if missing point is between two known points? – Kiryl Ch Jul 18 '22 at 23:15
  • @KirylCh: This is calculating the missing time value of a point. As long as acceleration is constant and the source data points are fully known, it doesn't matter which points are used - e.g. `findTime(&data[0], &data[1], &data[2]);` (or anything else) would be fine. – Brendan Jul 18 '22 at 23:18