资源描述:
《拉格朗日插值与牛顿插值》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、一、目的1.通过本实验加深对拉格朗日插值和牛顿插值法构造过程的理解;2.能对上述两种插值法提出正确的算法描述编程实现。二、内容与设计思想自选插值问题,编制一个程序,分别用拉格朗日插值法和牛顿插值法求解某点的函数近似值。(从课件jsff04.ppt或教材习题中选题)已知y=f(x)的数据表如下,求t=0.63处的函数值z=f(t)。ixiyiixiyi10.100.90483760.570.56552520.150.86070870.700.49658530.250.77880180.850.4274154
2、0.400.67032090.930.39455450.500.606531101.000.367879三、使用环境操作系统:windonsXP软件环境:VC6.0四、核心代码及调试过程拉格朗日插值法:#include#include#include#defineN20doublelagrange(doublex[N],doubley[N],doublet,intn){doubles,ft;inti,j,k,m;ft=0.0;if(n<1)exit(
3、0);if(n==1){ft=y[0];exit(0);}if(n==2){第4页共4页ft=(t-x[1])*y[0]/(x[0]-x[1])+(t-x[0])*y[1]/(x[1]-x[0]);exit(0);}i=0;while((x[i]n-1)m=n-1;for(i=k;i<=m;i++){s=1.0;for(j=k;j<=m;j++)if(j!=i)s=s*(t-x[j])/(x[i]-x[j]);ft=ft
4、+s*y[i];}return(ft);}voidmain(){doublex[N]={0.10,0.15,0.25,0.40,0.50,0.57,0.70,0.85,0.93,1.00};doubley[N]={0.904837,0.860708,0.77801,0.670320,0.6065310,0.565525,0.496585,0.427418,0.394554,0.367879};doublet=0.63,ft;ft=lagrange(x,y,t,10);printf("f(0.63)=%lf
5、",ft);}运行结果:第4页共4页牛顿插值法:#includevoidmain(){doublex[]={0.10,0.15,0.25,0.40,0.50,0.57,0.70,0.85,0.93,1.00};doubley[]={0.904837,0.860708,0.77801,0.670320,0.6065310,0.565525,0.496585,0.427418,0.394554,0.367879};doublec[10][10];doubleb[10];doublet=0
6、.63,ft;intn,i,j,k;for(i=0;i<10;i++)c[i][0]=y[i];for(i=1;i<10;i++)for(j=1;j<=i;j++)c[i][j]=(c[i][j-1]-c[i-1][j-1])/(x[i]-x[i-j]);b[9]=c[9][9];for(k=9;k>=1;k--)b[k-1]=c[k-1][k-1]+b[k]*(t-x[k-1]);ft=b[0];printf("f(t=0.63)=%lf",ft);}运行结果:总结第4页共4页1.通过本实验加深了对
7、拉格朗日插值和牛顿插值法构造过程的理解。2.通过编程能对上述两种插值法提出正确的算法。六、附录第4页共4页