Java Array Interpolate interpolate(double[] points, double[] values, double interpolateAt)

Here you can find the source of interpolate(double[] points, double[] values, double interpolateAt)

Description

Estimates the function value at a specified point on x-axis using the Lagrange interpolation.

License

Open Source License

Parameter

Parameter Description
points Points on x-axis at which the function values are known.
values Function values in the specified points.
interpolateAt Point on x-axis whose function value is to be computed.

Return

Estimated function value at the point on x-axis for which function value was sought.

Declaration

public static double interpolate(double[] points, double[] values, double interpolateAt) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//ww  w  .j  av  a  2 s.c  o  m
     * Estimates the function value at a specified point on x-axis using the
     * Lagrange interpolation. Points on x-axis and function values at these
     * points define points in 2-D space through which the graph of the function
     * goes. The points are used to estimate the function value of a point on
     * x-axis whose function values is not known.
     *
     * @param points   Points on x-axis at which the function values are known.
     * @param values   Function values in the specified points.
     * @param interpolateAt   Point on x-axis whose function value is to be
     * computed.
     * @return   Estimated function value at the point on x-axis for which
     * function value was sought.
     */
    public static double interpolate(double[] points, double[] values, double interpolateAt) {
        if (points.length != values.length) {
            throw new ArithmeticException("The number of elements in both arrays does not match!");
        } else {
            double value = 0;
            double tempValue = 0;
            for (int i = 0; i < points.length; i++) {
                tempValue = values[i];
                for (int j = 0; j < i; j++) {
                    tempValue *= (interpolateAt - points[j]) / (points[i] - points[j]);
                }
                for (int j = i + 1; j < points.length; j++) {
                    tempValue *= (interpolateAt - points[j]) / (points[i] - points[j]);
                }
                value += tempValue;
            }
            return value;
        }
    }

    /**
     * Estimates the function value at a specified point on x-axis using the
     * Lagrange interpolation. Points on x-axis and function values at these
     * points define points in 2-D space through which the graph of the function
     * goes. The points are used to estimate the function value of a point on
     * x-axis whose function values is not known.
     *
     * @param points   Points on x-axis at which the function values are known.
     * @param values   Function values in the specified points.
     * @param interpolateAt   Point on x-axis whose function value is to be
     * computed.
     * @return   Estimated function value at the point on x-axis for which
     * function value was sought.
     */
    public static double interpolate(int[] points, int[] values, double interpolateAt) {
        if (points.length != values.length) {
            throw new ArithmeticException("The number of elements in both arrays does not match!");
        } else {
            double value = 0;
            double tempValue = 0;
            for (int i = 0; i < points.length; i++) {
                tempValue = values[i];
                for (int j = 0; j < i; j++) {
                    tempValue *= (interpolateAt - points[j]) / (points[i] - points[j]);
                }
                for (int j = i + 1; j < points.length; j++) {
                    tempValue *= (interpolateAt - points[j]) / (points[i] - points[j]);
                }
                value += tempValue;
            }
            return value;
        }
    }
}

Related

  1. interpolate(double x, double xLeft, double yLeft, double xRight, double yRight, double[] weights)
  2. interpolate(double x, double[] begin, double[] end)
  3. interpolate(double xa[], double ya[], double x)
  4. interpolate(double[] array, int[] translation, double index)
  5. interpolate(double[] end0, double[] end1, double[] mid)
  6. interpolate(double[] x, double D)
  7. interpolate(double[] X, double[] Y, double[] Z)
  8. interpolate(double[] x, int newLength)
  9. interpolate(float out[], float in1[], float in2[], int in2_idx, float coef, int length)