📜  使用拉格朗日公式实现逆插值的程序

📅  最后修改于: 2021-04-29 02:27:08             🧑  作者: Mango

给定任务是找到未知函数y = f(x)的给定y的x值,其中给出了一些点(x,y)对的值。

令y = f(x)是一个未知函数,其中x是一个独立变量。
对于x的不同值,说x_0, x_1, x_2, ..., x_m ( i.e x_k, k=0, 1, 2, 3...m)各自的值y_0, y_1, y_2, ..., y_m ( i.e y_k = f(x_k), k=0, 1, 2, 3...m)给定的。

借助给定的未知函数观测值,找到位于两个列表值之间的y的给定值的独立变量x的过程,称为逆插值

这通常用于检查未知函数f的输出y的正确性,即该输出y的自变量x与原始输入有多少不同。

逆插值问题可以使用拉格朗日公式解决。

拉格朗日公式:
逆插值的公式与插值公式相似,但变化很小。

为了解决逆插值的问题,将x和y的位置互换。逆插值的公式为:

 x = \frac{(y-y_1)(y-y_2)(y-y_3)....(y-y_m)}{(y_0-y_1)(y_0-y_2)(y_0-y_3)....(y_0-y_m)}x_0  + \frac{(y-y_0)(y-y_2)(y-y_3)....(y-y_m)}{(y_1-y_0)(y_1-y_2)(y_1-y_3)....(y_1-y_m)}x_1  +         \frac{(y-y_0)(y-y_1)(y-y_3)....(y-y_m)}{(y_2-y_0)(y_2-y_1)(y_2-y_3)....(y_2-y_m)}x_2 + ...... + \frac{(y-y_0)(y-y_1)(y-y_2)(y-y_3)....(y-y_m)}{(y_m-y_0)(y_m-y_1)(y_m-y_2)((y_m-y_3)....(y_m-y_{m-1})}x_m

当点之间的间距不相等时,甚至可以使用此方法。在此,x表示为y的函数。

例子:

图形:

算法:
此处,数据是由x和y组成的点的列表,而n是数据点的数量。

执行:

C++
// C++ code for solving inverse interpolation
  
#include 
using namespace std;
  
// Consider a structure
// to keep each pair of
// x and y together
struct Data {
    double x, y;
};
  
// Function to calculate
// the inverse interpolation
  
double inv_interpolate(Data d[], int n, double y)
{
    // Initialize final x
    double x = 0;
  
    int i, j;
  
    for (i = 0; i < n; i++) {
  
        // Calculate each term
        // of the given formula
        double xi = d[i].x;
        for (j = 0; j < n; j++) {
  
            if (j != i) {
                xi = xi
                     * (y - d[j].y)
                     / (d[i].y - d[j].y);
            }
        }
  
        // Add term to final result
        x += xi;
    }
  
    return x;
}
  
// Driver Code
int main()
{
  
    // Sample dataset of 4 points
    // Here we find the value
    // of x when y = 4.5
    Data d[] = { { 1.27, 2.3 },
                 { 2.25, 2.95 },
                 { 2.5, 3.5 },
                 { 3.6, 5.1 } };
  
    // Size of dataset
    int n = 4;
  
    // Sample y value
    double y = 4.5;
  
    // Using the Inverse Interpolation
    // function to find the
    // value of x when y = 4.5
    cout << "Value of x at y = 4.5 : "
         << inv_interpolate(d, n, y);
  
    return 0;
}


Java
// Java code for solving inverse interpolation
class GFG
{
  
// Consider a structure
// to keep each pair of
// x and y together
static class Data
{
    double x, y;
  
    public Data(double x, double y)
    {
        super();
        this.x = x;
        this.y = y;
    }
      
};
  
// Function to calculate
// the inverse interpolation
static double inv_interpolate(Data []d, int n, double y)
{
    // Initialize final x
    double x = 0;
  
    int i, j;
  
    for (i = 0; i < n; i++)
    {
  
        // Calculate each term
        // of the given formula
        double xi = d[i].x;
        for (j = 0; j < n; j++) 
        {
  
            if (j != i) 
            {
                xi = xi
                    * (y - d[j].y)
                    / (d[i].y - d[j].y);
            }
        }
  
        // Add term to final result
        x += xi;
    }
    return x;
}
  
// Driver Code
public static void main(String[] args)
{
  
    // Sample dataset of 4 points
    // Here we find the value
    // of x when y = 4.5
    Data []d = { new Data( 1.27, 2.3 ),
            new Data( 2.25, 2.95 ),
            new Data( 2.5, 3.5 ),
            new Data( 3.6, 5.1 ) };
  
    // Size of dataset
    int n = 4;
  
    // Sample y value
    double y = 4.5;
  
    // Using the Inverse Interpolation
    // function to find the
    // value of x when y = 4.5
    System.out.printf("Value of x at y = 4.5 : %.5f"
        , inv_interpolate(d, n, y));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 code for solving 
# inverse interpolation
  
# Consider a structure
# to keep each pair of
# x and y together
class Data:
    def __init__(self, x, y):
        self.x = x
        self.y = y
  
# Function to calculate
# the inverse interpolation
def inv_interpolate(d: list, n: int, 
                    y: float) -> float:
  
    # Initialize final x
    x = 0
  
    for i in range(n):
  
        # Calculate each term
        # of the given formula
        xi = d[i].x
        for j in range(n):
            if j != i:
                xi = (xi * (y - d[j].y) / 
                      (d[i].y - d[j].y))
  
        # Add term to final result
        x += xi
    return x
  
# Driver Code
if __name__ == "__main__":
  
    # Sample dataset of 4 points
    # Here we find the value
    # of x when y = 4.5
    d = [Data(1.27, 2.3), 
         Data(2.25, 2.95), 
         Data(2.5, 3.5), 
         Data(3.6, 5.1)]
  
    # Size of dataset
    n = 4
  
    # Sample y value
    y = 4.5
  
    # Using the Inverse Interpolation
    # function to find the
    # value of x when y = 4.5
    print("Value of x at y = 4.5 :", 
           round(inv_interpolate(d, n, y), 5))
  
# This code is contributed by
# sanjeev2552


C#
// C# code for solving inverse interpolation
using System;
  
class GFG
{
  
// Consider a structure to keep 
// each pair of x and y together
class Data
{
    public double x, y;
  
    public Data(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
};
  
// Function to calculate the 
// inverse interpolation
static double inv_interpolate(Data []d, 
                       int n, double y)
{
    // Initialize readonly x
    double x = 0;
  
    int i, j;
  
    for (i = 0; i < n; i++)
    {
  
        // Calculate each term
        // of the given formula
        double xi = d[i].x;
        for (j = 0; j < n; j++) 
        {
            if (j != i) 
            {
                xi = xi * (y - d[j].y) / 
                              (d[i].y - d[j].y);
            }
        }
  
        // Add term to readonly result
        x += xi;
    }
    return x;
}
  
// Driver Code
public static void Main(String[] args)
{
  
    // Sample dataset of 4 points
    // Here we find the value
    // of x when y = 4.5
    Data []d = {new Data(1.27, 2.3),
                new Data(2.25, 2.95),
                new Data(2.5, 3.5),
                new Data(3.6, 5.1)};
  
    // Size of dataset
    int n = 4;
  
    // Sample y value
    double y = 4.5;
  
    // Using the Inverse Interpolation
    // function to find the
    // value of x when y = 4.5
    Console.Write("Value of x at y = 4.5 : {0:f5}", 
                         inv_interpolate(d, n, y));
}
}
  
// This code is contributed by Rajput-Ji


输出:
Value of x at y = 4.5 : 2.79501

复杂度:给定解的时间复杂度为O(n ^ 2) ,空间复杂度为O(1)