Gistrec

asm3 C++ edition

Dec 3rd, 2018
321
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. // Написать  программу, реализующую метод 'Ньютона с подвижным полюсом'
  6. // Входные данные: Точность решения
  7.  
  8. // Функция f(X)
  9. double f(double x) {
  10.     return log((x - 1) / 3) / 3;
  11. }
  12.  
  13. // Производная функции f(x)
  14. double fdx(double x) {
  15.     return 1 / (3 * (x - 1));
  16. }
  17.  
  18. // ln((x - 1) / 3 ) / 3 = 0
  19. double next(double x) {
  20.     return x + f(x) * (f(x) - 2) / 2 * fdx(x);
  21. }
  22.  
  23. int main() {
  24.     double x = 1.5;
  25.     double nextX = 1.5;
  26.  
  27.     double accuracy = 1e-10;
  28.  
  29.     for (int i = 0;; i++) {
  30.         nextX = next(x);
  31.  
  32.         if (abs(nextX - x) < accuracy) {
  33.             std::cout << std::setprecision(10) << x << std::endl;
  34.             return 0;
  35.         }
  36.         x = nextX;
  37.     }
  38.     return 0;
  39. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment