数值分析所有代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1实验一:拉格朗日插值多项式命名(源程序.cpp及工作区.dsw):lagrange问题:ix0.40.550.800.91iy0.410750.578150.888111.026521.17520用四次拉格朗日多项式求)5.0(4L的函数近似值//Lagrange.cpp#includestdio.h#includeconio.h#defineN4intcheckvalid(doublex[],intn);voidprintLag(doublex[],doubley[],doublevarx,intn);doubleLagrange(doublex[],doubley[],doublevarx,intn);voidmain(){doublex[N+1]={0.4,0.55,0.8,0.9,1};doubley[N+1]={0.41075,0.57815,0.88811,1.02652,1.17520};doublevarx=0.5;if(checkvalid(x,N)==1){printf(\n\n插值结果:P(%f)=%f\n,varx,Lagrange(x,y,varx,N));}else{printf(结点必须互异);}getch();}intcheckvalid(doublex[],intn){inti,j;for(i=0;in;i++){for(j=i+1;jn+1;j++){if(x[i]==x[j])//若出现两个相同的结点,返回-1{return-1;}}}2return1;}doubleLagrange(doublex[],doubley[],doublevarx,intn){doublefenmu;doublefenzi;doubleresult=0;inti,j;printf(Ln(x)=\n);for(i=0;in+1;i++){fenmu=1;for(j=0;jn+1;j++){if(i!=j){fenmu=fenmu*(x[i]-x[j]);}}printf(\t%f,y[i]/fenmu);fenzi=1;for(j=0;jn+1;j++){if(i!=j){printf(*(x-%f),x[j]);fenzi=fenzi*(varx-x[j]);}}if(i!=n){printf(+\n);}result+=y[i]/fenmu*fenzi;}3returnresult;}运行及结果显示:实验二:牛顿插值多项式命名(源程序.cpp及工作区.dsw):newton_cz问题:ix0.40.550.800.91iy0.410750.578150.888111.026521.17520用牛顿插值多项式求)5.0(4N//newton_cz.cpp#includestdio.h#includeiostream.h#includeconio.h#defineN4intcheckvaild(doublex[],intn){inti,j;for(i=0;in+1;i++){for(j=i+1;jn+1;j++)if(x[i]==x[j])return-1;}return1;}voidchashang(doublex[],doubley[],doublef[][N+1]){inti,j,t=0;for(i=0;iN+1;i++){f[i][0]=y[i];//f[0][0]=y[0],f[1][0]=y[1];f[2][0]=y[2];f[3][0]=y[3];f[4][0]=y[4]}for(j=1;jN+1;j++)//阶数j{for(i=0;iN-j+1;i++)//差商个数if[i][j]=(f[i+1][j-1]-f[i][j-1])/(x[t+i+1]-x[i]);//一阶为f[0][1]~f[3][1];二阶为f[0][2]~f[2][2]//三阶为f[0][3]~f[1][3];四阶为f[0][4]t++;4}}doublecompvalue(doublet[][N+1],doublex[],doublevarx){inti,j,r=0;doublesum=t[0][0],m[N+1]={sum,1,1,1,1};printf(i\tXi\tF(Xi)\t1阶\t2阶\t\t3阶\t4阶\n);printf(--------------------------------------------------------------------------------);for(i=0;iN+1;i++){r=i;printf(x%d\t%f,i,x[i]);for(j=0;j=i;j++){printf(%f,t[r][j]);r--;}printf(\n);}printf(--------------------------------------------------------------------------------);/**/printf(N(x)=\n);printf(%f\n,t[0][0]);for(i=1;iN+1;i++){for(j=0;ji;j++)m[i]=m[i]*(varx-x[j]);m[i]=t[0][i]*m[i];sum=sum+m[i];}for(i=1;iN+1;i++){printf(+%f*,t[0][i]);for(j=0;ji;j++)printf((x-%f),x[j]);printf(\n);}returnsum;}voidmain(){doublevarx,f[N+1][N+1];doublex[N+1]={0.4,0.55,0.8,0.9,1};doubley[N+1]={0.41075,0.57815,0.88811,1.02652,1.17520};checkvaild(x,N);chashang(x,y,f);varx=0.5;if(checkvaild(x,N)==1){chashang(x,y,f);printf(\n\n牛顿插值结果:P(%f)=%f\n,varx,compvalue(f,x,varx));}elseprintf(输入的插值节点的x值必须互异!);getch();5}运行及结果显示:实验三:自适应梯形公式命名(源程序.cpp及工作区.dsw):autotrap][nT问题:计算10214dxx的近似值,使得误差(EPS)不超过610//autotrap.cpp#includestdio.h#includeconio.h#includemath.h#defineEPS1e-6doublef(doublex){return4/(1+x*x);}doubleAutoTrap(double(*f)(double),doublea,doubleb,doubleeps){doublet2,t1,sum=0.0;inti,k;t1=(b-a)*(f(a)+f(b))/2;printf(T(%4d)=%f\n,1,t1);for(k=1;k11;k++){for(i=1;i=pow(2,k-1);i++)sum+=f(a+(2*i-1)*(b-a)/pow(2,k));t2=t1/2+(b-a)*sum/pow(2,k);printf(T(%4d)=%f\n,(int)pow(2,k),t2);if(fabs(t2-t1)EPS)break;elset1=t2;sum=0.0;}returnsum;}voidmain()6{doubles;s=AutoTrap(f,0.0,1.0,EPS);getch();}运行及结果显示:实验四:龙贝格算法命名(源程序.cpp及工作区.dsw):romberg问题:求10214dxx的近似值,要求误差不超过610//romberg.cpp#includestdio.h#includeconio.h#includemath.h#defineN20#defineEPS1e-6doublef(doublex){return4/(1+x*x);}doubleRomberg(doublea,doubleb,double(*f)(double),doubleeps){doubleT[N][N],p,h;intk=1,i,j,m=0;T[0][0]=(b-a)/2*(f(a)+f(b));do{p=0;h=(b-a)/pow(2,k-1);for(i=1;i=pow(2,k-1);i++)p=p+f(a+(2*i-1)*h/2);T[0][k]=T[0][k-1]/2+p*h/2;for(i=1;i=k;i++){j=k-i;T[i][j]=(pow(4,i)*T[i-1][j+1]-T[i-1][j])/(pow(4,i)-1);}k++;p=fabs(T[k-1][0]-T[k-2][0]);}while(p=EPS);k--;while(m=k){for(i=0;i=m;i++)printf(T(%d,%d)=%f,i,m-i,T[i][m-i]);m++;printf(\n);}returnT[k][0];}voidmain(){7printf(\n\n积分结果=%f,Romberg(0,1,f,EPS));getch();}运行及结果显示:实验五:牛顿切线法问题:求方程01)(3xxxf在5.1x,6.0x附近的根(精度31021)//newton_qxf.cpp#includemath.h#includeconio.h#includestdio.h#defineMaxK1000#defineEPS0.5e-3doublef(doublex){returnx*x*x-x-1;}doublef1(doublex){return3*x*x-1;}intnewton(double(*f)(double),double(*f1)(double),double&x0,doubleeps){doublexk,xk1;intcount=0;printf(k\txk\n);printf(-----------------------\n);xk=x0;printf(%d\t%f\n,count,xk);do{8if((*f1)(xk)==0)return2;xk1=xk-(*f)(xk)/(*f1)(xk);if(fabs(xk-xk1)eps){count++;xk=xk1;printf(%d\t%f\n,count,xk);x0=xk1;return1;}count++;xk=xk1;printf(%d\t%f\n,count,xk);}while(countMaxK);return0;}voidmain(){for(inti=0;i2;i++){doublex=0.6;if(i==1)x=1.5;printf(x0初值为%f:\n,x);if(newton(f,f1,x,EPS)==1){printf(-----------------------\n);printf(therootisx=%f\n\n\n,x);}else{printf(themethodisfail!);}}getch();}9实验六:牛顿下山法命名(源程序.cpp及工作区.dsw):newton_downhill问题:求方程01)(3xxxf在5.1x,6.0x附近的根(精度31021)//newton_downhill.cpp#includestdio.h#includeconio.h#includemath.h#includestdlib.h#defineEt1e-3//下山因子下界#defineE11e-3//根的误差限#defineE21e-3//残量精度doublef(doublex){returnx*x*x-x-1

1 / 27
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功