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