Program to implement Inverse Interpolation using Lagrange Formula

Given task is to find the value of x for a given y of an unknown function y = f(x) where values of some points (x, y) pairs are given.

Let, y = f(x) be an unknown function where x in an independent variable.
For different values of x, say x_0, x_1, x_2, ..., x_m ( i.e x_k, k=0, 1, 2, 3...m) values of respective y_0, y_1, y_2, ..., y_m ( i.e y_k = f(x_k), k=0, 1, 2, 3...m) given.

The process of finding the value of the independent variable x for a given value of y lying between two tabulated values with the help of the given set of observation for an unknown function is known as Inverse Interpolation.

This is often used to check whether the correctness of output y for an unknown function f i.e how much argument x for this output y differs from the original input.

The problem of inverse interpolation can be solved using Lagrange’s Formula.

Lagrange’s Formula:
The formula for inverse interpolation is similar to interpolation formula but few changes.

Here to solve the problem of inverse interpolation the places of x and y are interchanged. The formula for inverse interpolation is:

 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

This method can even be used when the points are unequally spaced. Here x is expressed as a function of y.

Examples:

Input: Find the value of x where y = 4.5 and the given points are:

 \begin{tabular}{|l|l|l|} \hline k & x_k & y_k \\ \hline 0 & 1.27 & 2.3  \\ \hline 1 & 2.25 & 2.95 \\ \hline 2 & 2.5  & 3.5  \\ \hline 3 & 3.6  & 5.1  \\ \hline \end{tabular}

Output: 2.79501
Explanation: Here num of data points given = 4 and y = 4.5
So, putting the values of all x and y in the inverse interpolation formula given above we get,
x = \frac{(4.5-2.95)(4.5-3.5)(4.5-5.1)}{2.3-2.95)(2.3-3.5)(2.3-5.1)}\cdot1.27+\frac{(4.5-2.3)(4.5-3.5)(4.5-5.1)}{(2.95-2.3)(2.95-3.5)(2.95-5.1)}\cdot2.25+\frac{(4.5-2.3)(4.5-2.95)(4.5-5.1)}{(3.5-2.3)(3.5-2.95)(3.5-5.1)}\cdot2.5+\frac{(4.5-2.3)(4.5-2.95)(4.5-3.5)}{(5.1-2.3)(5.1-2.95)(5.1-3.5)}\cdot3.6
From here we get,
The value of x = 2.79501 where the value of y = 4.5

Graph:

Algorithm:
Here, data is a list of points consisting of x and y and n is the num of data points.

STEP – 1 : Initialize the final value x = 0
STEP – 2 : FOR i = 1 to n do
    STEP – 3 : Initialize xi = data[i].x
    STEP – 4 : FOR j = 1 to n do
        STEP – 5 : IF i != j do
            STEP – 6 : Multiply xi by ( y – data[j].y ) and divide by ( data[i].y – data[j].y )
        ENDIF
    ENDFOR
    STEP – 7 : Add xi to x
ENDFOR
STEP – 8 : Return final value of x
STEP – 9 : END

Implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for solving inverse interpolation
  
#include <bits/stdc++.h>
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;
}

chevron_right


Output:

Value of x at y = 4.5 : 2.79501

Complexity: The time complexity of the given solution is O(n^2) and space complexity is O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.