fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  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, b = 4.0;
  13. double eps = 0.0001;
  14. double x0;
  15.  
  16. if (f(a) * f(b) >= 0) {
  17. cout << "Brak miejsca zerowego w podanym przedziale!" << endl;
  18. return 1;
  19. }
  20.  
  21. // Metoda bisekcji
  22. while ((b - a) >= eps) {
  23. x0 = (a + b) / 2.0;
  24.  
  25. if (f(x0) == 0.0)
  26. break;
  27. else if (f(a) * f(x0) < 0)
  28. b = x0;
  29. else
  30. a = x0;
  31. }
  32.  
  33. cout << "Miejsce zerowe: x = " << x0 << endl;
  34.  
  35. // Zapis danych do wykresu
  36. ofstream data("funkcja.dat");
  37. for (double x = 1.5; x <= 4.5; x += 0.05) {
  38. data << x << " " << f(x) << endl;
  39. }
  40. data.close();
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843