fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. // Definicja funkcji
  8. double f(double x)
  9. {
  10. return 0.25 * x * x - 2.0;
  11. }
  12.  
  13. int main()
  14. {
  15. double a = 2.0;
  16. double b = 4.0;
  17. double eps = 1e-6; // dokładność
  18. double c;
  19.  
  20. // Sprawdzenie warunku
  21. if (f(a) * f(b) >= 0)
  22. {
  23. cout << "Warunek f(a)*f(b) < 0 nie jest spełniony!" << endl;
  24. return 1;
  25. }
  26.  
  27. // Metoda bisekcji
  28. while ((b - a) > eps)
  29. {
  30. c = (a + b) / 2.0;
  31.  
  32. if (f(c) == 0.0)
  33. break;
  34. else if (f(a) * f(c) < 0)
  35. b = c;
  36. else
  37. a = c;
  38. }
  39.  
  40. cout << "Miejsce zerowe: x = " << c << endl;
  41. cout << "f(x) = " << f(c) << endl;
  42.  
  43. // Zapis danych do wykresu
  44. ofstream file("wykres.dat");
  45. for (double x = 1.5; x <= 4.5; x += 0.01)
  46. {
  47. file << x << " " << f(x) << endl;
  48. }
  49. file.close();
  50.  
  51. cout << "Dane do wykresu zapisane w pliku wykres.dat" << endl;
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843
f(x) = 2.68718e-07
Dane do wykresu zapisane w pliku wykres.dat