Advertisement
illfate

Untitled

May 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <functional>
  8. double function_(double x) {
  9. return x*x-10*std::sin(x)*std::sin(x)+2 ;
  10. }
  11.  
  12. double derivative(double x) {
  13. return 2*x-10*std::sin(2*x);
  14. }
  15.  
  16.  
  17. double NewtonsMethod(std::function<double(double x)> f, std::function<double(double x)> df, double xn, double eps) {
  18. double x1 = xn - f(xn) / df(xn);
  19. double x0 = xn;
  20. while (abs(x0 - x1) >= eps) {
  21. if (df(x1) == 0) break;
  22. x0 = x1;
  23. x1 = x1 - f(x1) / df(x1);
  24. }
  25. return x1;
  26. }
  27.  
  28.  
  29.  
  30.  
  31. int main() {
  32. const double A = -3;
  33. const double B = 2;
  34. double eps;
  35. std::cin >> eps;
  36. double h;
  37. std::cin >> h;
  38. for (double it = A; it <= B; it += h) {
  39. if (function_(it)*function_(it+h) < 0){
  40. std::cout << NewtonsMethod(function_, derivative, it, eps) << std::endl;
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement