fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. // Definicja funkcji
  9. double f(double x)
  10. {
  11. return 0.25 * x * x - 2.0;
  12. }
  13.  
  14. int main()
  15. {
  16. double a = 2.0;
  17. double b = 4.0;
  18. double eps = 1e-6;
  19. double c;
  20.  
  21. // Sprawdzenie warunku
  22. if (f(a) * f(b) >= 0)
  23. {
  24. cout << "Brak zmiany znaku w przedziale!" << endl;
  25. return 1;
  26. }
  27.  
  28. // Metoda bisekcji
  29. while ((b - a) > eps)
  30. {
  31. c = (a + b) / 2.0;
  32.  
  33. if (f(a) * f(c) < 0)
  34. b = c;
  35. else
  36. a = c;
  37. }
  38.  
  39. double root = (a + b) / 2.0;
  40. cout << "Miejsce zerowe: x = " << root << endl;
  41.  
  42. // Zapis danych do pliku
  43. ofstream dane("dane.txt");
  44. for (double x = 1.5; x <= 4.5; x += 0.01)
  45. dane << x << " " << f(x) << endl;
  46. dane.close();
  47.  
  48. // Skrypt gnuplot
  49. ofstream wykres("wykres.gp");
  50. wykres << "set grid\n";
  51. wykres << "set xlabel 'x'\n";
  52. wykres << "set ylabel 'f(x)'\n";
  53. wykres << "set title 'Wykres f(x) = (1/4)x^2 - 2'\n";
  54. wykres << "plot 'dane.txt' with lines title 'f(x)', 0 with lines lt -1\n";
  55. wykres.close();
  56.  
  57. system("gnuplot -persist wykres.gp");
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout #stderr 0s 5316KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843
stderr
sh: 1: gnuplot: not found