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 [Tex]x_k, k=0, 1, 2, 3…m)   [/Tex]values of respectiveÂ
[Tex]y_k = f(x_k), k=0, 1, 2, 3…m)   [/Tex]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:
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:Â
Â
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,Â
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:Â
Â
C++
// C++ code for solving inverse interpolationÂ
#include <bits/stdc++.h>using namespace std;Â
// Consider a structure// to keep each pair of// x and y togetherstruct 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 Codeint 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 interpolationclass GFG{Â
// Consider a structure// to keep each pair of// x and y togetherstatic class Data{Â Â Â Â double x, y;Â
    public Data(double x, double y)    {        super();        this.x = x;        this.y = y;    }     };Â
// Function to calculate// the inverse interpolationstatic 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 Codepublic 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 togetherclass Data:    def __init__(self, x, y):        self.x = x        self.y = yÂ
# Function to calculate# the inverse interpolationdef 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 Codeif __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 interpolationusing System;Â
class GFG{Â
// Consider a structure to keep // each pair of x and y togetherclass Data{Â Â Â Â public double x, y;Â
    public Data(double x, double y)    {        this.x = x;        this.y = y;    }};Â
// Function to calculate the // inverse interpolationstatic 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 Codepublic 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 |
Javascript
<script>// javascript code for solving inverse interpolation   // Consider a structure    // to keep each pair of    // x and y together     class Data {                 constructor(x , y) {            this.x = x;            this.y = y;        }Â
    };Â
    // Function to calculate    // the inverse interpolation    function inv_interpolate( d , n , y)     {             // Initialize final x        var x = 0;Â
        var i, j;Â
        for (i = 0; i < n; i++) {Â
            // Calculate each term            // of the given formula            var 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             // Sample dataset of 4 points        // Here we find the value        // of x when y = 4.5        var 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        var n = 4;Â
        // Sample y value        var y = 4.5;Â
        // Using the Inverse Interpolation        // function to find the        // value of x when y = 4.5        document.write("Value of x at y = 4.5 : ", inv_interpolate(d, n, y).toFixed(5));Â
// This code is contributed by gauravrajput1 </script> |
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)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




