fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Definicja funkcji
  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; // dokładność
  15. double c;
  16.  
  17. // Sprawdzenie warunku istnienia miejsca zerowego
  18. if (f(a) * f(b) >= 0) {
  19. cout << "Brak miejsca zerowego w podanym przedziale." << endl;
  20. return 0;
  21. }
  22.  
  23. // Metoda bisekcji
  24. while ((b - a) / 2 > eps) {
  25. c = (a + b) / 2;
  26.  
  27. if (f(c) == 0.0)
  28. break;
  29. else if (f(a) * f(c) < 0)
  30. b = c;
  31. else
  32. a = c;
  33. }
  34.  
  35. cout << "Miejsce zerowe funkcji: x = " << (a + b) / 2 << endl;
  36. cout << "Wartosc funkcji w tym punkcie: f(x) = " << f((a + b) / 2) << endl;
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Miejsce zerowe funkcji: x = 2.82842
Wartosc funkcji w tym punkcie: f(x) = -6.47477e-06