📄 func1p4.cpp
字号:
//Interpolate,using Langrang Methom
#include <iostream.h>
//Langrange polynomial
double LP(double x,double *ax,double *ay,int n)
{
double sum=0,Tsum=1;
int i,j;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (j!=i)
Tsum=Tsum*(x-ax[j])/(ax[i]-ax[j]);
}
sum=sum+Tsum*ay[i];
Tsum=1;
}
return sum;
}
void main()
{
double ax[]={1,2,3,4,5,6,7,8,9,10,11,12},ay[]={12,234,34,-1,34,2,5,23,34,9,45,23},x1=6.5,x2=11.2;
int n=12;
cout<<"The value at 1st point is: "<<LP(x1,ax,ay,n)<<endl;
cout<<"The value at 2nd point is: "<<LP(x2,ax,ay,n)<<endl;
cin>>n;
}
//运行结果:p(6.5)=-2.5738,p(11.2)=112.379
//与表中原有数据比较,发现其偏离极其之大,Runge现象非常显著
//即使在离插值点靠近的点,p(6.01)=1.7629,p(11.01)=47.7551,变化幅度非常的大
//可见对于该函数,并不适用多项式高次插值
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -