fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Definicja funkcji
  7. double f(double x)
  8. {
  9. return 0.25 * x * x - 2.0;
  10. }
  11.  
  12. int main()
  13. {
  14. double a = 2.0;
  15. double b = 4.0;
  16. double eps = 1e-6;
  17. double c;
  18.  
  19. // Sprawdzenie warunku istnienia miejsca zerowego
  20. if (f(a) * f(b) >= 0)
  21. {
  22. cout << "Brak zmiany znaku w przedziale!" << endl;
  23. return 1;
  24. }
  25.  
  26. // Metoda bisekcji
  27. while ((b - a) > eps)
  28. {
  29. c = (a + b) / 2.0;
  30.  
  31. if (f(a) * f(c) < 0)
  32. b = c;
  33. else
  34. a = c;
  35. }
  36.  
  37. // Wynik
  38. c = (a + b) / 2.0;
  39. cout << "Miejsce zerowe: x = " << c << endl;
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843