In this tutorial, we will explore Bessel's Interpolation in C++ including its equation, step-by-step process, and coding demonstration.
Interpolation:
One form of estimation technique for values that are not known and lie between known values is interpolation. Interpolation involves the creation of additional data points based on an existing set of discrete data points.
One benefit of interpolation is its potential to lower computational costs. Interpolation is favored in cases where the computation of a specific value using a complex or costly formula is impractical. The original function is typically employed to compute a limited set of data points, while interpolation can be applied to approximate the remaining data points. Although not perfectly accurate, these estimates are generally very close to the actual values.
In this scenario, the straightforwardness and reduced computational expenses effectively balance out the loss in interpolation accuracy.
Bessels Interpolation:
Bessels Interpolation is employed to approximate y=f(x) for an input (x) in close proximity to the midpoint of provided values. It proves beneficial particularly with an equal count of evenly spaced inputs. To perform the interpolation, it leverages the average of Gauss forward formulas for an even quantity of inputs alongside the Gauss third formula.
Bessels Interpolation Formula:
It has the following formula:
Process:
Step 1
[taking inputs from the user]
Read x [the number of elements]
for i = 0 to x - 1 repeat
Read a[i]
[End of 'for' loop]
for i = 0 to n - 1
Read b[i][0]
Read v
Step 2:
[Bessel's Interpolation]
for x = 1 to m - 1
for y = 0 to m - i - 1
Set b[y][x] <- b[y + 1][x- 1] - b[y][x -1]
[End of 'for' loop]
[End of 'for' loop]
for I = 0 to m- 1
for j = 0 to m - i - 1
print b[i][j]
[End of 'for' loop]
Move to the next line
[End of 'for' loop]
Set sum <- (b[2] [0] + b[3][0]) / 2
if n Mod 2 != 0 then
Set k<-n/2
else
Set k + n / 2 - 1
[End of 'if']
Setu u <- (v - x[k]) / (x[1] - x[0])
for i = 1 to n - 1 repeat
if i Mod 2!=0 then
Set sum <- sum + ((u - 0.5) * cal_u(u, i - 1) * y[k][i]) / factorial(i)
else
Set sum <- sum+(cal_u(u, i)* (y[k][i] + y[-- k][i]) /(factoria1(i)*2))
[End of 'if']
[End of 'for' loop]
Print v, sum
[End of Bessel's interpolation]
Step 3:
[function cal_u]
if n = 0 then
return 1
Set tmp <- u
for i = 1 to n / 2 repeat
set tmp <- tmp*(u - i)
for i = 1 to(n / 2) - 1 repeat
Set tmp<- tmp * (u + i)
return tmp
[End of function 'cal_u']
Step 4:
[function 'factorial']
Set fact <- 1
Set i <- 2
for i = 2 to n repeat
Set fact <- fact * i
return fact
[End of function 'factorial']
Example:
Let's consider a scenario to demonstrate Bessel's Interpolation using C++.
#include<iostream>
using namespace std;
class Bessel {
private:
float x[10], y[10][10], u, v, sum;
int k, n;
public:
float cal_u(float u, int n);
int factorial(int n);
void accept();
void bessels_interpolation();
};
float Bessel::cal_u(float u, int n) {
float temp = 1;
int i;
if (n == 0)
return 1;
temp = u;
for (i = 1; i <= n / 2; i++)
temp *= (u - i);
for (i = 1; i < n / 2; i++)
temp *= (u + i);
return temp;
}
int Bessel::factorial(int n) {
int fact = 1, i;
for (i = 2; i <= n; i++)
fact *= i;
return fact;
}
void Bessel::accept() {
int i;
cout << "Enter the number of elements: ";
cin >> n;
cout << "Enter the values of x: ";
for (i = 0; i < n; i++)
cin >> x[i];
cout << "\nEnter the value of y: ";
for (i = 0; i < n; i++)
cin >> y[i][0];
cout << "Enter the value of Interpolation: ";
cin >> v;
}
void Bessel::bessels_interpolation() {
int i, j;
for (i = 1; i < n; i++) {
for (j = 0; j < n - i; j++)
y[j][i] = y[j + 1][i - 1] - y[j][i - 1];
}
sum = (y[2][0] + y[3][0]) / 2;
if (n % 2 != 0)
k = n / 2;
else
k = n / 2 - 1;
u = (v - x[k]) / (x[1] - x[0]);
for (i = 1; i < n; i++) {
if (i % 2 != 0)
sum += ((u - 0.5) * cal_u(u, i - 1) * y[k][i] / factorial(i));
else
sum += (cal_u(u, i - 1) * (y[k][i] + y[--k][i]) / (factorial(i) * 2));
}
cout << "Value at " << v << ": " << sum;
}
int main() {
Bessel b;
b.accept();
b.bessels_interpolation();
return 0;
}
Output: