# How to implement Lagrangian/Polynomial Interpolation for my C++ Code?

I have a working C++ function:

/// Structure for surface nodes
// Each node has a current x- and y-position and a reference x- and y-position.
struct nstruct {
/// Constructor
node_struct() {
x = 0;
y = 0;
x_ref = 0;
y_ref = 0;
Ax = 0;
Ay = 0;
}
/// Elements
double x; // current x-position
double y; // current y-position
double x_ref; // reference x-position
double y_ref; // reference y-position
double Ax; // node  (x-component)
double Ay; // node  (y-component)
};
/// Structure for object
struct pstruct {
/// Constructor
pstruct() {
num_nodes = pnodes;
center.x;
center.y;
center.x_ref;
center.y_ref;;
node = new node_struct[num_nodes];
}
/// Elements
int num_nodes; // number of surface nodes
node_struct center; // center node
node_struct *node; // list of nodes
};

//
void Function(pstruct particle) {

for (int X = 0; X < Nx; ++X) {
for (int Y = 0; Y < Ny; ++Y) {
Ax[X][Y] = 0;
Ay[X][Y] = 0;
}
}

for (int n = 0; n < particle.num_nodes; ++n) {
int xStart = static_cast <int> (particle.node[n].x - 3.0);
int xEnd = static_cast <int> (particle.node[n].x + 3.0);
int yStart = static_cast <int> (particle.node[n].y - 3.0);
int yEnd = static_cast <int> (particle.node[n].y + 3.0);
for (int X = xStart; X < xEnd; ++X) {
for (int Y = yStart; Y <= yEnd; ++Y) {
**// here I want to use Lagrangian Interpolation**
const double xDistance = X - particle.node[n].x;
const double yDistance = Y - particle.node[n].y;
const double delta = dirac_4(xDistance, yDistance);
// dirac_4 : is Dirac Delta function
Ax[X][Y] += (particle.node[n].Ax * delta);
Ay[X][Y] += (particle.node[n].Ay * delta);
}
}
}
return;
}


Its is an immersed boundary problem. I have Lagrangian points over an object (circular, or any shape), corresponding acceleration, etc. What I want to do, is to interpolate flow variables over Euler mesh using information of Lagrangian points (defined in pstruct and nstruct). I would like to use Lagrangian Interpolation instead of dirac_4 function to compute Ax and Ay. How I will implement Lagrangian/Polynomial interpolation in such a situation?

Regards

• I am pretty sure a lot of contributors can help you with this. However, probably no one will, unless you give some context. Posting a code and asking for a solution is not a question. Commented Mar 28, 2016 at 12:28
• Also, the code has no documentation, does not include a declaration or definition of the pstruct or dirac_4 functions, not basically anything else. Not much anyone can help you here without more context. Commented Mar 28, 2016 at 14:33
• @WolfgangBangerth, I edited my original post with some more explanation. Hope, now everyone can understand my problem better. Commented Mar 29, 2016 at 1:36
• I still have no idea what the dirac_4 function is. Mathematically speaking, the Dirac function is zero everywhere except for one point where it is infinite. That's clearly not a useful definition for a computer code. Commented Mar 29, 2016 at 11:32
• Refer to the attached link for the dirac delta function Eq6.28. Dirac Delta function is not problem here, it is doing the same work here as an interpolant. I want to replace this dirac_4 function with Lagrangian Interpolation. Commented Mar 30, 2016 at 12:55

$$f(x,y) = \sum_i^N f_i L_i(x,y)$$
$$L_i(x,y) = \prod_{j,j\neq i}^N \frac{(x-x_j)(y-y_j)}{(x_i-x_j)(y_i-y_j)}$$
Based on the above formulas, you should be able to program this up. The thing to note is that when $N$ is large, you end up with high order polynomial interpolants, which can result in high errors between data points. I would consider another approach unless you are interpolating using a few data points or you are certain this sort of fit would work well in your situation.