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