Using Polynomials in a Rails Project

Posted by Richard-Burd on December 3, 2019

Background

For my Rails portfolio project I decided to build an app that could be used by military and aid organizations operating in expeditionary environments, that is, out in the middle of nowhere, in a war zone, or in a developing economic zone. For safety, people in such environments often travel in convoys (a bunch of cars, one behind the other) that rarely stop to get gas because doing so is either unsafe, or impractical. Here is a United Nations (UN) convoy delivering humanitarian aid to civilians in Syria during the civil war in that country:

Imgur

My app is pretty simple, it considers the following variables and tells the user if a convoy has enough fuel (or not) to reach a given destination:

  • The miles per gallon for each individual vehicle in the convoy.
  • The total amount of fuel each vehicle in the convoy is carrying.
  • What type of terrain the convoy is traveling on during different segments of the journey: paved road, dirt road, or off road.
  • The slope of the grade the convoy is traveling on; the convoy will need more fuel to travel uphill and less to travel downhill.

Curve Fitting to Generate a Polynomial Equation

I quickly ran into a problem with this project when I realized I can’t do math. I needed a way to get an equation to calculate the impact of terrain sloping on fuel consumption. The steeper the hill, the more fuel will be used…conversely, if you drove downhill for 7 straight miles you would use less fuel then you would going uphill or even on flat terrain. Enter the imprecise art of something called curve fitting. This is where you do fancy math-y stuff without doing any actual math. To do this yourself, you will need to create an account on geogebra.org and watch this video here. I took a stab at guessing some data points and created this workspace here and in turn, got a polynomial equation I could use in my code to calculate the impact of terrain slope on fuel consumption here. Here is what the equation looks like:

Imgur

The basic idea is to add the data points on the x-y axis that you think may be correct, or that you know are correct from some source of information such as field testing. In my example, I am guessing that, on average, a vehicle will consume twice as much fuel on a 1:1 slope (which is 45°) as it would if it were on flat terrain. I don’t know this to be a fact, and my research indicates that fuel consumption varies widely between vehicle types…but for purposes of my app I will use this guess as a starting point. When I have data that is more realistic, I will change the ruby method below (and only that ruby method) once I have a newer, more accurate polynomial equation:

def graphed_function_1(x)
	1.87*x**3 + 0.11*x
end

Conclusion

Curve fitting has a wide variety of applications in programming, particularly when you are trying to optimize efficiencies. The next step of course is to have your program do the curve fitting for you so that your users enter in data points and then get polynomial equations as outputs that in turn expose the user to the optimal settings for whatever problems they are trying to solve.