Java Linear Interpolate linearInterpReal(double dX, double[] dXCoordinate, double[] dYCoordinate)

Here you can find the source of linearInterpReal(double dX, double[] dXCoordinate, double[] dYCoordinate)

Description

linear Interp Real

License

Open Source License

Declaration

public static double linearInterpReal(double dX, double[] dXCoordinate, double[] dYCoordinate)
            throws Exception 

Method Source Code

//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;
    }
}

Related

  1. linearInterpolation(double x, double x1, double x2, double y1, double y2)
  2. linearInterpolation(double x0, double y0, double x1, double y1, double x)
  3. linearInterpolation(double y2, double y1, double deltaX, double x1)
  4. LinearInterpolation(float a, float b, float f)
  5. linearInterpolation(float min, float max, float min2, float max2, float value)