You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++ Hermite插值程序结果错误,求修正以输出正确系数与点值

Hermite插值程序错误修正求助

问题概述

我编写了一个C++ Hermite插值程序,预期输出插值多项式的系数及指定点的函数值,但运行结果存在错误。

程序代码

#include <iostream>
#include <vector>

using namespace std;

struct DataPoint {
    double x;
    vector<double> values;  
};

void computeDividedDifferences(vector<DataPoint>& points, vector<int>& multiplicity, vector<vector<double>>& divDiff) {
    int n = points.size();
    int totalNodes = 0;
    for (int i = 0; i < n; ++i) {
        totalNodes += multiplicity[i];
    }

    for (int i = 0; i < totalNodes; ++i) {
        for (int j = 0; j < totalNodes; ++j) {
            divDiff[i][j] = 0.0;
        }
    }

    int index = 0;
    for (int i = 0; i < n; ++i) {
        for (int k = 0; k < multiplicity[i]; ++k) {
            divDiff[index][0] = points[i].values[k];
            if (k != 0) {
                divDiff[index][1] = points[i].values[k] / k;
            } else if (index != 0) {
                divDiff[index][1] = (divDiff[index][0] - divDiff[index - 1][0]) / (points[i].x - points[(index - 1) / multiplicity[(index - 1) / multiplicity[i]]].x);
            }
            index++;
        }
    }

    for (int j = 2; j < totalNodes; ++j) {
        for (int i = j; i < totalNodes; ++i) {
            divDiff[i][j] = (divDiff[i][j - 1] - divDiff[i - 1][j - 1]) / (points[i / multiplicity[i / multiplicity[i]]].x - points[(i - j) / multiplicity[(i - j) / multiplicity[i]]].x);
        }
    }
}

double hermitePolynomial(double x, vector<DataPoint>& points, vector<int>& multiplicity, vector<vector<double>>& divDiff) {
    int n = points.size();
    int totalNodes = 0;
    for (int i = 0; i < n; ++i) {
        totalNodes += multiplicity[i];
    }

    double result = divDiff[0][0];
    double product = 1.0;

    for (int i = 1; i < totalNodes; ++i) {
        product *= (x - points[i / multiplicity[i / multiplicity[i]]].x);
        result += divDiff[i][i] * product;
    }

    return result;
}

void printCoefficients(vector<DataPoint>& points, vector<int>& multiplicity, vector<vector<double>>& divDiff) {
    int n = points.size();
    int totalNodes = 0;
    for (int i = 0; i < n; ++i) {
        totalNodes += multiplicity[i];
    }

    cout << "Hermite polynomial coefficients:" << endl;
    for (int i = 0; i < totalNodes; ++i) {
        cout << "a" << i << " = " << divDiff[i][i] << endl;
    }
}

int main() {
    int k;
    cout << "Enter the number of nodes minus one (k): ";
    cin >> k;
    int n = k + 1;

    vector<DataPoint> points(n);
    vector<int> multiplicity(n);

    cout << "Enter the values of interpolation nodes (x[i], where i = 0, ..., k):" << endl;
    for (int i = 0; i < n; ++i) {
        cout << "x[" << i << "] = ";
        cin >> points[i].x;
    }

    cout << "Enter the multiplicities of nodes (m[i], where i = 0, ..., k):" << endl;
    for (int i = 0; i < n; ++i) {
        cout << "m[" << i << "] = ";
        cin >> multiplicity[i];
    }

    for (int i = 0; i < n; ++i) {
        points[i].values.resize(multiplicity[i]);
        cout << "Enter the function values and its derivatives at node " << points[i].x << " (f[i][s], where s = 0, ..., m[i] - 1):" << endl;
        for (int j = 0; j < multiplicity[i]; ++j) {
            cout << "f[" << i << "][" << j << "] = ";
            cin >> points[i].values[j];
        }
    }

    vector<vector<double>> divDiff(2 * n, vector<double>(2 * n));
    computeDividedDifferences(points, multiplicity, divDiff);

    double x;
    cout << "Enter the value of x to calculate the Hermite polynomial: ";
    cin >> x;

    double result = hermitePolynomial(x, points, multiplicity, divDiff);
    cout << "The value of the Hermite polynomial at point " << x << " is: " << result << endl;

    printCoefficients(points, multiplicity, divDiff);

    return 0;
}

测试输入

Enter the number of nodes minus one (k): 2
Enter the values of interpolation nodes (x[i], where i = 0, ..., k):
x[0] = 0
x[1] = 1
x[2] = 2
Enter the multiplicities of nodes (m[i], where i = 0, ..., k):
m[0] = 1
m[1] = 1
m[2] = 2
Enter the function values and its derivatives at node 0 (f[i][s], where s = 0, ..., m[i] - 1):
f[0][0] = 2
Enter the function values and its derivatives at node 1 (f[i][s], where s = 0, ..., m[i] - 1):
f[1][0] = 3
Enter the function values and its derivatives at node 2 (f[i][s], where s = 0, ..., m[i] - 1):
f[2][0] = 4
f[2][1] = 5
Enter the value of x to calculate the Hermite polynomial: 4

错误输出

The value of the Hermite polynomial at point 4 is: 3.79421e+99
Hermite polynomial coefficients:
a0 = 2
a1 = 1
a2 = 0
a3 = 1.58092e+98

预期正确结果

a[0] =  2 
a[1] = -5 
a[2] =  6 
a[3] = -2 
The value of polynomial at  x =  4: 
H(x) =  5.4

我已尝试调试、查阅Hermite插值相关资料,但仍未找到问题所在,希望修正代码以输出正确的系数和点值。


内容的提问来源于stack exchange,提问作者Wiktor Janowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:17:05