I just lately started driving a automobile that had historically used premium fuel (93 octane). According to the maker, although, it requires solely 91 octane. The factor is, within the US, you should buy solely 87, 89, or 93 octane. Where I stay, fuel costs soar 30 cents per gallon soar from one grade to the subsequent, so premium prices 60 cents greater than common. So why not attempt to avoid wasting cash?
It’s simple sufficient to attend till the fuel gauge exhibits that the tank is half full after which fill it with 89 octane, and there you could have 91 octane. But it will get difficult to know what to do subsequent—half a tank of 91 octane plus half a tank of 93 finally ends up being 92, and the place do you go from there? You could make persevering with calculations, however they get more and more messy. This is the place Python got here into the image.
I wished to provide you with a easy scheme by which I may fill the tank at some stage with 93 octane, then on the similar or another stage with 89 octane, with the first objective to by no means get beneath 91 octane with the ultimate combination. What I wanted to do was create some recurring calculation that makes use of the earlier octane worth for the previous fill-up. I suppose there can be some polynomial equation that may clear up this, however in Python, this feels like a loop.
o = 93.zero
newgas = 93.zero # this represents the octane of the final fillup
i = 1
whereas i < 21: # 20 iterations (journeys to the pump)
if newgas == 89.zero: # if the final fillup was with 89 octane
# change to 93
newgas = 93.zero
o = newgas/2 + o/2 # fill when gauge is half of full
else: # if it wasn't 89 octane, change to that
newgas = 89.zero
o = newgas/2 + o/2 # fill when gauge says half of full
print str(i) + ': '+ str(o)
i += 1
As you possibly can see, I’m initializing the variable o (the present octane combination within the tank) and the variable newgas (what I final stuffed the tank with) on the similar worth of 93. The loop then will repeat 20 occasions, for 20 fill-ups, switching from 89 octane and 93 octane for each different journey to the station.
This exhibits is that I most likely want solely 10 or 15 loops to see stabilization. It additionally exhibits that quickly sufficient, I undershoot my 91 octane goal. It’s additionally fascinating to see this stabilization of the alternating combination values, and it seems this occurs with any scheme the place you select the identical quantities every time. In reality, it’s true even when the quantity of the fill-up is totally different for 89 and 93 octane.
So at this level, I started enjoying with fractions, reasoning that I’d most likely want a much bigger 93 octane fill-up than the 89 fill-up. I additionally didn’t need to make frequent journeys to the fuel station. What I ended up with (which appeared fairly good to me) was to attend till the tank was about 7⁄12 full and fill it with 89 octane, then wait till it was ¼ full and fill it with 93 octane.
Here is what the modifications within the loop appear to be:
if newgas == 89.zero:
newgas = 93.zero
o = three*newgas/four + o/four
newgas = 89.zero
o = 5*newgas/12 + 7*o/12
Here are the numbers, beginning with the tenth fill-up:
As you possibly can see, this retains the ultimate octane very barely above 91 on a regular basis. Of course, my fuel gauge isn’t marked in twelfths, however 7⁄12 is barely lower than 5⁄eight, and I can deal with that.
An various easy answer might need been run the tank to empty and fill with 93 octane, then subsequent time solely half-fill it for 89—and maybe this will likely be my default plan. Personally, I’m not a fan of working the tank all the best way down since this isn’t all the time handy. On the opposite hand, it may simply work on a protracted journey. And generally I purchase fuel due to a sudden drop in costs. So ultimately, this scheme is certainly one of a sequence of choices that I can contemplate.
The most essential factor for Python customers: Don’t code whereas driving!