func1p4.cpp
资源名称:数值分析课程设计.zip [点击查看]
上传用户:zhdd911129
上传日期:2007-05-11
资源大小:722k
文件大小:1k
源码类别:
matlab例程
开发平台:
Matlab
- //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,变化幅度非常的大
- //可见对于该函数,并不适用多项式高次插值