Advertisement
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);
- }
- signed main() {
- double l = 0.2, r = 0.5;
- cout << "l = " << l << endl;
- cout << "r = " << r << endl;
- int k = 1;
- vector<double> epss = { 1e-2, 1e-3, 1e-4 };
- double a = l, b = r;
- for (auto eps : epss) {
- cout.unsetf(ios_base::fixed);
- cout << endl << "current epsilon: " << eps << endl;
- cout << fixed << setprecision(5);
- while (true) {
- if (fabs(f(b)) < eps) break;
- a = b - (b - a) * f(b) / (f(b) - f(a));
- b = a - (a - b) * f(a) / (f(a) - f(b));
- cout << "x" << k << " = " << setw(8) << b <<
- " f(x" << k++ << ") = " << setw(8) << f(b) << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement