Advertisement
MargaritaOwl

chislMethods

Mar 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.48 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<ctime>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7. double myfunc(double t);
  8. double myfunc1(double t);
  9. double myfunc2(double t);
  10. void coutPromFirst(int n, double c, double a, double b);
  11. void coutPromSecond(int n, double x, char c);
  12. void coutPromThird(int n, double x, double xLast, double sigma, char c);
  13. void coutPromSecond(int n, double x, char c);
  14. void coutPromFourth(int n, double x, double xLast, double gamma, char c);
  15. int sign(double t);
  16. double min(double first, double second);
  17. double max(double first, double second);
  18. void HalfDis();
  19. void SimpleIteration();
  20. void Newton();
  21. void ModifyNewton();
  22. void Chords();
  23. double TransFunc(double t);
  24. double TransFunc1(double t);
  25. double TransFunc2(double t);
  26. void TransModifyNewton(double a, double b);
  27. void TransNewton(double a, double b);
  28. void main()
  29. {
  30.     setlocale(LC_ALL, "rus");
  31.     HalfDis();
  32.     SimpleIteration();
  33.     Newton();
  34.     ModifyNewton();
  35.     Chords();
  36.     cout << "========================================================================"<<endl;
  37.     TransModifyNewton(19,20);
  38.     TransModifyNewton(18, 18.5);
  39.     TransModifyNewton(0.1, 0.2);
  40.     TransModifyNewton(-0.01, 0.1);
  41.     TransModifyNewton(-15, -15.3);
  42.     TransNewton(-16.2, -16.5);
  43.     TransModifyNewton(-21, -21.1);
  44.     TransNewton(-22.5, -23);
  45.     system("pause");
  46. }
  47. void HalfDis()
  48.     {
  49.        
  50.         double a = -1;
  51.         double b = 0;
  52.         double  c, eps = 0.00001;
  53.         cout << "1) Метод половинного деления." << endl;
  54.  
  55.         int iterator = 0;
  56.         while (fabs(b - a) >= eps)
  57.         {
  58.             c = (a + b) / 2.;
  59.             coutPromFirst(iterator, c, a, b);
  60.             if (myfunc(a)*myfunc(c) < 0)
  61.                 b = c;
  62.             else
  63.                 a = c;
  64.             iterator++;
  65.         }
  66.  
  67.         cout << "Уточненный корень уравнения: x=" << setprecision(5) << fixed << c << endl;
  68.         cout << "Итераций: " << iterator << endl;
  69.     }
  70. void SimpleIteration()
  71. {
  72.    
  73.     double a = -1.;
  74.     double b = 0.;
  75.     double  eps = 0.00001;
  76.     double x, xLast, alpha, m1, M1;
  77.     cout << "2)Метод простой итерации." << endl;
  78.     x = xLast = 2;
  79.    
  80.     if (sign(myfunc2(a)) == sign(myfunc2(b)))
  81.     {
  82.         M1 = fabs(myfunc1(max(a, b)));
  83.         m1 = fabs(myfunc1(min(a, b)));
  84.     }
  85.     else
  86.     {
  87.         M1 = m1 = fabs(myfunc1(a));
  88.         for (double count = a; count <= b; count += 0.05)
  89.         {
  90.             if (fabs(myfunc1(count)) > M1)
  91.                 M1 = fabs(myfunc1(count));
  92.             if (fabs(myfunc1(count)) < m1)
  93.                 m1 = fabs(myfunc1(count));
  94.         }
  95.     }
  96.     alpha = 2. / (m1 + M1);
  97.     if (!(myfunc1(a) > 0 && myfunc1(b) > 0))
  98.         alpha *= (-1);
  99.     int n;
  100.     for (n = 0; fabs(myfunc(x)) / m1 >= eps; n++)
  101.     {
  102.         coutPromSecond(n, x, '-');
  103.         xLast = x;
  104.         x = xLast - alpha*myfunc(xLast);
  105.     }
  106.     coutPromSecond(n, x, '+');
  107.     cout << "Уточненный корень уравнения: x=" << setprecision(5) << fixed << x << endl;
  108.     cout << "Итераций:" << n << endl;
  109. }
  110.  
  111. void Newton()
  112. {
  113.     double a = -1.;
  114.     double b = 0.;
  115.     double  eps = 0.00001, x0, x1;
  116.     cout << "3) Метод касательных." << endl;
  117.     int iterator = 0;
  118.    
  119.     x0 = a;
  120.     x1 = x0 - myfunc(x0) / myfunc1(x0);
  121.     while (fabs(x1 - x0) > eps)
  122.     {
  123.         coutPromThird(iterator, x1, x0, eps, '-');
  124.         x0 = x1;
  125.         x1 = x0 - myfunc(x0) / myfunc1(x0);
  126.         iterator++;
  127.     }
  128.     coutPromThird(iterator++, x1, x0, eps, '+');
  129.    
  130.     cout << "Уточненный корень уравнения: x=" << setprecision(5) << fixed << x1 << endl;
  131.     cout << "Итераций:" << iterator << endl;
  132. }
  133.  
  134. void ModifyNewton()
  135. {
  136.     double a = -1.;
  137.     double b = 0.;
  138.     double  eps = 0.00001, x0;
  139.     double x, xLast, m1;
  140.     int n = 0;
  141.     cout << "3.1) Модифицированный метод касательных." << endl;
  142.     xLast = 0;
  143.     m1 = min(fabs(myfunc1(a)), fabs(myfunc1(b)));
  144.  
  145.     for (x = a; myfunc(x)*myfunc2(x) <= 0; x += 0.05);
  146.     x0 = x;
  147.  
  148.     for (n = 0; (fabs(myfunc(x)) / m1) >= eps; n++)
  149.     {
  150.         coutPromSecond(n, x, '-');
  151.         xLast = x;
  152.         x = xLast - (myfunc(xLast) / myfunc1(x0));
  153.     }
  154.  
  155.     coutPromSecond(n++, x, '+');
  156.     cout << "Уточненный корень уравнения: x=" << setprecision(6) << fixed << x << endl;
  157.     cout << "Итераций:" << n << endl;
  158. }
  159. void Chords()
  160. {
  161.     double a = -1;
  162.     double b = 0.;
  163.     double  eps = 0.00001;
  164.     double x, xLast, M1, m1, gamma;
  165.     int n = 0;
  166.     cout << "4) Метод хорд." << endl;
  167.  
  168.     M1 = fabs(myfunc1(max(a, b)));
  169.     m1 = fabs(myfunc1(min(a, b)));
  170.     gamma = (m1 * eps) / (M1 - m1);
  171.    
  172.     if (myfunc(a)*myfunc2(a) < 0)
  173.         for (xLast = 1, x = a; fabs(x - xLast) >= fabs(gamma);n++)
  174.         {
  175.             coutPromFourth(n, x, xLast, gamma, '-');
  176.             xLast = x;
  177.             x = xLast - (myfunc(xLast) / (myfunc(b) - myfunc(xLast))*(b - xLast));
  178.         }
  179.     else
  180.         for (xLast = 1, x = b; fabs(x - xLast) >= fabs(gamma);n++)
  181.         {
  182.             coutPromFourth(n, x, xLast, gamma, '-');
  183.             xLast = x;
  184.             x = xLast - (myfunc(xLast) / (myfunc(a) - myfunc(xLast))*(a - xLast));
  185.         }
  186.     coutPromFourth(n++, x, xLast, gamma, '+');
  187.     cout << "Уточненный корень уравнения: x=" << setprecision(6) << fixed << x << endl;
  188.     cout << "Итераций:" << n << endl;
  189. }
  190.  
  191.  
  192. double myfunc(double t)
  193. {
  194.     double res;
  195.     res = t*t*t - t*t + 2 * t + 3;
  196.     return res;
  197. }
  198.  
  199. double myfunc1(double t)
  200. {
  201.     double res;
  202.     res = 3*t*t - 2*t + 2;
  203.     return res;
  204. }
  205.  
  206. double myfunc2(double t)
  207. {
  208.     double res;
  209.     res = 6 * t - 2;
  210.     return res;
  211. }
  212.  
  213. int sign(double t)
  214. {
  215.     if (t > 0)
  216.         return 1;
  217.     else
  218.         if (t < 0)
  219.             return -1;
  220.     return 0;
  221. }
  222. double min(double first, double second)
  223. {
  224.     if (first < second)
  225.         return first;
  226.     return second;
  227. }
  228. double max(double first, double second)
  229. {
  230.     if (first > second)
  231.         return first;
  232.     return second;
  233. }
  234.  
  235. void coutPromFirst(int n, double c, double a, double b)
  236. {
  237.     if (n == 0)
  238.         cout << setw(5) << "n" << setw(10) << "a" << setw(10) << "x" << setw(10) << "b" << setw(15) << "f(a)" << setw(15) << "f(x)" << setw(12) << "f(b)" << endl << endl;
  239.     cout << setw(5) << n + 1 << setw(10) << a << setw(10) << c << setw(10) << b << setw(15) << myfunc(a) << setw(15) << myfunc(c) << setw(12) << myfunc(b) << endl;
  240. }
  241. void coutPromSecond(int n, double x, char c)
  242. {
  243.     double eps = 0.00001;
  244.     if (n == 0)
  245.         cout << setw(5) << "n" << setw(10) << "x" << setw(15) << "|f(x)|/m_1" << setw(25) << "|f(x)|/m_1 < " << eps << endl << endl;
  246.     cout << setw(5) << n + 1 << setw(10) << x << setw(15) << fabs(myfunc(x)) / 6 << setw(25) << c << endl;
  247. }
  248. void coutPromThird(int n, double x, double xLast, double sigma, char c)
  249. {
  250.     if (n == 0)
  251.         cout << setw(5) << "n" << setw(12) << "x_n" << setw(20) << "|x_n - x_(n-1)|" << setw(20) << "|x_n - x_(n-1)| < " << sigma << endl << endl;
  252.     cout << setw(5) << n + 1 << setw(7) << x << setw(14);
  253.     if (n == 0)
  254.         cout << "---";
  255.     else
  256.         cout << fabs(x - xLast);
  257.     cout << setw(25) << c << endl;
  258. }
  259. void coutPromFourth(int n, double x, double xLast, double gamma, char c)
  260. {
  261.     cout.precision(8);
  262.     coutPromThird(n, x, xLast, gamma, c);
  263.     cout.precision(6);
  264. }
  265. double TransFunc(double t)
  266. {
  267.     return(pow((t - 1), 3)*cos(t) + 1 - 14 * pow(t, 2));
  268. }
  269. double TransFunc1(double t)
  270. {
  271.     return(-28 * t - pow((t - 1), 3)*sin(t) + 3 * pow((t - 1), 2)*cos(t));
  272. }
  273. double TransFunc2(double t)
  274. {
  275.     return(-pow((t - 1), 3)*cos(t) - 6 * pow((t - 1), 2)*sin(t) + 6 * (t - 1)*cos(t) - 28);
  276.  
  277. }
  278.  
  279. void TransModifyNewton(double a, double b)
  280. {
  281.    
  282.     //double a;
  283.     //double b;
  284.     double  eps = 0.00001, x0;
  285.     double x, xLast, m1;
  286.     int n = 0;
  287.     cout << "3.1) Модифицированный метод касательных." << endl;
  288.     /*cout << "Введите начало и конец отрезка:" << endl;
  289.     cout << "a="; cin >> a;
  290.     cout << "b="; cin >> b;*/
  291.     xLast = 0;
  292.     m1 = min(fabs(TransFunc1(a)), fabs(TransFunc1(b)));
  293.  
  294.     for (x = a; TransFunc(x)*TransFunc2(x) <= 0; x += 0.05);
  295.     x0 = x;
  296.  
  297.     for (n = 0; (fabs(TransFunc(x)) / m1) >= eps; n++)
  298.     {
  299.         coutPromSecond(n, x, '-');
  300.         xLast = x;
  301.         x = xLast - (TransFunc(xLast) / TransFunc1(x0));
  302.     }
  303.  
  304.     coutPromSecond(n++, x, '+');
  305.     cout << "Уточненный корень уравнения: x=" << setprecision(5) << fixed << x << endl;
  306.     cout << "Итераций:" << n << endl;
  307. }
  308. void TransNewton(double a, double b)
  309. {
  310.     double  eps = 0.00001, x0, x1;
  311.     cout << "3) Метод касательных." << endl;
  312.     int iterator = 0;
  313.  
  314.     x0 = a;
  315.     x1 = x0 - TransFunc(x0) / TransFunc1(x0);
  316.     while (fabs(x1 - x0) > eps)
  317.     {
  318.         coutPromThird(iterator, x1, x0, eps, '-');
  319.         x0 = x1;
  320.         x1 = x0 - TransFunc(x0) / TransFunc1(x0);
  321.         iterator++;
  322.     }
  323.     coutPromThird(iterator++, x1, x0, eps, '+');
  324.  
  325.     cout << "Уточненный корень уравнения: x=" << setprecision(5) << fixed << x1 << endl;
  326.     cout << "Итераций:" << iterator << endl;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement