1C语言编程习题第二章习题2-25.用二分法编程求6x4-40x2+9=0的所有实根。#includestdio.h#includemath.h#defineN10000doubleA,B,C;doublef(doublex){return(A*x*x*x*x+B*x*x+C);}voidBM(doublea,doubleb,doubleeps1,doubleeps2){intk;doublex,xe;doublevaluea=f(a);doublevalueb=f(b);if(valuea0&&valueb0||valuea0&&valueb0)return;printf(Findingrootintherange:[%.3lf,%.3lf]\n,a,b);for(k=1;k=N;k++){x=(a+b)/2;xe=(b-a)/2;if(fabs(xe)eps2||fabs(f(x))eps1){printf(Thexvalueis:%g\n,x);printf(f(x)=%g\n\n,f(x));return;}if(f(a)*f(x)0)b=x;elsea=x;}printf(Noconvergence!\n);}2intmain(){doublea,b,eps1,eps2,step,start;printf(PleaseinputA,B,C:\n);scanf(%lf%lf%lf,&A,&B,&C);printf(Pleaseinputa,b,step,eps1,eps2:\n);scanf(%lf%lf%lf%lf%lf,&a,&b,&step,&eps1,&eps2);for(start=a;(start+step)=b;start+=step){doubleleft=start;doubleright=start+step;BM(left,right,eps1,eps2);}return0;}运行:PleaseinputA,B,C:6-409Pleaseinputa,b,step,eps1,eps2:-101011e-51e-5Findingrootintherange:[-3.000,-2.000]Thexvalueis:-2.53643f(x)=-0.00124902Findingrootintherange:[-1.000,0.000]Thexvalueis:-0.482857f(x)=0.00012967Findingrootintherange:[0.000,1.000]Thexvalueis:0.482857f(x)=0.00012967Findingrootintherange:[2.000,3.000]Thexvalueis:2.53643f(x)=-0.00124902有时若把判别语句3if(fabs(xe)eps2||fabs(f(x))eps1)改为if(fabs(xe)eps2&&fabs(f(x))eps1)会提高精度,对同一题运行结果:Findingrootintherange:[-3.000,-2.000]Thexvalueis:-2.53644f(x)=-4.26496e-007Findingrootintherange:[-1.000,0.000]Thexvalueis:-0.482861f(x)=-7.3797e-006Findingrootintherange:[0.000,1.000]Thexvalueis:0.482861f(x)=-7.3797e-006Findingrootintherange:[2.000,3.000]Thexvalueis:2.53644f(x)=-4.26496e-007习题2-35.请用埃特金方法编程求出x=tgx在4.5(弧度)附近的根。#includestdio.h#includemath.h#defineN100#definePI3.1415926voidSM(doublex0,doubleeps){intk;doublex;doublex1,x2;for(k=1;k=N;k++){x1=sin(x0)/cos(x0);x2=sin(x1)/cos(x1);x=x0-(x1-x0)*(x1-x0)/(x2-2*x1+x0);if(fabs(x-x0)eps){4printf(Thexvalueis:%g\n,x);return;}x0=x;}printf(Noconvegence!\n);}intmain(){doubleeps,x0;printf(Pleaseinputeps,x0:\n);scanf(%lf%lf,&eps,&x0);SM(x0,eps);return0;}运行:Pleaseinputeps,x0:1e-54.5Thexvalueis:4.49341习题2-411.请编出用牛顿法求复根的程序,并求出P(z)=z4-3z3+20z2+44z+54=0接近于z0=2.5+4.5i的零点。#includecstdio#includecmath#defineMAX_TIMES1000typedefstruct{doublereal,image;}COMPLEX;COMPLEXAa[5]={{54,0},{44,0},{20,0},{-3,0},{1,0}};COMPLEXBb[4]={{44,0},{40,0},{-9,0},{4,0}};COMPLEXzero={0,0};5doubleeps1=1e-6;doubleeps2=1e-6;COMPLEXmulti(COMPLEXa,COMPLEXb){COMPLEXresult;result.real=a.real*b.real-a.image*b.image;result.image=a.image*b.real+a.real*b.image;returnresult;}COMPLEXDiv(COMPLEXa,COMPLEXb){COMPLEXz3;doubles;s=(b.real*b.real)+(b.image*b.image);z3.real=b.real;z3.image=-b.image;z3=multi(a,z3);z3.real=z3.real/s;z3.image=z3.image/s;returnz3;}COMPLEXadd(COMPLEXa,COMPLEXb){COMPLEXresult;result.real=a.real+b.real;result.image=a.image+b.image;returnresult;}COMPLEXsubtract(COMPLEXa,COMPLEXb){COMPLEXresult;result.real=a.real-b.real;result.image=a.image-b.image;returnresult;}COMPLEXtimes(COMPLEXz,intn){inti;COMPLEXresult={1,0};for(i=0;in;i++)result=multi(result,z);returnresult;}6doubledistance(COMPLEXa,COMPLEXb){returnsqrt((a.real-b.real)*(a.real-b.real)+(a.image-b.image)*(a.image-b.image));}doublecomplex_abs(COMPLEXa){returnsqrt(a.real*a.real+a.image*a.image);}COMPLEXf(COMPLEXx){inti;COMPLEXresult=zero;for(i=0;i5;i++){result=add(result,multi(Aa[i],times(x,i)));}returnresult;}COMPLEXff(COMPLEXx){inti;COMPLEXresult=zero;for(i=0;i4;i++){result=add(result,multi(Bb[i],times(x,i)));}returnresult;}intmain(){COMPLEXz0,z1,result;doublex,y;intk;printf(pleaseinputx,y\n);scanf(%lf%lf,&x,&y);z0.real=x;z0.image=y;for(k=0;kMAX_TIMES;k++){z1=subtract(z0,Div(f(z0),ff(z0)));result=f(z1);if(distance(z0,z1)eps1||complex_abs(result)eps2){printf(Therootis:z=(%.3f+%.3fi),f(z)=(%e+%ei)\n,z1.real,z1.image,result.real,result.image);return0;7}z0=z1;}printf(Noconvergence!\n);return0;}运行:pleaseinputx,y2.54.5Therootis:z=(2.471+4.641i),f(z)=(-1.122705e-007+-1.910245e-007i)习题2-62.请编程用劈因子法求高次方程x4+x3+5x2+4x+4=0的所有复根。#includecstdio#includecmathintmain(){floatb[20],c[20];floatdelta;floateps;intprint=0;floatfru0,fru1,s0,s1,frv0,frv1;floatdeltau,deltav;floatu,v;floata[20];intn,j;floatr0,r1;floatr;inti,k;printf(Pleaseinputmaxexponent:\n);scanf(%d,&n);printf(Pleaseinputepslion:\n);scanf(%f,&eps);printf(Pleaseinputthecoefficient:\n);for(i=0;i=n;i++)scanf(%f,,&a[i]);8for(j=0;j=n;j++)printf(+%.3fX^%d,a[j],n-j);printf(\n);for(k=1;k=2;k++){u=a[n-1]/a[n-2];v=a[n]/a[n-2];if(k==2){u=0;v=4;}while(1){b[0]=a[0];b[1]=a[1]-u*b[0];for(j=2;j=n;j++){b[j]=a[j]-u*b[j-1]-v*b[j-2];}r0=b[n-1];r1=b[n]+u*b[n-1];c[0]=b[0];c[1]=b[1]-u*b[0];for(j=2;j=n-2;j++)c[j]=b[j]-u*c[j-1]-v*c[j-2];s0=c[n-3];s1=c[n-2]+u*c[n-3];fru0=u*s0-s1;fru1=v*s0;frv0=-s0;frv1=-s1;deltau=-(frv1*r0-frv0*r1)/(fru0*frv1-fru1*frv0);deltav=-(fru1*r0-fru0*r1)/(frv0*fru1-frv1*fru0);u=u+deltau;v+=deltav;delta=u*u-4*v;if((fabs(deltau)eps)&&(fabs(deltav)eps)){print=1;printf(foundroots:);r=-u/2;i=(int)sqrt(fabs(delta))/2;if(delta0){9printf(%.3f+%.3fi\t,r,fabs((double)i));printf(%.3f-%.3fi\n,r,fabs((double)i));}else{printf