fork(2) download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. double f(double x)
  6. {
  7. return cos(x);
  8. }
  9.  
  10. int main()
  11. {
  12. double p,p0=1,eps = 0.001;
  13. int i=1,N=1000;
  14.  
  15. while(i <= N)
  16. {
  17. p = f(p0);
  18. if(fabs(p-p0) < eps)
  19. {
  20. cout<<p<<endl;
  21. break;
  22. }
  23. cout<<"Iteration "<<i<<": p = "<<p<<endl;
  24. i++;
  25. p0 = p;
  26. cout<<"The solution is "<<p<<endl;
  27. if(i>N)
  28. {
  29. cout<<"Solution not found (method diverges)"<<endl;;
  30. break;
  31. }
  32. }
  33. cout<<"The approximated solution is x = "<<p<<" in the iteration "<<i-1<<endl;
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Iteration 1: p = 0.540302
The solution is 0.540302
Iteration 2: p = 0.857553
The solution is 0.857553
Iteration 3: p = 0.65429
The solution is 0.65429
Iteration 4: p = 0.79348
The solution is 0.79348
Iteration 5: p = 0.701369
The solution is 0.701369
Iteration 6: p = 0.76396
The solution is 0.76396
Iteration 7: p = 0.722102
The solution is 0.722102
Iteration 8: p = 0.750418
The solution is 0.750418
Iteration 9: p = 0.731404
The solution is 0.731404
Iteration 10: p = 0.744237
The solution is 0.744237
Iteration 11: p = 0.735605
The solution is 0.735605
Iteration 12: p = 0.741425
The solution is 0.741425
Iteration 13: p = 0.737507
The solution is 0.737507
Iteration 14: p = 0.740147
The solution is 0.740147
Iteration 15: p = 0.738369
The solution is 0.738369
Iteration 16: p = 0.739567
The solution is 0.739567
0.73876
The approximated solution is x = 0.73876 in the iteration 16