DemoPublic/Algorithm/DSP/LagrangeInterpolation.c

53 lines
1.5 KiB
C
Raw Normal View History

static double domega(const double* xn, const unsigned int n, const unsigned int k, const double x);
/**
* @brief
* https://blacktea.vip.cpolar.top/OrgLion-Writes/NotePublic/src/branch/master/Algorithm/DSP/拉格朗日插值.md
*
* @param xn x
* @param yn y
* @param n
* @param x x
* @return double y
*/
double Lagrange(const double* xn, const double* yn, const unsigned int n, const double x) {
unsigned int i;
double l = 0.0;
for (i=0; i<n; i++)
l += yn[i]*domega(xn, n, i, x);
return l;
}
static double domega(const double* xn, const unsigned int n, const unsigned int k, const double x) {
unsigned int i;
double omega=1.0;
double omega_deri = 1.0;
for (i=0; i<n; i++) {
if (i==k)
continue;
omega *= (x-xn[i]);
omega_deri *= (xn[k] - xn[i]);
}
return omega/omega_deri;
}
/** Demo */
static double Xn[] = {0, 0.3000, 0.6000, 0.9000, 1.2000, 1.5000, 1.8000, 2.1000, 2.4000, 2.7000, 3.0000};
static double Yn[] = {2.0000, 2.3780, 3.9440, 7.3460, 13.2320, 22.2500, 35.0480, 52.2740, 74.5760, 102.6020, 137.0000};
/**
* @brief
*
* @return int
*/
int main(void) {
double out = 0.0;
out = Lagrange(Xn, Yn, 11, 1.5);
return 0;
}