fork(13) download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. double f(double x) {
  6. return x*x*x+10*sin(5*x);
  7. }
  8.  
  9. int main() {
  10. double h=0.001;
  11. double x, pogr;
  12. cin >> x >> pogr;
  13. if (x==0) x+=0.1;
  14. while ((f(x+h)-2*f(x)+f(x-h))/(h*h)<=0) x+=0.1;
  15. double x1;
  16. x1=x-0.5*h*(f(x+h)-f(x-h))/(f(x+h)-2*f(x)+f(x-h));
  17. while (fabs(x1-x)>pogr) {
  18. x=x1;
  19. x1=x-0.5*h*(f(x+h)-f(x-h))/(f(x+h)-2*f(x)+f(x-h));
  20. }
  21. cout << x1;
  22. return 0;
  23. }
Success #stdin #stdout 0s 3476KB
stdin
4.1 0.01
stdout
3.31207