C++中的拉格朗日插值
在本教程中,我们将编写一个程序来查找拉格朗日插值公式的结果。
您没有为程序编写任何逻辑。只需将公式转换为代码即可。让我们看看代码。
示例
#include输出结果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
结论
如果您对本教程有任何疑问,请在评论部分提及。
热门推荐
10 英文节日祝福语简短唯美
11 祝福语简短唯美老师的话
12 美好的毕业祝福语简短
13 医师考试祝福语毕业简短
14 心理考试祝福语结婚简短
15 小孩生日祝福语简短励志
16 正月十五简短谜语祝福语
17 初四祝福语简短霸气
18 卡片朋友祝福语大全简短