牛顿插值法及其C++实现

时间:2023-03-08 18:07:23

h1 { margin-bottom: 0.21cm }
h1.western { font-family: "Liberation Sans", sans-serif; font-size: 18pt }
h1.cjk { font-family: "Noto Sans CJK SC Regular"; font-size: 18pt }
h1.ctl { font-family: "Noto Sans CJK SC Regular"; font-size: 18pt }
p { margin-bottom: 0.25cm; line-height: 120% }

牛顿插值法

一、背景引入

相信朋友们,开了拉格朗日插值法后会被数学家的思维所折服,但是我想说有了拉格朗日插值法还不够,因为我们每次增加一个点都得重算所有插值基底函数,这样会增加计算量,下面我们引入牛顿插值法,这种插值法,添加一个插值结点我们只要做很小的变动便可以得到新的插值多项式。

二、理论推导

-均差的定义:

牛顿插值法及其C++实现

(一阶均差)

二阶均差为一阶均差再求均差。(显然是递推的)

一般地,函数f 的k阶均差定义为:

牛顿插值法及其C++实现

由均差的性质可以推导出:

k+1阶均差:

牛顿插值法及其C++实现

p { margin-bottom: 0.25cm; line-height: 120% }

(具体性质看:《数值分析:第5版》 page:30)

由均差的递推性,我们可以用以下表来求:

牛顿插值法及其C++实现

求表的公式:

table[i][j] = (table[i - 1][j] - table[i - 1][j - 1]) / (x[j] - x[j - i]);

p { margin-bottom: 0.25cm; line-height: 120% }

牛顿插值法及其C++实现

其中P(x) 为插值多项式,而R(x) 为插值余项。

所以p(x):

(由于图片问题此处P(x) 同N(x))

牛顿插值法及其C++实现

p { margin-bottom: 0.25cm; line-height: 120% }

三、代码实现

由以上推导可知,求牛顿插值多项式子主要就是求均差。

均差可由上表递推求得:

求表的公式:

table[i][j] = (table[i - 1][j] - table[i - 1][j - 1]) / (x[j] - x[j - i]);

#include <iostream>
using namespace std;
#include <vector>
inline double newton_solution(double x[], double y[], int n, double num, int newton_time)
{
    vector<vector<);
    ; i <= n; i++) {
        table[i].resize(n + );
    }
    ; i <= n; i++) table[][i] = y[i];
    ; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            table[i][j] = (table[i - ][j] - table[i - ][j - ]) / (x[j] - x[j - i]);
        }
    }

    double res = 0.0;
    ; i <= newton_time; i++) {
        double temp = table[i][i];
        ; j < i; j++) {
            temp *= num - x[j];
        }
        res += temp;
    }

    return res;

}
int main(int argc, char const *argv[])
{
    ;
    cout << "插值节点个数-1:";
    cin >> n;
    ], y[n + ];
    cout << "\n请输入x[i]:";
    ; i <= n; i++) {
        cin >> x[i];
    }
    cout << "\n请输入y[i]:";
    ; i <= n; i++) {
        cin >> y[i];
    }
    ;
    cout << "\n请输入要求的点的x:";
    cin >> num;
    cout << "\n请输入所求的插值多项式次数:";
    ;
    cin >> newton_time;
    cout << newton_solution(x, y, n, num, newton_time) << endl;
    ;

牛顿插值法及其C++实现