#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
// Definicja funkcji
double f(double x) {
return 0.25 * x * x - 2.0;
}
int main() {
double a = 2.0;
double b = 4.0;
double eps = 1e-6;
double c;
// Sprawdzenie warunku bisekcji
if (f(a) * f(b) >= 0) {
cout << "Warunek f(a)*f(b) < 0 nie jest spełniony!" << endl;
return 1;
}
// Metoda bisekcji
while ((b - a) / 2.0 > eps) {
c = (a + b) / 2.0;
if (f(c) == 0.0)
break;
else if (f(a) * f(c) < 0)
b = c;
else
a = c;
}
cout << "Miejsce zerowe: x = " << c << endl;
// Zapis danych do pliku
ofstream data("dane.dat");
for (double x = 1.5; x <= 4.5; x += 0.01) {
data << x << " " << f(x) << endl;
}
data.close();
// Skrypt gnuplota
ofstream plot("wykres.gp");
plot << "set terminal png size 800,600\n";
plot << "set output 'wykres.png'\n";
plot << "set title 'Wykres f(x) = 1/4 x^2 - 2'\n";
plot << "set xlabel 'x'\n";
plot << "set ylabel 'f(x)'\n";
plot << "set grid\n";
plot << "plot 'dane.dat' with lines title 'f(x)', \\\n";
plot << " 0 with lines lt -1 title 'oś X'\n";
plot.close();
// Uruchomienie gnuplota
system("gnuplot wykres.gp");
cout << "Wykres zapisany jako wykres.png" << endl;
return 0;
}