Bessels Interpolation In C++

In this article, we will discuss the Bessel's Interpolation in C++ with their formula, algorithm, and implementation.

Interpolation:

One kind of estimation method for unknown values that fall between known values is interpolation . Interpolation is the process of generating new data points from a discrete set of known data points.

One use case for interpolation is that it may reduce computing expenses. We prefer to use interpolation when the formula (function) needed to determine a given number is too difficult or expensive to compute. The original function is used to calculate a small number of data points; interpolation can be used to estimate the remaining data points. These might not be exact, but they're quite close.

In this case, the simplicity and lower computing cost essentially offset the interpolation error loss.

Bessels Interpolation:

Bessels Interpolation is used to estimate y=f(x) for an argument (x) near the center point of tabulated values. This method is useful when dealing with an even number of equispaced arguments. In order to compute interpolation, the arithmetic mean of Guass forward formulae for an even number of arguments and the Guass third formulae are utilized.

Bessels Interpolation Formula:

It has the following formula:

Process:

Step 1

Example

[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:

Example

[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:

Example

[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:

Example

[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 us take an example to illustrate the Bessel's Interpolation in C++.

Example

#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:

Input Required

This code uses input(). Please provide values below: