fork(1) 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 = 1e-6; // dokładność
  15. double c;
  16.  
  17. // Sprawdzenie warunku metody bisekcji
  18. if (f(a) * f(b) >= 0) {
  19. cout << "Brak miejsca zerowego w podanym przedziale." << endl;
  20. return 1;
  21. }
  22.  
  23. // Metoda bisekcji
  24. while ((b - a) / 2.0 > eps) {
  25. c = (a + b) / 2.0;
  26.  
  27. if (f(c) == 0.0) {
  28. break; // znaleziono dokładne miejsce zerowe
  29. } else if (f(a) * f(c) < 0) {
  30. b = c;
  31. } else {
  32. a = c;
  33. }
  34. }
  35.  
  36. c = (a + b) / 2.0;
  37. cout << "Miejsce zerowe funkcji: x = " << c << endl;
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Miejsce zerowe funkcji: x = 2.82843