Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.        
  3. using namespace std;
  4.  
  5.        
  6. double f(double x)
  7. {
  8.     return 3.0 * cos(x) - x;
  9. }
  10.  
  11. double dfdx(double x)
  12. {
  13.     return (- 3.0 * sin(x) - 1);
  14. }
  15.  
  16.  
  17. int main()
  18. {              
  19.     cout.precision(20);
  20.  
  21.     double eps;
  22.  
  23.     cout << "Get me your precision: ";
  24.  
  25.     cin >> eps;
  26.  
  27.         cout << '\n';
  28.        
  29.         double x = 0;
  30.  
  31.         int iter = 1;
  32.  
  33.     while (abs(f(x) - 0.) > eps)
  34.     {                              
  35.             assert(abs(dfdx(x)) > 1e-9);
  36.  
  37.             x = x - f(x) / dfdx(x);
  38.  
  39.         cout << "On step " << iter << " value is " << x << ".\n";
  40.  
  41.         cout << "Residual is " << -f(x) << ".\n";
  42.  
  43.         cout << " * * * * \n";
  44.  
  45.         iter++;
  46.  
  47.     }                            
  48.  
  49.         cout << "(x, f(x) = (" << x <<  ", " << f(x) << ")\n";
  50.  
  51.         return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement