Here you can find the source of linearInterpReal(double dX, double[] dXCoordinate, double[] dYCoordinate)
public static double linearInterpReal(double dX, double[] dXCoordinate, double[] dYCoordinate) throws Exception
//package com.java2s; //License from project: Open Source License public class Main { public static double linearInterpReal(double dX, double[] dXCoordinate, double[] dYCoordinate) throws Exception { if (dXCoordinate == null || dYCoordinate == null) return 0; //find where x lies in the x coordinate if (dXCoordinate.length == 0 || dYCoordinate.length == 0 || dXCoordinate.length != dYCoordinate.length) throw new Exception("linearInterpReal: Lengths of passed in arrays are incorrect"); for (int iIndex = 0; iIndex < dXCoordinate.length; iIndex++) { if (dX <= dXCoordinate[iIndex]) { //Check to see if dX is exactly equal to dXCoordinate[iIndex] //If so then don't calculate dY. This was added to remove roundoff error. if (dX == dXCoordinate[iIndex]) return dYCoordinate[iIndex]; //Found position else if (iIndex == 0) return dYCoordinate[iIndex]; else { //interpolate - y = mx+c if ((dXCoordinate[iIndex] - dXCoordinate[iIndex - 1]) == 0) return dYCoordinate[iIndex - 1]; else return ((dYCoordinate[iIndex] - dYCoordinate[iIndex - 1]) / (dXCoordinate[iIndex] - dXCoordinate[iIndex - 1]) * (dX - dXCoordinate[iIndex - 1]) + dYCoordinate[iIndex - 1]); }//from w w w . j a v a2 s.co m } else if (iIndex == (dXCoordinate.length - 1)) return dYCoordinate[iIndex]; } // END OF FOR LOOP return 0.0; } }