func1p3.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[]={0.4,0.5,0.6,0.7,0.8},ay[]={-.916291,-.693147,-.510826,-.356675,-.223144},x=0.54;
- int n=5;
- cout<<"The value at that point is: "<<LP(x,ax,ay,n)<<endl;
- cin>>n;
- }
- //运行结果:-0.616143
- //真值:-0.616186;误差:0.000043; 理论误差限(由余项公式):0.001029
- //可见对于该函数y=ln x,多项式插值有着相当好的效果