fork(1) 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. return 0.25 * x * x - 2.0;
  11. }
  12.  
  13. int main() {
  14. double a = 2.0;
  15. double b = 4.0;
  16. double eps = 1e-6;
  17. double c;
  18.  
  19. // Sprawdzenie warunku bisekcji
  20. if (f(a) * f(b) >= 0) {
  21. cout << "Warunek f(a)*f(b) < 0 nie jest spełniony!" << endl;
  22. return 1;
  23. }
  24.  
  25. // Metoda bisekcji
  26. while ((b - a) / 2.0 > eps) {
  27. c = (a + b) / 2.0;
  28.  
  29. if (f(c) == 0.0)
  30. break;
  31. else if (f(a) * f(c) < 0)
  32. b = c;
  33. else
  34. a = c;
  35. }
  36.  
  37. cout << "Miejsce zerowe: x = " << c << endl;
  38.  
  39. // Zapis danych do pliku
  40. ofstream data("dane.dat");
  41. for (double x = 1.5; x <= 4.5; x += 0.01) {
  42. data << x << " " << f(x) << endl;
  43. }
  44. data.close();
  45.  
  46. // Skrypt gnuplota
  47. ofstream plot("wykres.gp");
  48. plot << "set terminal png size 800,600\n";
  49. plot << "set output 'wykres.png'\n";
  50. plot << "set title 'Wykres f(x) = 1/4 x^2 - 2'\n";
  51. plot << "set xlabel 'x'\n";
  52. plot << "set ylabel 'f(x)'\n";
  53. plot << "set grid\n";
  54. plot << "plot 'dane.dat' with lines title 'f(x)', \\\n";
  55. plot << " 0 with lines lt -1 title 'oś X'\n";
  56. plot.close();
  57.  
  58. // Uruchomienie gnuplota
  59. system("gnuplot wykres.gp");
  60.  
  61. cout << "Wykres zapisany jako wykres.png" << endl;
  62.  
  63. return 0;
  64. }
  65.  
Success #stdin #stdout #stderr 0.01s 5308KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843
Wykres zapisany jako wykres.png
stderr
sh: 1: gnuplot: not found