资源描述:
《计算方法c语言编程》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、计算方法C语言编程第二章2已知方程在区间[1,2]内有一根,试问用二分法求根,使其具有5位有效数字至少应二分多少次?【程序设计】#includemain(){intn=0;floatx1=1.0,x2=2.0,x=1.0,x0;do{x0=x;x=(x1+x2)/2;n++;if(x*x*x+x-4>0)x2=x;elsex1=x;}while(fabs(x-x0)>0.00005);printf("N=%d",n);}〖运行结果〗N=154用迭代法求的正根,要求准确到小数点后第5位。【程序设计】
2、#includemain(){floatx0,x=1.5,y;do{y=(log(x+0.2))/5;x0=x;x=exp(y);}while(fabs(x-x0)>0.000005);printf("X=%f",x);}-〖运行结果〗x=1.0447639用牛顿法求方程在x0=2附近的根,要求准确到小数点后第3位。【程序设计】#includemain(){floatx=2.0,x0;do{x0=x;x=x-(x*x*x-3*x-1)/(3*x*x-3);}while(fabs(x
3、-x0)>0.0005);printf("X=%f",x);}〖运行结果〗x=1.87938511.分别用单点和双点弦截法求方程-x-1=0在[1,1.5]内的根。要求
4、xn+1-xn
5、<=0.000005【程序设计】#includefloatf(floatx){floatf;f=x*x*x-x-1;returnf;}floatg(floatx1,floatx2){floatg;g=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));returng;}main(){floatx1=
6、1.0,x2=1.5,x,x0;x=x1;do{x0=x;x=g(x1,x2);if(f(x)>0)x2=x;elsex1=x;}while(fabs(x-x0)>0.000005);printf("X=%f",x);}〖运行结果〗x=1.324717第三章1.分别用列主元素消去法求解下列方程组.(计算取4位小数).【程序设计】#definen4main(){floata[n][n]={1.1161,0.1254,0.1397,0.1490,0.1582,1.1675,0.1768,0.1871,0.1968,0
7、.2071,1.2168,0.2271,0.2368,0.2471,0.2568,1.2671},b[n]={1.5471,1.6471,1.7471,1.8471},k,x[n+1],y[n+1];inti,j,t;y[n]=0.0;for(i=0;i=
8、0;i--){for(j=n-2;j>=0;j--){if(a[i][j]!=0)b[i]=b[i]-y[j+1]*a[i][j+1];elsebreak;}y[i]=b[i]/a[i][j+1];x[i+1]=y[i];}for(i=1;i<=n;i++)printf("X%d=%f",i,x[i]);}〖运行结果〗x1=1.040584x2=0.986957x3=0.935052x4=0.8812979设有方程组取初始向量,分别用雅可比迭代法与赛德尔迭代法求解,要求时迭代终止.【程序设计】#include9、ath.h>main()/*赛德尔*/{floatx1[100],x2[100],x3[100],y;intk=0;x1[0]=-3;x2[0]=1;x3[0]=1;do{x1[k+1]=((-2)*x2[k]-x3[k]-12)/5;x2[k+1]=(x1[k+1]-2*x3[k]+20)/4;x3[k+1]=((-2)*x1[k+1]+3*x2[k+1]+3)/10;if(fabs(x3[k+1]-x3[k])>fabs(x2[k+1]-x2[k]))y=x3[k+1]-x3[k];elseif(fabs(x2[
10、k+1]-x2[k])>fabs(x1[k+1]-x1[k]))y=x2[k+1]-x2[k];elsey=x1[k+1]-x1[k];k++;}while(fabs(y)>0.001);printf("x1=%fx2=%fx3=%f",x1[k],x2[k],x3[k]);printf("K=%d",k);}〖运行结果〗x1=