C++中的拉格朗日插值
在这一教程中,我们将编写一个程序,该程序找到拉格朗日插值法的结果。
你无需编写任何程序逻辑。只需将公式转换为代码即可。让我们看看代码。
示例
#include<bits/stdc++.h>
using namespace std;
struct Data {
int x, y;
};
double interpolate(Data function[], int xi, int n) {
double result = 0;
for (int i = 0; i < n; i++) {
double term = function[i].y;
for (int j = 0; j < n; j++) {
if (j != i) {
term = term * (xi - function[j].x) / double(function[i].x - function[j].x);
}
}
result += term;
}
return result;
}
int main() {
Data function[] = {{0,3}, {1,2}, {6,9}, {10,17}};
cout << interpolate(function, 3, 5) << endl;
return 0;
}输出
如果你运行以上代码,则将获得以下结果。
3
结论
如果你在教程中有什么疑问,请在评论部分中提出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP