Here you can find the source of interpolate(double[] points, double[] values, double interpolateAt)
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. |
public static double interpolate(double[] points, double[] values, double interpolateAt)
//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; } } }