Jump to content

Recommended Posts

Posted

I have a larger planning tool I use on my site, but wanted to try out the down and dirty of AI.  Well here the question went and results are in attached zip as well.:  (so long as the user makes a couple flights virtually any platform can use the resulting numbers to build a quick fuel calculator)

Quote

Please correct the following code to have user input for Distance in (NM), Altitude in (FT), Weight in (LBS) and Drag in (DU).  Then make adjustments to the interpolated values as follows:  


interpolate altitude from altitude 0 ft to altitude 45000 ft to have a calculated result of 0 lb/nm to 9 lb/nm 

interpolate weight from 26000 lbs to weight 52000 lbs to have a calculated result of 13 lb/nm to 16 lb/nm  

interpolate drag from 0 drag units to 315 drag units to have a calculated result of 0 lb/nm to 13 lb/nm 


Finally, add the interpolated weight lb/nm and drag lb/nm, then subtract the interpolated altitude lb/nm and last multiply by the distance travelled which will be the end result to the user.

 

Resulting script:

<!DOCTYPE html>
<html>
<head>
  <title>Interpolate and compute fuel consumption</title>
</head>
<body>
  <h1>F/A-18C Fuel Calculator</h1>
  <p>Enter the weight, distance, altitude, and drag:</p>
  <form>
    <label for="weight">Weight (lbs):</label>
    <input type="number" id="weight" name="weight"><br>
    <label for="distance">Distance (NM):</label>
    <input type="number" id="distance" name="distance"><br>
    <label for="altitude">Altitude (ft):</label>
    <input type="number" id="altitude" name="altitude"><br>
    <label for="drag">Drag (units):</label>
    <input type="number" id="drag" name="drag"><br>
    <button type="button" onclick="computeFuelConsumption()">Compute fuel consumption</button>
  </form>
  <p id="result"></p>
  <script>
    // Define the input data
    const weights = [26000, 52000];
    const drags = [0, 315];
    const altitudes = [0, 45000];

    // Interpolate lb/NM based on weight and altitude
    function interpolate(x, x0, x1, y0, y1) {
      const t = (x - x0) / (x1 - x0);
      const a = y1 - y0;
      return y0 + a * (t * t * (3 - 2 * t));
    }

    // Compute fuel consumption in lb/NM based on weight, distance, altitude, and drag
    function fuelConsumption(weight, distance, altitude, drag) {
      // Limit the weight, distance, altitude, and drag to their respective min/max values
      weight = Math.max(Math.min(weight, weights[1]), weights[0]);
      altitude = Math.max(Math.min(altitude, altitudes[1]), altitudes[0]);
      drag = Math.max(Math.min(drag, drags[1]), drags[0]);

      // Interpolate lb/NM based on weight and altitude
      const lbnm = interpolate(weight, weights[0], weights[1], 13, 16) - interpolate(altitude, altitudes[0], altitudes[1], 0, 9);

      // Interpolate drag value in lb/NM based on drag
      const drag_lbnm = interpolate(drag, drags[0], drags[1], 0, 13);

      // Compute fuel consumption in lb
      const fuel = lbnm + drag_lbnm;

      return (fuel * distance).toFixed(2); // Round to 2 decimal places
    }

    // Define a function to handle the button click
    function computeFuelConsumption() {
      // Get the input values
      const weight = parseInt(document.getElementById("weight").value);
      const distance = parseInt(document.getElementById("distance").value);
      const altitude = parseInt(document.getElementById("altitude").value);
      const drag = parseInt(document.getElementById("drag").value);

      // Compute the fuel consumption
      const fuel = fuelConsumption(weight, distance, altitude, drag);

      // Set the result text
      document.getElementById("result").textContent = `Fuel consumption: ${fuel} lb`;
    }
  </script>
</body>
</html>

FA-18C Fuel Calculator.zip

  • Like 3
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...