Georgiy031

Untitled

Oct 5th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. #define all(x) x.begin(), x.end()
  7. #define rall(x) x.rbegin(), x.rend()
  8. #define endl '\n'
  9. #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  10. ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
  11.  
  12. double f(double x) {
  13.     return x + 1 / x - 4 * cos(x);
  14. }
  15. double df(double x) {
  16.     return 1 - 1 / x / x + 4 * sin(x);
  17. }
  18.  
  19. signed main() {
  20.     double l = 1, r = 2;
  21.     cout << "l = " << l << endl;
  22.     cout << "r = " << r << endl;
  23.  
  24.     int k = 1;
  25.     double x = (r + l) * 0.5;
  26.     vector<double> epss = { 1e-2, 1e-3, 1e-4 };
  27.     for (auto eps : epss) {
  28.         cout.unsetf(ios_base::fixed);
  29.         cout << endl << "current epsilon: " << eps << endl;
  30.         cout << fixed << setprecision(5);
  31.  
  32.         while (true) {
  33.             if (fabs(f(x)) < eps) break;
  34.             x = x - f(x) / df(x);
  35.             cout << "x" << k << " = " << setw(8) << x <<
  36.                 "   f(x" << k++ << ") = " << setw(8) << f(x) << endl;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment