Sc3ric

olya

Dec 13th, 2022
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 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);
  18.     return (((a - 1) * (a - 1)) - (x * x)) / (((a + 1) * (a + 1)) - (x * x));
  19. }
  20.  
  21. double f(double x, double e) {
  22.     double z1 = 0;
  23.     double z2 = 1 / (1 - x * x);
  24.     int n = 2;
  25.     double k = myf(n, x);
  26.     double res = z1 + z2;
  27.     while (abs(z2 - z1) >= e)
  28.     {
  29.         z1 = z2;
  30.         z2 = z1 * k;
  31.         cout << z1 << " " << k << " " << z2 << endl;
  32.         res += 1/ (((2*n - 1) * (2*n - 1)) - (x * x));
  33.         n++;
  34.         k = myf(n, x);
  35.     }
  36.     return ((4 * x) / (pi)) - res;
  37. }
  38.  
  39. double F(double x) {
  40.     return tan((pi * (x-(int)x) / 2.0));
  41. }
  42.  
  43.  
  44. int main() {
  45.     setlocale(LC_ALL, "rus");
  46.     double inf = numeric_limits<double>::infinity();
  47.     double x_start, x_end, dx, x_ideal;
  48.     double e;
  49.     cout << "Введите начальное значение x:" << endl;
  50.     cin >> x_start;
  51.     while (!cin) {
  52.         cin.clear();
  53.         cout << "Введите корректное значение:" << endl;
  54.         cin >> x_start;
  55.     }
  56.  
  57.     cout << "Введите конечное значение x:" << endl;
  58.     cin >> x_end;
  59.     while (!cin || x_end == x_start) {
  60.         cin.clear();
  61.         cout << "Введите корректное значение:" << endl;
  62.         cin >> x_end;
  63.     }
  64.  
  65.     cout << "Введите шаг:" << endl;
  66.     cin >> dx;
  67.     while (!cin || ((dx < 0) & (x_start < x_end)) || ((dx > 0) & (x_start > x_end)) || (abs(x_start - x_end) < abs(dx)) || dx == 0) {
  68.         cin.clear();
  69.         cout << "Введите корректное значение:" << endl;
  70.         cin >> dx;
  71.     }
  72.  
  73.     cout << "Введите точность:" << endl;
  74.     cin >> e;
  75.     while (!cin || e <= 0) {
  76.         cin.clear();
  77.         cout << "Введите корректное значение:" << endl;
  78.         cin >> e;
  79.     }
  80.  
  81.     cout << "f(x) = f(x-1)*(((a-2)*(a-2))-(x*x))/((a*a)-(x*x))), где a=2n-1" << "\n";
  82.     cout << "F(x) = tg((pi*x)/2) " << "\n";
  83.  
  84.     cout << " _________________________________________________________________________________________________" << endl;
  85.     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;
  86.     cout << " _________________________________________________________________________________________________" << endl;
  87.     for (double i = x_start; i <= x_end + 0.000001; i += dx) {
  88.         double f1 = f(i, e);
  89.         double F1 = F(i);
  90.         double del;
  91.         if ((f1 == inf && F1 == inf) || f1 == -inf && F1 == inf) {
  92.             del = INFINITY;
  93.         }
  94.         else {
  95.             del = sqrt(abs(f1 * f1 - F1 * F1));
  96.         }
  97.  
  98.         cout << fixed << setprecision(6);
  99.         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;
  100.         cout << " _________________________________________________________________________________________________" << endl;
  101.     }
  102.     cout << "Введите значение x_ideal:" << endl;
  103.     cin >> x_ideal;
  104.     while (!cin) {
  105.         cin.clear();
  106.         scanf_s("%*[^\n]");
  107.         cout << "Введите корректное значение:" << endl;
  108.         cin >> x_ideal;
  109.     }
  110.     cout << " _________________________________________________________________________________________________" << endl;
  111.     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;
  112.     cout << " _________________________________________________________________________________________________" << endl;
  113.  
  114.     for (double i = 0.1; i >= 0.0000001; i = i / 10) {
  115.         double f1 = f(x_ideal, i);
  116.         double F1 = F(x_ideal);
  117.         double del;
  118.         if (f1 == inf && F1 == inf) {
  119.             del = INFINITY;
  120.         }
  121.         else {
  122.             del = sqrt(abs(f1 * f1 - F1 * F1));
  123.         }
  124.         cout << fixed << setprecision(6);
  125.         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;
  126.         cout << " _________________________________________________________________________________________________" << endl;
  127.     }
  128. }
  129.  
  130.  
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment