Advertisement
MrGhost75

LAB6_VAR3

Feb 21st, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. //Лабораторная работа №6. Студента группы ПС-19-1 Дьяченко Максима Артёмовича.
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. //[ 1 ; 2.5 ]
  11. double f1(double x) {
  12.     return cos(x / 2) - 2 * sin(1 / x) + 1 / x;
  13. }
  14.  
  15. //[ 0.25 ; 0.45 ]
  16. double f2(double x) {
  17.     return 1.8 * x * x * x * x - sin(10 * x);
  18. }
  19.  
  20. double signature(double x) {
  21.     if (x == 0) return 0;
  22.     else if (x > 0) return 1;
  23.     else return -1;
  24. }
  25.  
  26. double halfDiv(double (*function)(double), double (*sign)(double), double eps, double leftEdge, double rightEdge, int &iter, int maxIter) {
  27.     double dx = 0, middle = 0;
  28.     if (function(leftEdge) == 0)
  29.         return leftEdge;
  30.     else if (function(rightEdge) == 0)
  31.         return rightEdge;
  32.     else {
  33.         while ((rightEdge - leftEdge) > eps && iter < maxIter) {
  34.             dx = (rightEdge - leftEdge) / 2;
  35.             middle = leftEdge + dx;
  36.             if (sign(function(leftEdge)) != sign(function(middle)))
  37.                 rightEdge = middle;
  38.             else
  39.                 leftEdge = middle;
  40.             iter++;
  41.         }
  42.     }
  43.     return middle;
  44. }
  45.  
  46. int main()
  47. {
  48.     setlocale(LC_CTYPE, "rus");
  49.  
  50.     double x, eps, firstLeftEdge, firstRightEdge, secondLeftEdge, secondRightEdge;
  51.     int iterOne = 0, iterTwo = 0, maxIterOne, maxIterTwo;
  52.  
  53.    
  54.     cout << "Введите точность приближенного значения eps: "; cin >> eps;
  55.     cout << "Введите левую границу отрезка: "; cin >> firstLeftEdge;
  56.     cout << "Введите правую границу отрезка: "; cin >> firstRightEdge;
  57.     cout << "Введите максимальное количество итераций: "; cin >> maxIterOne;
  58.    
  59.  
  60.     x = halfDiv(&f1, &signature, eps, firstLeftEdge, firstRightEdge, iterOne, maxIterOne);
  61.     cout << "x = " << x << ";   Количество итераций = " << iterOne << ";\n\n\n";
  62.  
  63.    
  64.     cout << "Введите точность приближенного значения eps: "; cin >> eps;
  65.     cout << "Введите левую границу отрезка: "; cin >> secondLeftEdge;
  66.     cout << "Введите правую границу отрезка: "; cin >> secondRightEdge;
  67.     cout << "Введите максимальное количество итераций: "; cin >> maxIterTwo;
  68.    
  69.  
  70.     x = halfDiv(&f2, &signature, eps, secondLeftEdge, secondRightEdge, iterTwo, maxIterTwo);
  71.     cout << "x = " << x << ";   Количество итераций = " << iterTwo << ";\n\n\n";
  72.  
  73.     system("pause");
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement