Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- #define endl '\n'
- #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
- double f(double x) {
- return x + 1 / x - 4 * cos(x);
- }
- double df(double x) {
- return 1 - 1 / x / x + 4 * sin(x);
- }
- signed main() {
- double l = 1, r = 2;
- cout << "l = " << l << endl;
- cout << "r = " << r << endl;
- int k = 1;
- double x = (r + l) * 0.5;
- vector<double> epss = { 1e-2, 1e-3, 1e-4 };
- for (auto eps : epss) {
- cout.unsetf(ios_base::fixed);
- cout << endl << "current epsilon: " << eps << endl;
- cout << fixed << setprecision(5);
- while (true) {
- if (fabs(f(x)) < eps) break;
- x = x - f(x) / df(x);
- cout << "x" << k << " = " << setw(8) << x <<
- " f(x" << k++ << ") = " << setw(8) << f(x) << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment