# Straight line with complications

I am trying to measure fuel consumption from OBD data from a car.

I get values for the sum of the product of engine load and airflow for all observations in a trip. I know that fuel consumption is a linear function of this value, but I don't know the coefficients. Let's call this value airload

In this first version I am trying to make a simple algorithm, setting max and min values for the fuel consumption that can be calculated. Let's say this minimum is 5 liters per 100km, and the maximum is 9.

For each trip I calculate this:

Fuel consumption liters per hour = intercept + slope × airload. Fuel consumption liters per 100km = Fuel consumption liters per hour × trip time / trip distance

If this calculation is below the expected minium, I increase the intercept just as much as needed to get the minimum value as a result.

If this calculation is above the expected maximum, I increade the slope just as much as needed to get the maxium value as a result.

When I have a lot of trips I would not expect this to be 100% correct, but I would expect the algorithm to calculate values between min and max.

But this doesn't work, I get negative slopes and other problems.

My question is this: What mechanism should I use to calibrate the linear function so that the values are between min and max?

The complicating factor is that the linear function calculates fuel consumption in liters per hour, and that has be converted to liters per 100 km with data for one trip. There is something here I cannot fully grasp, I would be grateful of anyone could help.

Edit:

I realise that the function I end up with is not linear.

$fuel_{l/100km} = (intercept + airload*slope)*time/distance$

And the question is: How do I update intercept and slope so that this never calculates values below the minimum limit or above the maximum limit. It must be done so that if the old trips were recalculated, they would also end up between min and max.

edit 2:

Here are some sample data. Let's say I want these to estimate consumption between 5 and 9 liters per 100km.

distance   time                 air*load
(km)       (hours)
12,19   0,278333333333333   151,356079101563
2,282   0,0588888888888889  1630,91814661774
4,188   0,0788888888888889  1187,94004800139
2,428   0,0533333333333333  458,380833387931
34,743  0,548611111111111   1292,71204459255
34,607  0,614722222222222   1379,45206970902
33,554  0,678888888888889   1197,35476190243
1,429   0,0930555555555556  1263,5320338142
34,618  0,631111111111111   1377,86593901133
39,633  0,986944444444444   1028,77216338065
0,614   0,0355555555555556  517,011641880112
57,13   0,854722222222222   525,678311725693
55,023  0,779722222222222   525,678311725693
34,694  0,546111111111111   525,678311725693
56,08   0,682777777777778   1825,42975219665


• Is there any reason why can't you simply use linear regression? – Tim Sep 21 '16 at 13:39
• I think yes. I don't have the correct values for fuel consumption to train it with. I only have my estimate, which I want to be in a given interval. But maybe I am wrong? – Terje Kolderup Sep 21 '16 at 13:50
• But if you don't have the data then how do you know min and max ..? – Tim Sep 21 '16 at 13:57
• Could you possibly post some example data for clarity (e.g. 10 random rows of the table that you are working with copy and pasted as text table)? This would make easier to understand your problem. – Tim Sep 21 '16 at 14:02
• Now I have added the data. – Terje Kolderup Sep 23 '16 at 14:01