Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- double f(double x)
- {
- return -3 * x / (pow(x, 2) + 1);
- }
- double df(double x)
- {
- return 3 * (pow(x, 2) - 1) / pow((pow(x, 2) + 1), 2);
- }
- double d2f(double x)
- {
- return -6 * x * (pow(x, 2) - 3) / pow((pow(x, 2) + 1), 3);
- }
- int main()
- {
- double x0, x, eps;
- int i = 0;
- cout << "x0: ";
- cin >> x0;
- cout << "eps: ";
- cin >> eps;
- x = x0;
- cout << endl;
- do {
- cout << "Iteration: " << i << " x = " << x << " f(x) = " << f(x) << " df(x) = " << df(x) << endl;
- x = x - df(x) / d2f(x);
- i++;
- } while (fabs(df(x)) > eps);
- cout << "Iteration: " << i << " x = " << x << " f(x) = " << f(x) << " df(x) = " << df(x) << endl;
- cout << endl;
- cout << "Result: " << "xmin = " << x << " fmin = " << f(x) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement