您好,欢迎访问三七文档
一、对分法1、#includemath.hmain(){floata[3]={-3.0,1.0,0.0},b[3]={-2,0,1},c[3];floatf(floatx);inti;for(i=0;i3;i++){do{c[i]=(a[i]+b[i])/2.0;if(f(c[i])==0)exit(0);elseif(f(c[i])*f(a[i])0)b[i]=c[i];elsea[i]=c[i];}while((b[i]-a[i])1e-5);}c[i]=a[i];printf(therootsare:);for(i=0;i3;i++)printf(%f,c[i]);printf(2.000000);}floatf(floatx){floaty;y=x*x*x-2*x*x-4*x-7;return(y);}3、对分部分函数调用(题目要求如2)#includemath.hfloatf(floatx){floaty;y=x*x*x-2*x*x-4*x-7;return(y);}floatf1(floata,floatb){floatc;do{c=(a+b)/2;if(f(c)==0)exit(0);elseif(f(a)*f(c)0)b=c;elsea=c;}while((b-a)1e-5);return(a);}main(){floata=3.0,b=4.0,s;s=f1(a,b);printf(therootis%f,s);}2、用对分法求出方程x3-2x2-4x-7=0在区间【3,4】内的根,精度要求为。#includemath.hmain(){floata=3.0,b=4.0,c;floatf(floatx);do{c=(a+b)/2.0;if(f(c)==0){printf(therootis%f,c);exit(0);/*找到方程的根*/}elseif(f(c)*f(a)0)b=c;elsea=c;}while((b-a)1e-5);printf(therootis%f,a);}floatf(floatx){floaty;y=x*x*x-2*x*x-4*x-7;return(y);}对分法的算法:扫描法的算法:4、对分法和扫描结合求方程x4-5x2+x+2=0的实根的上、下界,实现根的隔离,并用对分法求出所有的实根,精度要求为。此方程的4个实根分别为:root=2.246979root=0.554956root=0.801941root=2.000000#includemath.hfloatf(floatx){floaty;y=x*x*x*x-5*x*x+x+2;returny;}main(){floata,b,c;inti=0;floatx,h=0.001,p[5],q[5],n;scanf(%f%f,&a,&b)x=a;while(xb){if(f(x)*f(x+h)=0){p[i]=x;q[i]=x+h;i=i+1;printf([%f,%f]\n,p[i],q[i]);}x=x+h;n=i;}for(i=0;in;i++){a=p[i];b=q[i];while(b-a1e-5){c=(a+b)/2;if(f(c)==0){printf(%f\n,c);exit(0);}elseif(f(a)*f(c)0)b=c;elsea=c;}printf(%f\n,c)}}二、秦九韶算法使用秦九韶算法计算多项式的值例如计算3x2+2x+1,当x=-1时值为2。main(){floataa[20],y,x;inti,n;printf(inputduoxiangshicishun:\n)scanf(%d,&n);printf(inputduoxiangshidexishu\n);for(i=0;i=n;i++)scanf(%f,&aa[i]);printf(inputx\n);scanf(%f,&x);y=aa[0];for(i=1;i=n;i++)y=y*x+aa[i];printf(pn=%f\n,y);}秦九韶算法三、牛顿法1、编写牛顿法求方程根的通用程序,精度要求为。(x3-x2-2x-3=0初值x0=2)#includemath.hmain(){floatf(floatx);floatfd(floatx);floatx,x0,x1;x1=2.0;do{x0=x1;x1=x0-f(x0)/fd(x0);}while(fabs(x1-x0)1e-5);printf(therootis%f\n,x1);}floatf(floatx){returnx*x*x-x*x-2*x-3;}floatfd(floatx){return3*x*x-2*x-2;}2、用牛顿法求a的立方根,精度要求为0.000005。#includemath.hmain(){doublex0,x1;inta;printf(inputa\n);scanf(%d,&a);if(a==0){printf(a=0\n);exit(0);}x1=a;do{x0=x1;x1=x0-(x*x*x-a)/(3*x*x);}while(fabs(x1-x0)=0.5e-5);printf(therootis%f\n,x1);printf(root=%f\n,x1);}3、用牛顿法求方程x-(x-exp(-x)=0)在1附近的根,精度要求为0.00001。#includemath.hmain(){floatx0,x1;x1=1.0;do{x0=x1;x1=x0-(x-exp(-x))/(1+exp(-x));}while(fabs(x1-x0)1e-5);printf(therootis%f\n,x1);}牛顿法的算法:4四、列主元高斯消元法(通用程序)#includemath.h#defineM3#defineN3main(){floata[M][N+1],x[N],temp,l,s;inti,j,k,r;printf(pleaseinputthedate:\n);for(i=0;iM;i++)for(j=0;jN+1;j++)scanf(%f,&a[i][j]);for(k=0;kN-1;k++){{r=k;for(i=k+1;i=M-1;i++)if(fabs(a[i][k])fabs(a[r][k]))r=i;}if(a[r][k]==0){printf(QY\n);exit(0);}elseif(r!=k)for(j=k;j=N;j++){temp=a[k][j];a[k][j]=a[r][j];a[r][j]=temp;}for(i=k+1;i=N-1;i++){l=a[i][k]/a[k][k];for(j=k+1;j=N;j++)a[i][j]=a[i][j]-l*a[k][j];}}for(k=N-1;k=0;k--){s=0.0;for(j=k+1;j=N-1;j++)s=s+a[k][j]*x[j];x[k]=(a[k][N]-s)/a[k][k];}for(i=0;i=N-1;i++)printf(x[%d]=%f\n,i,x[i]);}列主元高斯消元法的算法:5五、LU分解法用LU分解法解线性方程组,系数矩阵由二维数组a给出,右端由b数组给出。#includemath.hmain(){inti,j,k,m,n=4;floatt;floatb[4]={1,1,-1,-1};floata[4][4]={{4,3,2,1},{3,4,3,2},{2,3,4,3},{1,2,3,4}};for(k=0;kn;k++){for(j=k;jn;j++){t=0;for(m=0;m=k-1;m++)t=t+a[k][m]*a[m][j];a[k][j]=a[k][j]-t;}for(i=k+1;in;i++){t=0;for(m=0;m=k-1;m++)t=t+a[i][m]*a[m][k];a[i][k]=(a[i][k]-t)/a[k][k];}}for(i=0;in;i++){t=0;for(j=0;j=i-1;j++)t=t+a[i][j]*b[j];b[i]=b[i]-t;}for(i=n-1;i=0;i--){t=0;for(k=i+1;kn;k++)t=t+a[i][k]*b[k];b[i]=(b[i]-t)/a[i][i];}for(i=0;in;i++)printf(%10.4f,b[i]);}LU分解法的算法:6六、雅可比迭代法1、通用程序#defineN4#includemath.hfloatcha(x,y)floatx[N],y[N];{floatz;inti,k;z=fabs(y[0]-x[0]);for(i=1;iN;i++)if(fabs(y[i]-x[i])z)z=fabs(y[i]-x[i]);return(z);}main(){floata[N][N],b[N],x[N],y[N],h;inti,j,k;floatt=0,s;for(i=0;iN;i++)for(j=0;jN;j++){scanf(%f,&h);a[i][j]=h;}for(i=0;iN;i++){scanf(%f,&h);b[i]=h;y[i]=0;}for(i=0;iN;i++)if(a[i][i]==0){printf(QIYI);exit(0);}printf(Jacobi\n);for(k=1;k=30;k++){for(i=0;iN;i++)x[i]=y[i];for(i=0;iN;i++){t=0;for(j=0;jN;j++)if(j!=i)t=t+a[i][j]*x[j];y[i]=(b[i]-t)/a[i][i];}if(cha(x,y)1e-6){printf(xunhuancishu:k=%d\n,k);for(i=0;iN;i++)printf(%10f,x[i]);printf(\n);break;}}if(k30)printf(fasann\n);}2、实验书第4题#includemath.hfloatcha(x,y)floatx[3],y[3];{floatz;inti,k,n=3;z=fabs(y[0]-x[0]);for(i=1;in;i++)if(fabs(y[i]-x[i])z)z=fabs(y[i]-x[i]);return(z);}main(){floata[3][3]={{10,-2,-1},{-2,10,-1},{-1,-2,5}};floatb[3]={3,15,10},y[3]={0,0,0},x[3];floatt=0,s;inti,j,k,n=3;printf(Gauss-Seidel\n);for(k=1;k=30;k++){for(i=0;in;i++)x[i]=y[i];for(i=0;in;i++){t=0;for(j=0;jn;j++)if(j!=i)t=t+a[i][j]*x[j];y[i]=(b[i]-t)/a[i][i];}if(cha(x,y)1e-6){printf(xunhuancishu:k=%d\n,k);for(i=0;in;i++)printf(%10f,y[i]);printf(\n);break;}}if(k30)printf(fasann\n);}�6六、雅可比迭代法1、通用程序#defineN4#includemath.hfloatcha(x,y)floatx[N],y[N];{floatz;inti,k;z=fabs(y[0]-x[0]);for(i=1;iN;i++)if(fabs(y[i]-x[i])z)z=fabs(y[i]-x[i]);return(z);}main(){floata[N][N],b[N],x[N],y[N],h;inti,j,k;floatt=0,s;for(i=0;iN;i++)for(j=
本文标题:计算方法及程序实现
链接地址:https://www.777doc.com/doc-6928279 .html