全部博文(556)
分类: C/C++
2011-12-04 14:28:48
牛顿迭代法1:
#include "stdio.h" #include "math.h" #define C 411.7910 #define e 0.01 void main() { float x0,x1=7.0f; int i=0; printf("\nx%d=%6.4f",i,x1); i++; do { x0=x1; x1=x0-(x0*x0*x0-C)/(3*x0*x0); printf("\nx%d=%6.4f",i,x1); i++; }while(fabs(x1-x0)>e); printf("\nx=%4.2f",x1); getchar(); } |
计算结果:
x0=7.0000
x1=7.4680
x2=7.4399
x3=7.4398
x=7.44
牛顿迭代法2:
#include "stdio.h" #include "math.h" #define e 0.00001 void main() { float x0,x1=0.5; int i=0; printf("\nx%d=%7.5f",i,x1); i++;
do{ x0=x1; x1=x0-(x0-exp(-x0))/(exp(x0)+x0*exp(x0)); printf("\nx%d=%7.5f",i,x1); i++; }while(fabs(x1-x0)>=e); printf("\nx=%6.5f\n",x1); } |
运行结果:
x0=0.50000
x1=0.54308
x2=0.55734
x3=0.56300
x4=0.56537
x5=0.56638
x6=0.56681
x7=0.56700
x8=0.56708
x9=0.56712
x10=0.56713
x11=0.56714
x=0.56714
Press any key to continue