Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // methodHord.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- using namespace std;
- double f(double x)
- {
- // [-1, 1] - отрезок на котором ищем корень
- return 3 * x - cos(x) -1;
- //return sqrt(x) - 2;
- }
- double first_derivate(double x)
- {
- double h = 0.01; // шаг производной
- return (f(x) - f(x - h)) / h;
- }
- double second_derivative(double x)
- {
- double h = 0.01; // шаг производной
- return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h); // вторая производная
- }
- double methodHord(double(*funct)(double), int a, int b, double eps)
- {
- double c;
- double xNext;
- double x0;
- cout << "\t\t" << f(a) * second_derivative(a) << endl;
- cout << "\t\t" << f(b) * second_derivative(b) << endl;
- if (f(a) * second_derivative(a) > 0)
- {
- c = a;
- x0 = b;
- //cout << "c>>> " << c << endl;
- }
- else if (f(b) * second_derivative(b) > 0)
- {
- c = b;
- x0 = a;
- //cout << "c>>> " << c << endl;
- }
- xNext = x0 + (c - x0) / (1 - f(c) / f(x0));
- while (fabs(xNext - x0) <= eps)
- {
- x0 = xNext;
- xNext = x0 + (c - x0) / (1 - f(c) / f(x0));
- }
- return xNext;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- //cout << "Вторая производная >>> " << second_derivative(20) << endl;
- int a, b;
- cout << "Начало отрезка (a) \n>>> ";
- cin >> a;
- cout << "Конец отрезка (b) \n>>> ";
- cin >> b;
- double eps = 0.00001;
- double m1 = fabs(first_derivate(a)), M1 = fabs(first_derivate(a));
- int sec_a = a;
- for (sec_a; sec_a <= b; sec_a++)
- {
- if (m1 > fabs(first_derivate(sec_a)))
- {
- m1 = fabs(first_derivate(sec_a));
- }
- if (M1 < fabs(first_derivate(sec_a)))
- {
- M1 = fabs(first_derivate(sec_a));
- }
- }
- //cout << m1 << endl << M1 << endl;
- if (M1 <= 2 * m1)
- {
- double res = methodHord(f, a, b, eps);
- cout << res << endl;
- cout << f(res) << endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement