C统考习题答案(60)

  • 格式:doc
  • 大小:189.00 KB
  • 文档页数:22

统考复习题 1. 编写程序,用牛顿切线法求方程f(x)=x+㏑x-1.56=0的近似实根r,迭代初值为2,精确到0.0001。(注意:㏑x是以e为底的自然对数)(提示:牛顿切线法的计算公式为x=x-f(x)/f′(x)). #include "stdio.h" #include "math.h" void main() {float x0,x1; x0=2; x1=x0-(x0+log(x0)-1.56)/(1+1/x0); do {x0=x1; x1=x0-(x0+log(x0)-1.56)/(1+1/x0);} while(fabs(x0-x1)>1e-4); printf("x=%10.4f\n",x1); } 运行结果:x= 1.2987

2. 编写程序,用牛顿切线法求方程f(x)= x+㏒x-3.18=0的近似实根r,迭代初值为2.5,要求结果保留4位小数。(注意:㏒x是以10为底常用对数lgx)(提示:必须把方程f(x)=0化成其等价形式x=g(x))(本题允许使用其他迭代法)

#include "stdio.h" #include "math.h" void main() {float x0,x1; x0=2.5; x1=x0-(x0+log10(x0)-3.18)/(1+1/(x0*log(10))); do {x0=x1; x1=x0-(x0+log10(x0)-3.18)/(1+1/(x0*log(10))); } while(fabs(x0-x1)>1e-4); printf("x=%10.4f\n",x1); } 运行结果:x=2.741938

3. 编写程序,用普通迭代法求方程f(x)= x+㏒x-3.18=0的近似实根r,迭代初值为2.5,要求结果保留4位小数。(注意:㏒x是以10为底常用对数lgx)(提示:必须把方程f(x)=0化成其等价形式x=g(x))(本题允许使用其他迭代法) 要求:在运行程序时,使用文本框或InputBox函数输入迭代初值,并用适当的方法输出运算结果 #include "stdio.h" #include "math.h" void main() {float x0,x1; x0=2.5; x1=3.18-log10(x0); do {x0=x1; x1=3.18-log10(x0);} while(fabs(x0-x1)>1e-4); printf("x=%f\n",x1); } 运行结果:x=2.741938

4. 编写程序,用牛顿切线法求方程f(x)=x^3+2x^2-14=0(其中^表示幂运算),在区间(0,5)上的近似实根r,迭代初值自选,精确到0.0001。{提示:牛顿切线法的计算公式为x=x-f(x)/ f’(x)}. #include "stdio.h" #include "math.h" void main() {float x0,x1; x0=2.5; x1=x0-(pow(x0,3)+2*pow(x0,2)-14)/(3*pow(x0,2)+4*x0) ; do {x0=x1; x1=x0-(pow(x0,3)+2*pow(x0,2)-14)/(3*pow(x0,2)+4*x0) ;} while(fabs(x0-x1)>1e-4); printf("x=%f\n",x1); } 运行结果:x=1.895706

5. 编写程序,用二分法求一元非线性方程f(x)=x+cosx-2.8=0在区间(0,5)上的近似实根r,精确到0.0001。

#include "stdio.h" #include "math.h" double f(double x) { double y; y=x+cos(x)-2.8; return y; } double xpoint(double x1,double x2) { double y; y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); return y; } double root(double x1,double x2) { double x,y,y1; y1=f(x1); do { x=xpoint(x1,x2); y=f(x); if(y*y1>0) { y1=y;x1=x;} else x2=x; }while(fabs(y)>=0.0001); return x; }

void main() { double x,x1,x2; x1=0; x2=5; if(f(x1)*f(x2)>0) printf("此区间无根"); else x=root(x1,x2); printf("x=%8.4f\n",x ); }

/*x= 3.6657*/ 6. 编写程序,用矩形法求一元函数f(x)=x^3+2x^2-12x-6(其中^示幂运算),在区间[3.5, 5]上的积分近似值S,保留2位小数(小区间数n=15,此参数不能改动,否则影响答案)。 #include "stdio.h" #include "math.h" double f(double x) { double y; y=x*x*x+2*x*x-12*x-6; return y; } void main() { int i,n; double a,b,x,h,s; n=15; a=3.5; b=5.0; h=(b-a)/n; x=a; s=0; for(i=1;i<=n;i++) { x=x+h; s=s+f(x)*h; } printf("s=%6.2f\n",s); } 运行结果:s=92.50 7. 编写程序,用矩形法求一元函数f(x)=x^3+2x^2-12x-6(其中^示幂运算),在区间[3.5, 5]上的积分近似值S,保留2位小数(小区间数n=25,此参数不能改动,否则影响答案) #include "stdio.h" #include "math.h" double f(double x) { double y; y=x*x*x+2*x*x-12*x-6; return y; } void main() { int i,n; double a,b,x,h,s; n=25; a=3.5; b=5.0; h=(b-a)/n; x=a; s=0; for(i=1;i<=n;i++) { x=x+h; s=s+f(x)*h; } printf("s=%6.2f\n",s); }

运行结果:s=90.69 8. 要求:编写程序,用梯形法求一元函数f(x)= ㏑(x+1)+ x/3,在区间[1,4]上的积分近似值S,保留3位小数(小区间数n=20,此参数不能改动)。(注意:㏑x是以e为底的自然对数) 在运行程序时,使用文本框或InputBox函数输入n值,并用适当的方法输出运算结果。 #include "stdio.h" #include "math.h" double f(double x) { double y; y=log(x+1)+x/3.0; return y; } double integ(double a, double b) { double s,x,h; int n=20,i; h=fabs(b-a)/n; s=(f(a)+f(b))/2.0; for(i=1;i<=n-1; i++) { x=a+i*h; s=s+f(x);} s=s*h; return s; } void main() { double s; s=integ(1.0,4.0); printf("s=%8.3f\n",s); } 运行结果:s= 6.160

9. 编写程序,用梯形法求一元函数f(x)=7x^3+2x^2-12x-16(其中^表示幂运算),在区间[3,5]上的积分近似植S,保留2位小数(小区间数n=25,此参数不能改动,否则影响答案) #include "stdio.h" #include "math.h" double f(double x) { double y; y=7*x*x*x+2*x*x-12*x-16; return y; } double integ(double a, double b) { double s,x,h; int n=25,i; h=fabs(b-a)/n; s=(f(a)+f(b))/2.0; for(i=1;i<=n-1; i++) { x=a+i*h; s=s+f(x);} s=s*h; return s; } void main() { double s; s=integ(3.0,5.0); printf("s=%8.2f\n",s); } 运行结果:s=889.52

10. 编写程序,用矩形法求一元函数f(x)=7x^3+2x^2-12x-16(其中^表示幂运算),在区间[5,7]上的积分近似值S,保留2位小数(小区间数n=15,此参数不能改动,否则影响答案) #include "stdio.h" #include "math.h" double f(double x)