#include<iostream>
#include<bits/stdc++.h>
using namespace std;
float m[100][100];
float Newton(float x[],float y[],int n,float t)
{
int r=1;
memset(m,0.0,sizeof(m));
float value=y[0];
for(int i=0;i<n;i++)
m[i][0]=y[i];
for(int j=1;j<n;j++)
{
for(int i=j;i<n;i++)
{
m[i][j]=(m[i][j-1]-m[i-1][j-1])/(x[i]-x[i-r]);
r=r+1;
}
}
float w=0.0;
for(int i=1;i<n;i++)
{
w=w*(t-x[i-1]);
value=value+m[i][i]*w;
}
return value;
}
int main()
{
float x[1000],y[1000];
int n;
cout<<"输入插值节点的个数"<<endl;
cin>>n;
cout<<"接下来输入这些插值节点"<<endl;
for(int i=0; i<n; i++)
{
cin>>x[i]>>y[i];
}
float t;
cout<<"请输入待求解的插值节点的t值"<<endl;
cin>>t;
float result=Newton(x,y,n,t);
cout<<"输出差值矩阵"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
cout<<"输出拉格朗日的值 "<<result<<endl;
return 0;
}
6
0.0 0.39894
0.1 0.39695
0.195 0.39142
0.3 0.38138
0.401 0.36812
0.5 0.35206
0.15
来源:CSDN
作者:啦啦啦mmm
链接:https://blog.csdn.net/qq_45593796/article/details/103535018