Sc3ric

lab4-olya

Dec 13th, 2022 (edited)
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #define LC_CTYPE  
  8. #define _USE_MATH_DEFINES
  9. #include<cmath>
  10. # define M_PI 3.14159265358979323846
  11.  
  12. using namespace std;
  13. double pi = 3.14159265358979323846;
  14.  
  15.  
  16. double myf(int n, double x) {
  17.     int a = (2 * n) - 1;
  18.     return (((a - 2) * (a - 2)) - (x * x)) / ((a * a) - (x * x));
  19. }
  20.  
  21. double f(double x, double e) {
  22.     double z1 = 1 / (1 - x * x);
  23.     double z2 = 0;
  24.     int n = 2;
  25.     double k = myf(n, x);
  26.     double res = z1;
  27.     while (abs(z2 - z1 * k) >= e)
  28.     {
  29.         z2 = z1 * k;
  30.         res += z1 * k;
  31.         z1 = z2;
  32.         n++;
  33.         k = myf(n, x);
  34.     }
  35.     if (abs(res) > 1e10) {
  36.         return INFINITY;
  37.     }
  38.     return ((4 * x) / (pi)) * res;
  39. }
  40. /*}
  41. else {
  42.     return INFINITY;
  43. }
  44. }
  45. }
  46. else {
  47.     while (abs(z2 - z1 * k) >= e)
  48.     {
  49.         z2 = z1 * k;
  50.         res += z1 * k;
  51.         z1 = z2;
  52.         n++;
  53.         k = myf(n, x);
  54.     }
  55.     return ((4 * x) / (M_PI)) - res;
  56. }
  57. }*/
  58.  
  59. double F(double x) {
  60.     double abs_x = abs(x);
  61.     int abs_int_x = int(abs_x + 1e-10);
  62.     if (abs(abs_x - abs_int_x) < 1e-10) {
  63.         if (abs(abs_int_x % 2) == 0) {
  64.             return (tan((pi * x) / 2));
  65.         }
  66.         else {
  67.             return INFINITY;
  68.         }
  69.     }
  70.     else {
  71.         return  (tan((pi * x) / 2));
  72.     }
  73. }
  74.  
  75.  
  76. int main() {
  77.     setlocale(LC_ALL, "rus");
  78.     double inf = numeric_limits<double>::infinity();
  79.     double x_start, x_end, dx, x_ideal;
  80.     double e;
  81.     cout << "Введите начальное значение x:" << endl;
  82.     cin >> x_start;
  83.     while (!cin) {
  84.         cin.clear();
  85.         cout << "Введите корректное значение:" << endl;
  86.         cin >> x_start;
  87.     }
  88.  
  89.     cout << "Введите конечное значение x:" << endl;
  90.     cin >> x_end;
  91.     while (!cin || x_end == x_start) {
  92.         cin.clear();
  93.         cout << "Введите корректное значение:" << endl;
  94.         cin >> x_end;
  95.     }
  96.  
  97.     cout << "Введите шаг:" << endl;
  98.     cin >> dx;
  99.     while (!cin || ((dx < 0) & (x_start < x_end)) || ((dx > 0) & (x_start > x_end)) || (abs(x_start - x_end) < abs(dx)) || dx == 0) {
  100.         cin.clear();
  101.         cout << "Введите корректное значение:" << endl;
  102.         cin >> dx;
  103.     }
  104.  
  105.     cout << "Введите точность:" << endl;
  106.     cin >> e;
  107.     while (!cin || e <= 0) {
  108.         cin.clear();
  109.         cout << "Введите корректное значение:" << endl;
  110.         cin >> e;
  111.     }
  112.  
  113.     cout << "f(x) = f(x-1)*(((a-2)*(a-2))-(x*x))/((a*a)-(x*x))), где a=2n-1" << "\n";
  114.     cout << "F(x) = tg((pi*x)/2) " << "\n";
  115.  
  116.     cout << " _________________________________________________________________________________________________" << endl;
  117.     cout << "|" << setw(12.5) << "x" << setw(12.5) << "|" << setw(13.5) << "f(x)" << setw(11.5) << "|" << setw(14) << "F(x)" << setw(11) << "|" << setw(15) << "delta" << setw(10) << "|" << endl;
  118.     cout << " _________________________________________________________________________________________________" << endl;
  119.     for (double i = x_start; i <= x_end + 0.000001; i += dx) {
  120.         double f1 = f(i, e);
  121.         double F1 = F(i);
  122.         double del;
  123.         if ((f1 == inf && F1 == inf) || f1 == -inf && F1 == inf) {
  124.             del = INFINITY;
  125.         }
  126.         else {
  127.             del = sqrt(abs(f1 * f1 - F1 * F1));
  128.         }
  129.  
  130.         cout << fixed << setprecision(6);
  131.         cout << "|" << setw(12.5) << i << setw(12.5) << "|" << setw(13.5) << f1 << setw(11.5) << "|" << setw(14) << F1 << setw(11) << "|" << setw(15) << del << setw(10) << "|" << endl;
  132.         cout << " _________________________________________________________________________________________________" << endl;
  133.     }
  134.     cout << "Введите значение x_ideal:" << endl;
  135.     cin >> x_ideal;
  136.     while (!cin) {
  137.         cin.clear();
  138.         scanf_s("%*[^\n]");
  139.         cout << "Введите корректное значение:" << endl;
  140.         cin >> x_ideal;
  141.     }
  142.     cout << " _________________________________________________________________________________________________" << endl;
  143.     cout << "|" << setw(12.5) << "eps" << setw(12.5) << "|" << setw(13.5) << "f(x_ideal)" << setw(11.5) << "|" << setw(14) << "F(x_ideal)" << setw(11) << "|" << setw(15) << "delta" << setw(10) << "|" << endl;
  144.     cout << " _________________________________________________________________________________________________" << endl;
  145.  
  146.     for (double i = 0.1; i >= 0.0000001; i = i / 10) {
  147.         double f1 = f(x_ideal, i);
  148.         double F1 = F(x_ideal);
  149.         double del;
  150.         if (f1 == inf && F1 == inf) {
  151.             del = INFINITY;
  152.         }
  153.         else {
  154.             del = sqrt(abs(f1 * f1 - F1 * F1));
  155.         }
  156.         cout << fixed << setprecision(6);
  157.         cout << "|" << setw(12.5) << i << setw(12.5) << "|" << setw(13.5) << f1 << setw(11.5) << "|" << setw(14) << F1 << setw(11) << "|" << setw(15) << del << setw(10) << "|" << endl;
  158.         cout << " _________________________________________________________________________________________________" << endl;
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment