Advertisement
noctual

Gauss / Fredholm / Volterra

Apr 12th, 2021
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. const int n = 10;
  7. const double Lambda = M_PI;
  8. const double a = 0;
  9. const double b = 1;
  10.  
  11. double K(double x, double s) {
  12.     return (1 - x) * sin(2 * M_PI * s);
  13. }
  14.  
  15. double f(double x) {
  16.     return (1 - x) / 2;
  17. }
  18.  
  19. double* Gauss(double** a, double* y, int n)
  20. {
  21.     double* x, max;
  22.     int k, index;
  23.     const double eps = 0.000001;
  24.     x = new double[n];
  25.     k = 0;
  26.     for (; k < n; k++)
  27.     {
  28.         max = abs(a[k][k]);
  29.         index = k;
  30.         for (int i = k + 1; i < n; i++)
  31.         {
  32.             if (abs(a[i][k]) > max)
  33.             {
  34.                 max = abs(a[i][k]);
  35.                 index = i;
  36.             }
  37.         }
  38.         if (max < eps) return 0;
  39.         for (int j = 0; j < n; j++)
  40.         {
  41.             double temp = a[k][j];
  42.             a[k][j] = a[index][j];
  43.             a[index][j] = temp;
  44.         }
  45.         double temp = y[k];
  46.         y[k] = y[index];
  47.         y[index] = temp;
  48.         for (int i = k; i < n; i++)
  49.         {
  50.             double temp = a[i][k];
  51.             if (abs(temp) < eps) continue;
  52.             for (int j = 0; j < n; j++)
  53.                 a[i][j] = a[i][j] / temp;
  54.             y[i] = y[i] / temp;
  55.             if (i == k)  continue;
  56.             for (int j = 0; j < n; j++)
  57.                 a[i][j] = a[i][j] - a[k][j];
  58.             y[i] = y[i] - y[k];
  59.         }
  60.        
  61.     }
  62.     for (k = n - 1; k >= 0; k--)
  63.     {
  64.         x[k] = y[k];
  65.         for (int i = 0; i < k; i++)
  66.             y[i] = y[i] - a[i][k] * x[k];
  67.     }
  68.     return x;
  69. }
  70.  
  71. double* Fredholm() {
  72.     double h = (b - a) / n;
  73.     double** mass = new double* [n + 1];
  74.     double* y = new double[n];
  75.     double xi = a;
  76.     for (int i = 0; i <= n; i++, xi += h) {
  77.         mass[i] = new double[n + 2];
  78.         double xj = a;
  79.         for (int j = 0; j <= n; j++, xj += h) {
  80.             mass[i][j] = -Lambda * (j != 0 && j != n ? h : h / 2) * K(xi, xj);
  81.         }
  82.         mass[i][i]++;
  83.         y[i] = f(xi);
  84.     }
  85.  
  86.     double *y2 = Gauss(mass, y, n+1);
  87.  
  88.     for (int i = 0; i <= n; i++)
  89.         delete[] mass[i];
  90.     delete[] mass;
  91.  
  92.     return y2;
  93. }
  94.  
  95. double* Volterra() {
  96.     double h = (b - a) / n;
  97.     double* y = new double[n + 1];
  98.  
  99.     double xi = a;
  100.     y[0] = f(xi);
  101.     xi += h;
  102.  
  103.     y[1] = (f(xi) + Lambda * h * K(xi, a) * y[0] / 2) / (1 - Lambda * h * K(xi, xi) / 2);
  104.     xi += h;
  105.    
  106.     for (int i = 2; i <= n; i++, xi += h) {
  107.         double xj = a, summ = 0;
  108.         for (int j = 0; j <= i - 1; j++, xj += h)
  109.             summ = summ + K(xi, xj) * y[j];
  110.         y[i] = (f(xi) + Lambda * h * (K(xi, a) * y[0] + 2 * summ) / 2) / (1 - Lambda * h * K(xi, xi) / 2);
  111.     }
  112.  
  113.     return y;
  114. }
  115.  
  116. int main() {
  117.     setlocale(LC_ALL, "RUS");
  118.     cout << "Уравнение Фредгольма второго рода:" << endl;
  119.     double* res = Fredholm();
  120.     for (int i = 0; i <= n; i++)
  121.         cout << floor(res[i]*100000000) / 100000000 << endl;
  122.     cout << endl << "Уравнение Вольтерра второго рода:" << endl;
  123.     double* res2 = Volterra();
  124.     for (int i = 0; i <= n; i++)
  125.         cout << floor(res2[i] * 100000000) / 100000000 << endl;
  126.     delete[] res;
  127.     delete[] res2;
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement