Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- double f(double x)
- {
- return 3.0 * cos(x) - x;
- }
- double dfdx(double x)
- {
- return (- 3.0 * sin(x) - 1);
- }
- int main()
- {
- cout.precision(20);
- double eps;
- cout << "Get me your precision: ";
- cin >> eps;
- cout << '\n';
- double x = 0;
- int iter = 1;
- while (abs(f(x) - 0.) > eps)
- {
- assert(abs(dfdx(x)) > 1e-9);
- x = x - f(x) / dfdx(x);
- cout << "On step " << iter << " value is " << x << ".\n";
- cout << "Residual is " << -f(x) << ".\n";
- cout << " * * * * \n";
- iter++;
- }
- cout << "(x, f(x) = (" << x << ", " << f(x) << ")\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement