fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. // Funkcja
  8. double f(double x) {
  9. return 0.25 * x * x - 2.0;
  10. }
  11.  
  12. int main() {
  13. // Stałe wartości – brak inputu
  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 << "Brak miejsca zerowego w przedziale." << endl;
  22. return 1;
  23. }
  24.  
  25. // Metoda bisekcji
  26. while ((b - a) > 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 funkcji: x = " << c << endl;
  38.  
  39. // Dane do wykresu
  40. ofstream dane("dane.txt");
  41. for (double x = 1.5; x <= 4.5; x += 0.01) {
  42. dane << x << " " << f(x) << endl;
  43. }
  44. dane.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 title 'f(x) = 1/4 x^2 - 2'\n";
  51. gp << "set xlabel 'x'\n";
  52. gp << "set ylabel 'f(x)'\n";
  53. gp << "set grid\n";
  54. gp << "plot 'dane.txt' with lines title 'f(x)'\n";
  55. gp.close();
  56.  
  57. system("gnuplot wykres.gp");
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout #stderr 0.01s 5316KB
stdin
Standard input is empty
stdout
Miejsce zerowe funkcji: x = 2.82843
stderr
sh: 1: gnuplot: not found