fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x) {
  7. return 0.25 * x * x - 2.0;
  8. }
  9.  
  10. int main() {
  11. double x0 = 2.0; // pierwszy punkt
  12. double x1 = 4.0; // drugi punkt
  13. double x2;
  14. double eps = 0.0001;
  15.  
  16. // Sprawdzenie warunku początkowego
  17. if (f(x0) * f(x1) >= 0) {
  18. cout << "Warunek f(x0)*f(x1) < 0 nie jest spelniony!" << endl;
  19. return 1;
  20. }
  21.  
  22. // Metoda siecznych
  23. do {
  24. x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
  25. x0 = x1;
  26. x1 = x2;
  27. } while (fabs(f(x2)) > eps);
  28.  
  29. cout << "Miejsce zerowe: x = " << x2 << endl;
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82842