Advertisement
Guest User

Untitled

a guest
May 25th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /* Метод дихотомии */
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. const double PI = 3.14;
  9. const double eps = 0.001;
  10.  
  11. double findRoot(double a, double b, double eps);
  12.  
  13. int main()
  14. {
  15. double a; double b;
  16.  
  17. cout << "a = ";
  18. cin >> a;
  19.  
  20. cout << "b = ";
  21. cin >> b;
  22. cout << endl;
  23.  
  24. cout << "Результат: " << "\n";
  25. cout << "x = " << findRoot(a, b, eps);
  26. cout << endl;
  27.  
  28. return 0;
  29. }
  30.  
  31. double f(double x) {
  32. return (1.0 / x) - PI * cos(PI * x);
  33. }
  34.  
  35. double findRoot(double a, double b, double eps) {
  36. double c;
  37. while (abs((b - a) / 2.0) > eps) {
  38. c = (a + b) / 2.0;
  39. if ((f(a) * f(c)) < 0) {
  40. b = c;
  41. } else {
  42. a = c;
  43. }
  44. }
  45. return c;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement