见鬼吧,拉格朗日插值法
一、理论证明
笔者看过比较多博文后发现一个比较的推导的博文:http://www.cnblogs.com/ECJTUACM-873284962/p/6833391.html
因而此处就不过多的证明了
主要两个公式:
二、实现(c++)
#include#include using namespace std;inline double coefficient_l(double* x, int k, double num,int n)//x是插值的x向量,k为第几个l,n是插值节点个数{ double res = 1.0; for (int i = 0; i < n; i++) { if (i != k) { res *= (num - x[i]) / (x[k] - x[i]); } } return res;}inline double lagrange(double* x, double* y, double num, int n)/* x,y都是插值的节点*/{ double res = 0.0; for (int i = 0; i < n; i++) { res += y[i] * coefficient_l(x,i,num,n); } return res;}int main(int argc, char const *argv[]){ int n = 0; cout<<"请输入插值结点个数n:"; cin>>n; double* x = new double[n]; double* y = new double[n]; cout<<"请输入n个x:"; for(int i=0;i >x[i]; } cout<<"请输入n+1个y:"; for(int i=0;i >y[i]; } cout<<"请输入要计算的num:"; double num = 0; cin>>num; double res = lagrange(x,y,num,n); cout<<"res:"< <