Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // methodHord.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. double f(double x)
  12. {
  13.     // [-1, 1] - отрезок на котором ищем корень
  14.     return 3 * x - cos(x) -1;
  15.     //return sqrt(x) - 2;
  16. }
  17.  
  18. double first_derivate(double x)
  19. {
  20.     double h = 0.01; // шаг производной
  21.     return (f(x) - f(x - h)) / h;
  22. }
  23.  
  24. double second_derivative(double x)
  25. {
  26.     double h = 0.01; // шаг производной
  27.     return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h); // вторая производная
  28. }
  29. double methodHord(double(*funct)(double), int a, int b, double eps)
  30. {
  31.     double c;
  32.     double xNext;
  33.     double x0;
  34.     cout << "\t\t" << f(a) * second_derivative(a) << endl;
  35.     cout << "\t\t" << f(b) * second_derivative(b) << endl;
  36.     if (f(a) * second_derivative(a) > 0)
  37.     {
  38.         c = a;
  39.         x0 = b;
  40.         //cout << "c>>> " << c  << endl;
  41.     }
  42.     else if (f(b) * second_derivative(b) > 0)
  43.     {
  44.         c = b;
  45.         x0 = a;
  46.         //cout << "c>>> " << c << endl;
  47.     }
  48.     xNext = x0 + (c - x0) / (1 - f(c) / f(x0));
  49.     while (fabs(xNext - x0) <= eps)
  50.     {
  51.         x0 = xNext;
  52.         xNext = x0 + (c - x0) / (1 - f(c) / f(x0));
  53.     }
  54.     return xNext;
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60.     setlocale(LC_ALL, "Russian");
  61.     //cout << "Вторая производная >>> " << second_derivative(20) << endl;
  62.     int a, b;
  63.     cout << "Начало отрезка (a) \n>>> ";
  64.     cin >> a;
  65.     cout << "Конец отрезка (b) \n>>> ";
  66.     cin >> b;
  67.     double eps = 0.00001;
  68.     double m1 = fabs(first_derivate(a)), M1 = fabs(first_derivate(a));
  69.     int sec_a = a;
  70.     for (sec_a; sec_a <= b; sec_a++)
  71.     {
  72.         if (m1 > fabs(first_derivate(sec_a)))
  73.         {
  74.             m1 = fabs(first_derivate(sec_a));
  75.         }
  76.         if (M1 < fabs(first_derivate(sec_a)))
  77.         {
  78.             M1 = fabs(first_derivate(sec_a));
  79.         }
  80.     }
  81.     //cout << m1 << endl << M1 << endl;
  82.  
  83.     if (M1 <= 2 * m1)
  84.     {
  85.     double res = methodHord(f, a, b, eps);
  86.     cout << res << endl;
  87.     cout << f(res) << endl;
  88.     }
  89.     system("pause");
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement