c++ - 拉盖尔插值算法,我的实现有问题

标签 c++ math polynomial-math

这是一个我已经苦苦挣扎了一个星期的问题,回来只是为了在浪费时间后放弃......

我应该找到以下拉盖尔多项式的系数:

P0(x) = 1

P1(x) = 1 - x

Pn(x) = ((2n - 1 - x) / n) * P(n-1) - ((n - 1) / n) * P(n-2)

我认为我的实现存在错误,因为出于某种原因,我得到的系数似乎太大了。这是该程序生成的输出:

a1 = -190.234
a2 = -295.833
a3 = 378.283
a4 = -939.537
a5 = 774.861
a6 = -400.612

代码说明(如下所示):

如果将代码向下滚动到我声明数组的部分,您会发现给定的 x 和 y。

函数 polynomial 只是用特定 x 的所述多项式的值填充一个数组。这是一个递归函数。我相信它运行良好,因为我已经检查了输出值。

gauss 函数通过对输出数组执行高斯消元来找到系数。我认为这是问题开始的地方。我想知道,如果这段代码有错误或者我的 veryfying 结果方法不好?我正在尝试这样验证它们:

-190.234 * 1.5 ^ 5 - 295.833 * 1.5 ^ 4 ... - 400.612 = -3017,817625 =/= 2

代码:

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

double polynomial(int i, int j, double **tab)
{
    double n = i;
    double **array = tab;
    double x = array[j][0];

    if (i == 0) {
        return 1;
    } else if (i == 1) {
        return 1 - x;
    } else {
        double minusone = polynomial(i - 1, j, array);
        double minustwo = polynomial(i - 2, j, array);
        double result = (((2.0 * n) - 1 - x) / n) * minusone - ((n - 1.0) / n) * minustwo;
        return result;
    }
}

int gauss(int n, double tab[6][7], double results[7])
{
    double multiplier, divider;

    for (int m = 0; m <= n; m++)
    {
        for (int i = m + 1; i <= n; i++)
        {
            multiplier = tab[i][m]; 
            divider = tab[m][m];

            if (divider == 0) {
                return 1;
            }

            for (int j = m; j <= n; j++)
            {
                if (i == n) {
                    break;
                }

                tab[i][j] = (tab[m][j] * multiplier / divider) - tab[i][j];
            }

            for (int j = m; j <= n; j++) {
                tab[i - 1][j] = tab[i - 1][j] / divider;
            }
        }
    }

    double s = 0;
    results[n - 1] = tab[n - 1][n];
    int y = 0;
    for (int i = n-2; i >= 0; i--)
    {
        s = 0;
        y++;
        for (int x = 0; x < n; x++)
        {
            s = s + (tab[i][n - 1 - x] * results[n-(x + 1)]); 

            if (y == x + 1) { 
                break;
            }
        }
        results[i] = tab[i][n] - s;
    }

}


int _tmain(int argc, _TCHAR* argv[])
{
    int num;
    double **array;

    array = new double*[5];
    for (int i = 0; i <= 5; i++)
    {
        array[i] = new double[2];
    }
                         //i         0      1       2       3       4       5
    array[0][0] = 1.5;  //xi         1.5    2       2.5     3.5     3.8     4.1
    array[0][1] = 2;    //yi         2      5       -1      0.5     3       7
    array[1][0] = 2;
    array[1][1] = 5;
    array[2][0] = 2.5;
    array[2][1] = -1;
    array[3][0] = 3.5;
    array[3][1] = 0.5;
    array[4][0] = 3.8;
    array[4][1] = 3;
    array[5][0] = 4.1;
    array[5][1] = 7;

    double W[6][7]; //n + 1

    for (int i = 0; i <= 5; i++)
    {
        for (int j = 0; j <= 5; j++)
        {
            W[i][j] = polynomial(j, i, array);
        }
        W[i][6] = array[i][1];
    }

    for (int i = 0; i <= 5; i++)
    {
        for (int j = 0; j <= 6; j++)
        {
            cout << W[i][j] << "\t";
        }
        cout << endl;
    }

    double results[6];
    gauss(6, W, results);

    for (int i = 0; i < 6; i++) {
        cout << "a" << i + 1 << " = " << results[i] << endl;
    }

    _getch();
    return 0;
}

最佳答案

我认为您对递归多项式生成的解释要么需要修改,要么对我来说有点太聪明了。

给定 P[0][5] = {1,0,0,0,0,...}; P[1][5]={1,-1,0,0,0,...};
那么 P[2] 就是 a*P[0] + convolution(P[1], { c, d });
其中 a = -((n - 1)/n) c = (2n - 1)/n 和 d= - 1/n

这可以概括为:P[n] == a*P[n-2] + conv(P[n-1], { c,d }); 在每一步中,都涉及到与 (c + d*x) 的多项式乘法,它将度数增加一(仅增加一...)并添加到与标量 a 相乘的 P[n-1]。

那么插值因子 x 很可能在 [0..1] 范围内。

(卷积意味着,你应该实现多项式乘法,幸运的是这很容易...)

              [a,b,c,d]
                * [e,f]
        ------------------
           af,bf,cf,df  +
        ae,be,ce,de, 0  +
--------------------------
 (= coefficients of the final polynomial)

关于c++ - 拉盖尔插值算法,我的实现有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13383688/

相关文章:

c++ - 在一组 shared_ptr<QString> 中搜索

将度数转换为弧度以用于 C 中的三角函数

php - 在循环中查找素数的公式

matlab - 如何在 MATLAB 中使用 fzero() 求解多项式方程?

c++ - 无论您新建和删除多少次,指针都保证保持不变吗?

c# - 面向 .NET Framework 4.0

c++ - boolean : Incorrect output of cout not 0 or 1

c# - 在 C# 中制作计时器 - 秒到分钟到小时等

c - 拟合多项式趋势线的好包

pseudocode - 用于计算多项式逆的 NTRU 伪代码