c
/**
* @file
* @author jUicE_g2R(qq:3406291309)------------彬(bin-必应)
* 通信与信息专业大二在读
*
* @brief Microsoft 源代码注释语言 SAL
*
* @copyright 2023.10
* @COPYRIGHT 原创技术笔记:转载需获得博主本人同意,且需标明转载源
*
* @language C/C++
*
* @IDE Base on Microsoft Visual Studio 2022
*/
数据
i i i | x i x_i xi | F x i Fx_i Fxi | 一阶 | 二阶 | 三阶 |
---|---|---|---|---|---|
0 | 121 | 11 | |||
1 | 144 | 12 | 1 23 \frac{1}{23} 231 | ||
2 | 169 | 13 | 1 25 \frac{1}{25} 251 | − 1 13800 -\frac{1}{13800} −138001 | |
3 | 196 | 14 | 1 27 \frac{1}{27} 271 | − 1 17550 -\frac{1}{17550} −175501 | ≈ 2.06449 ∗ 1 0 − 7 2.06449*10^{-7} 2.06449∗10−7 |
公式
一阶差商 F [ x i − 1 , x i ] = F x i − 1 − F x i x i − 1 − x i F[x_{i-1},x_i]=\frac{Fx_{i-1}-Fx_i}{x_{i-1}-x_i} F[xi−1,xi]=xi−1−xiFxi−1−Fxi
F [ x 0 , x 1 ) ] = F x 0 − F x 1 x 0 − x 1 = 11 − 12 121 − 144 = − 1 − 23 = 1 23 F[x_0,x_1)]=\frac{Fx_0-Fx_1}{x_0-x_1}=\frac{11-12}{121-144}=\frac{-1}{-23}=\frac{1}{23} F[x0,x1)]=x0−x1Fx0−Fx1=121−14411−12=−23−1=231
二阶差商 F [ x i − 2 , x i − 1 , x i ] = F [ x i − 2 , x i − 1 ] − F [ x i − 1 , x i ] x i − 2 − x i ] F[x_{i-2},x_{i-1},x_i]=\frac{F[x_{i-2},x_{i-1}]-F[x_{i-1},x_i]}{x_{i-2}-x_i]} F[xi−2,xi−1,xi]=xi−2−xi]F[xi−2,xi−1]−F[xi−1,xi]
F [ x 0 , x 1 , x 2 ] = F [ x 0 , x 1 ] − F [ x 1 , x 2 ] x 0 − x 2 = 1 23 − 1 25 121 − 169 = 2 575 − 48 = − 1 13800 F[x_0,x_1,x_2]=\frac{F[x_0,x_1]-F[x_1,x_2]}{x_0-x_2}=\frac{\frac{1}{23}-\frac{1}{25}}{121-169}=\frac{\frac{2}{575}}{-48}=-\frac{1}{13800} F[x0,x1,x2]=x0−x2F[x0,x1]−F[x1,x2]=121−169231−251=−485752=−138001
三阶差商 F [ x i − 3 , x i − 2 , x i − 1 , x i ] = F [ x i − 3 , x i − 2 , x i − 1 ] − F [ x i − 2 , x i − 1 , x i ] x i − 3 − x i ] F[x_{i-3},x_{i-2},x_{i-1},x_i]=\frac{F[x_{i-3},x_{i-2},x_{i-1}]-F[x_{i-2},x_{i-1},x_i]}{x_{i-3}-x_i]} F[xi−3,xi−2,xi−1,xi]=xi−3−xi]F[xi−3,xi−2,xi−1]−F[xi−2,xi−1,xi]
牛顿插值公式
C++代码实现
c
//牛顿插值
#include <bits/stdc++.h>
using namespace std;
#define NUMSIZE 10
double FX[NUMSIZE][2] = { {121,11},{144,12},{169,13},{196,14} }; //第一个存x,第二个存y;fx=√x
bool AddFlag;
void Add(int x, int y, int loc) { FX[loc][0] = x; FX[loc][1] = y; }
class InEquality { //求均差
public:
double ValidDif[NUMSIZE]; //返回公式要用的值double Valid[NUMSIZE];
void RetDev(int xNum) {
_xNum = xNum;
if (!AddFlag) {
for (int i = 1; i < xNum; i++) { //从一阶差商(均差)开始
_order = i;
GetVal();
}
}
else {
_order = xNum;
GetVal();
}
}
private:
int _xNum, _order;
double dif[NUMSIZE][NUMSIZE]; //第一个存阶乘数。第二个存均差
void GetVal() {
int st_i = _order;
if (_order == 1) { //求一阶差商
for (int i = st_i; i < _xNum; i++) {
dif[_order][i] = (FX[i - 1][1] - FX[i][1]) / (FX[i - 1][0] - FX[i][0]); //(Fx_i - Fx_i+1)/(x_i - x_i+1)
}
}
else {
for (int i = st_i; i < _xNum; i++) { //求非一阶差商
dif[_order][i] = (dif[_order - 1][i - 1] - dif[_order - 1][i]) / (FX[i - _order][0] - FX[i][0]);
}
}
ValidDif[_order] = dif[_order][_order];
}
};
class DeltaRide {
public:
double DR[NUMSIZE] = {1};
void RetRes(int xNum, int x) {
_xNum = xNum; _x = x;
GetVal();
}
private:
int _xNum, _x;
void GetVal(void) {
for (int i = 1; i < _xNum; i++) {
DR[i] = DR[i - 1] * (_x - FX[i - 1][0]);
}
}
};
class Newton :public InEquality, public DeltaRide { //继承 两个父类 中 public范围 的 数据以及函数
public:
double RetNWT(int xNum) {
_xNum = xNum;
return Merge();
}
private:
double _Nx;
int _xNum;
double Merge(void){
_Nx = DR[0] * FX[0][1];
for (int i = 1; i < _xNum; i++) {
_Nx += DR[i] * ValidDif[i];
}
return _Nx;
}
};
void Display(int n) {
cout << "====================" << endl;
cout << "编号 x y" << endl;
for (int i = 0; i < n; i++) {
cout << i << " " << FX[i][0] << " " << FX[i][1] << endl;
}
cout << "====================" << endl;
}
int main(int* argc, char* argv[]) {
int n = 4;
AddFlag = false;
Display(n);
int x; cout << "请输入数据:"; cin >> x;
Newton Nx;
Nx.RetDev(n);
Nx.RetRes(n, x);
cout << "√" << x << "=" << Nx.RetNWT(n);
//...AddFlag=true,向数据模型继续补充,使误差更小
return 0;
}
结果展示
分析
这样数据已经非常逼近了,可以继续Add数据,使模型更贴合√x函数曲线,减小计算得到值的误差