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