What is Interpolation?
Interpolation is a method of finding new data points within the range of a discrete set of known data points (Source Wiki). In other words interpolation is the technique to estimate the value of a mathematical function, for any intermediate value of the independent variable.
For example, in the given table we’re given 4 set of discrete data points, for an unknown function f(x) :

How to find?
Here we can apply the Lagrange’s interpolation formula to get our solution.
The Lagrange’s Interpolation formula:
If, y = f(x) takes the values y0, y1, … , yn corresponding to x = x0, x1 , … , xn then,

This method is preferred over its counterparts like Newton’s method because it is applicable even for unequally spaced values of x.
We can use interpolation techniques to find an intermediate data point say at x = 3.
Advantages of Lagrange Interpolation:
- This formula is used to find the value of the function even when the arguments are not equally spaced.
- This formula is used to find the value of independent variable x corresponding to a given value of a function.
Disadvantages of Lagrange Interpolation:
- A change of degree in Lagrangian polynomial involves a completely new computation of all the terms.
- For a polynomial of high degree, the formula involves a large number of multiplications which make the process quite slow.
- In the Lagrange Interpolation, the degree of polynomial is chosen at the outset. So it is difficult to find the degree of approximating polynomial which is suitable for given set of tabulated points.
C++
#include<bits/stdc++.h>
using namespace std;
struct Data
{
int x, y;
};
double interpolate(Data f[], int xi, int n)
{
double result = 0;
for ( int i=0; i<n; i++)
{
double term = f[i].y;
for ( int j=0;j<n;j++)
{
if (j!=i)
term = term*(xi - f[j].x)/ double (f[i].x - f[j].x);
}
result += term;
}
return result;
}
int main()
{
Data f[] = {{0,2}, {1,3}, {2,12}, {5,147}};
cout << "Value of f(3) is : " << interpolate(f, 3, 4);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Data
{
int x, y;
public Data( int x, int y)
{
super ();
this .x = x;
this .y = y;
}
};
static double interpolate(Data f[], int xi, int n)
{
double result = 0 ;
for ( int i = 0 ; i < n; i++)
{
double term = f[i].y;
for ( int j = 0 ; j < n; j++)
{
if (j != i)
term = term*(xi - f[j].x) / (f[i].x - f[j].x);
}
result += term;
}
return result;
}
public static void main(String[] args)
{
Data f[] = { new Data( 0 , 2 ), new Data( 1 , 3 ),
new Data( 2 , 12 ), new Data( 5 , 147 )};
System.out.print( "Value of f(3) is : " +
( int )interpolate(f, 3 , 4 ));
}
}
|
Python3
class Data:
def __init__( self , x, y):
self .x = x
self .y = y
def interpolate(f: list , xi: int , n: int ) - > float :
result = 0.0
for i in range (n):
term = f[i].y
for j in range (n):
if j ! = i:
term = term * (xi - f[j].x) / (f[i].x - f[j].x)
result + = term
return result
if __name__ = = "__main__" :
f = [Data( 0 , 2 ), Data( 1 , 3 ), Data( 2 , 12 ), Data( 5 , 147 )]
print ( "Value of f(3) is :" , interpolate(f, 3 , 4 ))
|
C#
using System;
class GFG
{
class Data
{
public int x, y;
public Data( int x, int y)
{
this .x = x;
this .y = y;
}
};
static double interpolate(Data []f,
int xi, int n)
{
double result = 0;
for ( int i = 0; i < n; i++)
{
double term = f[i].y;
for ( int j = 0; j < n; j++)
{
if (j != i)
term = term * (xi - f[j].x) /
(f[i].x - f[j].x);
}
result += term;
}
return result;
}
public static void Main(String[] args)
{
Data []f = { new Data(0, 2),
new Data(1, 3),
new Data(2, 12),
new Data(5, 147)};
Console.Write( "Value of f(3) is : " +
( int )interpolate(f, 3, 4));
}
}
|
Javascript
<script>
class Data
{
constructor(x,y)
{
this .x=x;
this .y=y;
}
}
function interpolate(f,xi,n)
{
let result = 0;
for (let i = 0; i < n; i++)
{
let term = f[i].y;
for (let j = 0; j < n; j++)
{
if (j != i)
term = term*(xi - f[j].x) / (f[i].x - f[j].x);
}
result += term;
}
return result;
}
let f=[ new Data(0, 2), new Data(1, 3),
new Data(2, 12), new Data(5, 147)];
document.write( "Value of f(3) is : " +
interpolate(f, 3, 4));
</script>
|
Output:
Value of f(3) is : 35
Complexity:
The time complexity of the above solution is O(n2) and auxiliary space is O(1).
References:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Nov, 2022
Like Article
Save Article