Interpolation

Assignment 11

by Jake Jernigan

HTML

Please enter a number (between 0 and 6):
<br/>
<input type="textbox" id="number" number="" />
<input type="button" id="button1" value="Enter number" onClick="Interpolation()" />
<br/>
<br>
<div id="output1"></div>

JavaScript

var table = [0.00, 0.04, 0.11, 0.60, 0.87, 0.95, 1.00];

function Interpolation() {
  clearDisplay();
  var n = parseFloat(document.getElementById("number").value);
  if (n < 0 || n > 6) {
    document.getElementById("output1").innerHTML += "Please enter a number between 0 and 6.";
  } else if (n == 0 || n == 1 || n == 2 || n == 3 || n == 4 || n == 5 || n == 6) {
    document.getElementById("output1").innerHTML += "The rainfall at " + n + " hours was " + table[n] + " of the total rainfall.";
  } else {
    var x = n;
    var x1 = Math.floor(x);
    var x2 = Math.ceil(n);
    var y1 = table[x1];
    var y2 = table[x2];
    var y = y1 + (y2 - y1) * ((x - x1) / (x2 - x1));
    var result = parseFloat(y).toFixed(2);
    document.getElementById("output1").innerHTML += "The rainfall at " + n + " hours was " + result + " of the total rainfall.";
  }
}

function clearDisplay() {
  document.getElementById("output1").innerHTML = "";
}