如何为我的 C++ 代码实现拉格朗日/多项式插值?

计算科学 C++ 插值
2021-12-25 06:21:12

我有一个有效的 C++ 函数:

/// 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;
}

它是一个沉浸式边界问题。我在对象(圆形或任何形状)、相应的加速度等上有拉格朗日点。我想做的是使用拉格朗日点的信息(在 pstruct 和 nstruct 中定义)在欧拉网格上插值流变量。我想使用拉格朗日插值而不是 dirac_4 函数来计算 Ax 和 Ay。在这种情况下,我将如何实现拉格朗日/多项式插值?

问候

1个回答

因此,二维中的一般拉格朗日插值可以写成如下:

f(x,y)=iNfiLi(x,y)

在哪里

Li(x,y)=j,jiN(xxj)(yyj)(xixj)(yiyj)

根据上述公式,您应该能够对其进行编程。需要注意的是,当N很大,您最终会得到高阶多项式插值,这可能会导致数据点之间的误差很大。我会考虑另一种方法,除非您使用一些数据点进行插值,或者您确定这种拟合在您的情况下会很好地工作。