fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  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;
  18. double c;
  19.  
  20. if (f(a) * f(b) >= 0)
  21. {
  22. cout << "Brak zmiany znaku w przedziale!" << endl;
  23. return 1;
  24. }
  25.  
  26. // Metoda bisekcji
  27. while ((b - a) > eps)
  28. {
  29. c = (a + b) / 2.0;
  30.  
  31. if (f(a) * f(c) < 0)
  32. b = c;
  33. else
  34. a = c;
  35. }
  36.  
  37. double root = (a + b) / 2.0;
  38. cout << "Miejsce zerowe: x = " << root << endl;
  39.  
  40. // Zapis danych do wykresu
  41. ofstream data("dane.txt");
  42. for (double x = 1.5; x <= 4.5; x += 0.01)
  43. data << x << " " << f(x) << endl;
  44. data.close();
  45.  
  46. // Skrypt gnuplot
  47. ofstream gp("wykres.gp");
  48. gp << "set terminal jpeg size 800,600\n";
  49. gp << "set output 'wykres.jpg'\n";
  50. gp << "set grid\n";
  51. gp << "set xlabel 'x'\n";
  52. gp << "set ylabel 'f(x)'\n";
  53. gp << "set title 'Wykres f(x) = (1/4)x^2 - 2'\n";
  54. gp << "plot 'dane.txt' with lines title 'f(x)', \\\n";
  55. gp << root << " title 'miejsce zerowe'\n";
  56. gp.close();
  57.  
  58. system("gnuplot wykres.gp");
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout #stderr 0.01s 5288KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843
stderr
sh: 1: gnuplot: not found