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 koniecznego
  21. if (f(a) * f(b) >= 0)
  22. {
  23. cout << "Funkcja nie spelnia warunku f(a)*f(b) < 0" << endl;
  24. return 1;
  25. }
  26.  
  27. // Metoda bisekcji
  28. do
  29. {
  30. c = (a + b) / 2.0;
  31.  
  32. if (f(a) * f(c) < 0)
  33. b = c;
  34. else
  35. a = c;
  36.  
  37. } while (fabs(b - a) > eps);
  38.  
  39. cout << "Miejsce zerowe: x = " << c << endl;
  40. cout << "f(x) = " << f(c) << endl;
  41.  
  42. // Zapis danych do pliku (do wykresu)
  43. ofstream plik("dane.dat");
  44. for (double x = 1.5; x <= 4.5; x += 0.01)
  45. {
  46. plik << x << " " << f(x) << endl;
  47. }
  48. plik.close();
  49.  
  50. cout << "Dane do wykresu zapisane w pliku dane.dat" << endl;
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843
f(x) = 2.68718e-07
Dane do wykresu zapisane w pliku dane.dat