fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double f(double x){
  5. return cos(x)-x;
  6. }
  7.  
  8. double f1(double x){
  9. return -sin(x)-1;
  10. }
  11.  
  12. int main(void) {
  13. double eps=1.0e-6;
  14. double x=1;
  15. int kmax=10;
  16. double d;
  17. int k;
  18.  
  19. k=0;
  20. do{
  21. d=-f(x)/f1(x);
  22. x +=d;
  23. k++;
  24. printf("k=%2d x=%19.16f d=%10.2e\n", k, x, d);
  25. }while(fabs(d)>eps &&k<kmax);
  26.  
  27. if(fabs(d)>eps){
  28. printf("failure\n");
  29. }
  30.  
  31. printf("f(x)=%9.2e\n", f(x));
  32.  
  33. // your code goes here
  34. return 0;
  35. }
Success #stdin #stdout 0s 5524KB
stdin
Standard input is empty
stdout
k= 1 x= 0.7503638678402439 d= -2.50e-01
k= 2 x= 0.7391128909113617 d= -1.13e-02
k= 3 x= 0.7390851333852840 d= -2.78e-05
k= 4 x= 0.7390851332151607 d= -1.70e-10
f(x)= 0.00e+00