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.
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.