//拉格朗日插值多项式的算法实现
#include
#include
#define Length 100
using namespace std;
double Lagrange_Interpolation( int n , double * x ,double * y,double xi) {
double fx=0.0;
for (int i = 0; i <= n; i++) {
double l = 1.0;
for (int j = 0; j <= n; j++) {
if (j != i) {
l *= (xi - x[j]) / (x[i] - x[j]);
}
}
fx += l * y[i];
}
return fx;
}
int main() {
int n;
double x[Length], y[Length], xi;
cout << “请输入拉格朗日差值多项式次数:” << endl;
cin >> n;
cout << “------录取数据------” << endl;
cout << endl;
for (int i = 0; i <= n; i++) {
cout << “x[” << i << “]=”;
cin >> x[i];
cout << “y[” << i << “]=”;
cin >> y[i];
cout << endl;
}
cout << “------录入完毕------” << endl;
cout << “请输入您要求的x的值:”;
cin >> xi;
cout << “拉格朗日插值多项式f(x)=” << setprecision(7)<< Lagrange_Interpolation(n, x, y, xi) << endl;
system(“pause”);
return 0;
}
拉格朗日插值多项式的算法实现
于 2020-11-29 16:13:47 首次发布