Tomki

Optima3Tasks

Mar 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. // Lst3Tasks.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <math.h>
  8. #include <Windows.h>
  9.  
  10. using namespace std;
  11.  
  12. double f(double x)
  13. {
  14.     return (3 * cos(x)*x);
  15. }
  16.  
  17. void doubleDots(double left, double right)
  18. {
  19.     double eps = 0.07f;
  20.     double a = left; double b = right;
  21.     double x1, x2;
  22.     int count = 0;
  23.     x1 = (a + b - eps) / 2;
  24.     x2 = (a + b + eps) / 2;
  25.     while (count < 10)
  26.     {
  27.         count++;
  28.         if (f(x1) < f(x2))
  29.             {
  30.  
  31.                 b = x2;
  32.                 x1 = (a + b - eps) / 2;
  33.                 x2 = (a + b + eps) / 2;
  34.             }
  35.         else
  36.             {
  37.                 a = x1;
  38.                 x1 = (a + b - eps) / 2;
  39.                 x2 = (a + b + eps) / 2;
  40.             }
  41.     }
  42.     cout << "xmin = " << (a + b) / 2 << "\nf(xmin) = " << f((a + b) / 2) << "\n";
  43. }
  44. void fibonaci(double left, double right)
  45. {
  46.     int arr[13] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 };
  47.     double a, b, eps, x1, x2;
  48.     eps = 0.01f; a = left; b = right;
  49.     x1 = ((b - a) * 233 - eps) / 377 + a;
  50.     x2 = a - x1 + b;
  51.     int i = 12; int j = 11;
  52.     while (i)
  53.     {
  54.         if (f(x2) > f(x1))
  55.         {
  56.             if (x2 > x1)
  57.             {
  58.                 b = x2;
  59.                 x1 = ((b - a) * arr[j] - eps) / arr[i] + a;
  60.                 x2 = a - x1 + b;
  61.             }
  62.             else
  63.             {
  64.                 a = x2;
  65.                 x1 = ((b - a) * arr[j] - eps) / arr[i] + a;
  66.                 x2 = a - x1 + b;
  67.             }
  68.         }
  69.         else
  70.         {
  71.             if (x1 > x2)
  72.             {
  73.                 b = x1;
  74.                 x1 = x2;
  75.                 x2 = a - x1 + b;
  76.             }
  77.             else
  78.             {
  79.                 a = x1;
  80.                 x1 = x2;
  81.                 x1 = ((b - a) * arr[j] - eps) / arr[i] + a;
  82.             }
  83.         }
  84.         i--; j--;
  85.     }
  86.     if (!i)
  87.     {
  88.         cout << "xmin = " << (a + b) / 2 << "\nf(xmin) = " << f((a + b) / 2) << "\n";
  89.         return;
  90.     }
  91. }
  92. void kvInterpol(double left, double right)
  93. {
  94.     double a;
  95.     a = (right - left) / 2 + (right - left) / 6.5f + left;
  96.     double x1, x2, x3, y1, y2, y3;
  97.     double h = 0.0005; double eps = 0.07; double xmin;
  98.     x1 = a - h;
  99.     x2 = a;
  100.     x3 = a + h;
  101.     y1 = f(x1);
  102.     y2 = f(x2);
  103.     y3 = f(x3);
  104.  
  105.     xmin = 0.5f * ((x2 * x2 - x3 * x3) * f(x1) + (x3 * x3 - x1 * x1) * f(x2) + (x1 * x1 - x2 * x2) * f(x3)) /
  106.         ((x2 - x3) * f(x1) + (x3 - x1) * f(x2) + (x1 - x2) * f(x3));
  107.  
  108.     while (abs(f(a) - f(xmin)) >= eps)
  109.     {
  110.         a = xmin;
  111.         x1 = a - h;
  112.         x2 = a;
  113.         x3 = a + h;
  114.         y1 = f(x1);
  115.         y2 = f(x2);
  116.         y3 = f(x3);
  117.         xmin = 0.5f * ((x2 * x2 - x3 * x3) * f(x1) + (x3 * x3 - x1 * x1) * f(x2) + (x1 * x1 - x2 * x2) * f(x3)) /
  118.             ((x2 - x3) * f(x1) + (x3 - x1) * f(x2) + (x1 - x2) * f(x3));
  119.     }
  120.  
  121.     cout << "xmin = " << xmin << "\nf(xmin) = " << f(xmin) << "\n";
  122. }
  123.  
  124. int main()
  125. {
  126.     double left, right;
  127.     cout << "f(x) = 3*cos(x)*x\n";
  128.     cout << "Input the left edge\n"; cin >> left;
  129.     cout << "\nInput the right edge\n"; cin >> right;
  130.     if (left > right)
  131.     {
  132.         double *buf = new double;
  133.         *buf = left;
  134.         left = right;
  135.         right = *buf;
  136.         delete buf;
  137.     }
  138.     doubleDots(left, right);
  139.     fibonaci(left, right);
  140.     kvInterpol(left, right);
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment