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

Java examples for java.lang:Math Trigonometric Function

Description

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

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] points = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        double[] values = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        double interpolateAt = 2.45678;
        System.out.println(interpolate(points, values, interpolateAt));
    }//w  w  w. jav  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 Tutorials