资源描述:
《数值分析编程》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、第二章方程求根二分法解方程/(兀)=x3-x-l=0在区间[1,1.5]内的根。程序如下:#include"math.h"floatErfenfa(floatm,floatn){floatxO,x1,x2,fx0,fx1,fx2;do{xl=m;x2=n;fxl=xl*xl*xl-xl-l;fx2=x2*x2*x2-x2-1;)while(fx1*fx2>0);do{xO二(xl+x2)/2;fxO=xO*xO*xO-xO-1;if((fxO*fx1)<0){x2=x0;fx2=fx0;)else{x1=
2、x0;fxl=fxO;while(fabs(fx0)>=0.0001);returnxO;floatNewton(floatk){floatf,df,x,xO;x=k;do{xO=x;f=xO*xO*xO-xO-1;df=3*xO*xO-l;x=xO-f/df;}while((fabs(x-x0))>=0.0001);returnxO;floatXianjie(floatz,floats)floatxO,x1,fx1,fxO,x,fx;xO=z;xl=s;fxO=xO*xO*xO-xO-l;fxl=xl*
3、xl*xl-xl-l;do{x=(xO*fx1-xl*fxO)/(fx1-fxO);fx=x*x*x・xJ;if(fx*fx0>0){fxO=fx;xO=x;}elsexl=x;}while(fabs(fx)>=0.0001);returnx;}main(){floata,b,c,fl,f2,f3;a=Erfenfa(1.0,1.5);b=Newton(1.5);c=Xianjie(1.0,1.5);printf(nu);printf(nErfenfax=%fa);printf("
4、nNewtonx=%f',b);printf(HXianjiex=%f”,c);fl=a*a*a-a-l;f2=b*b*b-b-l;f3=c*c*c-c-l;printf(uErfenfaf=%ffl);printf(nNewtonprintf("Xianjief=%f',f3);}第三章解线性方程组的直接法GUASS消去法程序如下:#include#include#definen3voidprint(doublea[n][n+l]);voidgau
5、ss(doublea[n][n+l],doublex[n]){inti,j,k;doubletemp,s,l;for(i=0;ifabs(a[k][i]))k二j;}〃换行if(k!=i)for(j二i;jv二n;j++){temp=a[i][j];a[i]U]=a[k]U];a[k][j]=temp;)〃消元for(j=i+l;j6、jJliJ/a[iJ[i];for
7、(k=0;k=0;i—){s=().O;for(j=i;j8、%fr,a[i]
9、j]);printf(uu);}}voidmain(){intij;doublea[n][n+l]={{3,-l,4,7},{-l,2,-2,-1},{2,-3,-2,0}},x[n]={1.0,2.0,3.0};gauss(a,x);printf(*方程组的解为:”);for(i=0;i10、intn.doubleeps,intCNT){double*y=newdouble[n];for(inti=0;ieps&&count