欢迎来到天天文库
浏览记录
ID:18217247
大小:1.32 MB
页数:39页
时间:2018-09-15
《数值方法上机作业》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、数值计算方法上机作业(C++版)2008-12-0138第二章插值法上机作业一、编制通用程序对n+1个节点xi及yi=f(xi)(i=1,……,n)(1)n次拉格朗日插值计算公式Ln(x);(2)n次牛顿向前插值计算公式;(3)n次牛顿向后插值计算公式。定义头文件:interpolate.h#ifndefINTERPOTATE_H_H#defineINTERPOTATE_H_Husingnamespacestd;constintZero=1e-6;boolLagrangeInterpolate(constintn,double*x,do
2、uble*y,doubleintervalue,double*result_x);boolNewtonFront(constintn,double*x,double*y,doubleintervalue,double*result_x);boolNewtonBack(constintn,double*x,double*y,doubleintervalue,double*result_x);#endif1.拉格朗日插值子程序流程图及程序:#include#include#include#
3、include"Interpolate.h"usingnamespacestd;boolLagrangeInterpolate(constintn,double*x,double*y,doubleintervalue,double*result_x){inti,j,k;doubleVandermonde=1.0;*result_x=0.0;for(k=0;k<=n;++k){Vandermonde=1.0;for(i=0;i<=n;++i){if(i!=k){Vandermonde=((intervalue-x[i])/(x[k]-x[i
4、]))*Vandermonde;}}*result_x=*result_x+Vandermonde*y[k];}cout<<"InterpolateatX="<5、ios::showpoint)<>"<#include#include#include"I6、nterpolate.h"boolNewtonFront(constintn,double*x,double*y,doubleintervalue,double*result_x){inti,j;intbegin=0;intN=n;doublet=0.0;doublemult_t=1.0;//=====存放t多项式乘积,阶乘doubletemp1=0.0;doubletemp2=0.0;//=========计算出插值的X0,确定插值次数N==========//38for(i=0;i<=n;++i){if(intervalue-x[i]7、<0){begin=i-1;N=n-begin;t=(intervalue-x[begin])/(x[1]-x[0]);break;}elseif(fabs(intervalue-x[i])8、ios::showpoint)<>"<9、========================//double*X=newdouble[N+1];double*Y=newdouble[N+1];double*difference_table=newdouble[N+1];for(i=begin;i<=n;++i){X[i-begin]=x[i];Y[i-begin]=y[i];difference_table[i-begin]=y[i];}//==========开始插值计算========================//*result_x=0.0;for(i=0;i<=N;++10、i){//==============计算差分表=================//temp2=difference_table[i];for(j=i+1;j<=N;++j){temp1=d
5、ios::showpoint)<>"<#include#include#include"I
6、nterpolate.h"boolNewtonFront(constintn,double*x,double*y,doubleintervalue,double*result_x){inti,j;intbegin=0;intN=n;doublet=0.0;doublemult_t=1.0;//=====存放t多项式乘积,阶乘doubletemp1=0.0;doubletemp2=0.0;//=========计算出插值的X0,确定插值次数N==========//38for(i=0;i<=n;++i){if(intervalue-x[i]
7、<0){begin=i-1;N=n-begin;t=(intervalue-x[begin])/(x[1]-x[0]);break;}elseif(fabs(intervalue-x[i])8、ios::showpoint)<>"<9、========================//double*X=newdouble[N+1];double*Y=newdouble[N+1];double*difference_table=newdouble[N+1];for(i=begin;i<=n;++i){X[i-begin]=x[i];Y[i-begin]=y[i];difference_table[i-begin]=y[i];}//==========开始插值计算========================//*result_x=0.0;for(i=0;i<=N;++10、i){//==============计算差分表=================//temp2=difference_table[i];for(j=i+1;j<=N;++j){temp1=d
8、ios::showpoint)<>"<9、========================//double*X=newdouble[N+1];double*Y=newdouble[N+1];double*difference_table=newdouble[N+1];for(i=begin;i<=n;++i){X[i-begin]=x[i];Y[i-begin]=y[i];difference_table[i-begin]=y[i];}//==========开始插值计算========================//*result_x=0.0;for(i=0;i<=N;++10、i){//==============计算差分表=================//temp2=difference_table[i];for(j=i+1;j<=N;++j){temp1=d
9、========================//double*X=newdouble[N+1];double*Y=newdouble[N+1];double*difference_table=newdouble[N+1];for(i=begin;i<=n;++i){X[i-begin]=x[i];Y[i-begin]=y[i];difference_table[i-begin]=y[i];}//==========开始插值计算========================//*result_x=0.0;for(i=0;i<=N;++
10、i){//==============计算差分表=================//temp2=difference_table[i];for(j=i+1;j<=N;++j){temp1=d
此文档下载收益归作者所有