fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. double f(double x) {
  8. return 0.25 * x * x - 2.0;
  9. }
  10.  
  11. int main() {
  12. double a = 2.0;
  13. double b = 4.0;
  14. double eps = 0.00001;
  15. double c;
  16.  
  17. if (f(a) * f(b) >= 0) {
  18. cout << "Brak miejsca zerowego w przedziale." << endl;
  19. return 0;
  20. }
  21.  
  22. while ((b - a) / 2 > eps) {
  23. c = (a + b) / 2;
  24.  
  25. if (f(c) == 0.0)
  26. break;
  27. else if (f(a) * f(c) < 0)
  28. b = c;
  29. else
  30. a = c;
  31. }
  32.  
  33. cout << fixed << setprecision(6);
  34. cout << "Miejsce zerowe funkcji: x = " << (a + b) / 2 << endl;
  35. cout << "Wartosc funkcji w tym punkcie: f(x) = "
  36. << f((a + b) / 2) << endl;
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Miejsce zerowe funkcji: x = 2.828423
Wartosc funkcji w tym punkcie: f(x) = -0.000006