Advertisement
Guest User

Untitled

a guest
May 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. const int A = 3;
  9. const int N1 = 20;
  10. const int N2 = 20;
  11. const double phi = 0;
  12. const double Eps = 0.001;
  13. const double l1 = 1;
  14. const double l2 = 1;
  15. const double h1 = l1 / N1;
  16. const double h2 = l2 / N2;
  17.  
  18. double k1(double x1, double x2) {
  19.     return (x1 * x1 + x2 * x2 + 10);
  20. }
  21. double k2(double x1, double x2) {
  22.     return (5*(x1 * x1 + x2 * x2)  + 20);
  23. }
  24. double p1(double x1, double x2) {
  25.     return 0/*(-2*(x1 + 5*x2))*/;
  26. }
  27. double p2(double x1, double x2) {
  28.     return 0/*(-1*(x1 * x1 + 5*x2*x2))*/;
  29. }
  30. double q(double x1, double x2) {
  31.     return 0 /*(5*x1*x1 + x2*x2 + 30)*/;
  32. }
  33. double func(double x1, double x2){
  34.     return(A * exp(x1 + x2));
  35. }
  36.  
  37. double Af(double *mas[], double *x[] , int i , int j) {
  38.     double T = (k1(x[0][i] + h1/2, x[1][j]) * (mas[i+1][j] - mas[i][j])) / (h1*h1)-
  39.         (k1(x[0][i] - h1/2, x[1][j]) * (mas[i][j] - mas[i-1][j])) / (h1*h1)+
  40.         (k2(x[0][i], x[1][j] + h2/2) * (mas[i][j+1] - mas[i][j])) / (h2*h2)-
  41.         (k2(x[0][i], x[1][j] - h2/2) * (mas[i][j] - mas[i][j-1])) / (h2*h2)+
  42.         (p1(x[0][i], x[1][j]) * (mas[i+1][j] - mas[i-1][j])) / (2*h1)+
  43.         (p2(x[0][i], x[1][j]) * (mas[i][j+1] - mas[i][j-1])) / (2*h2)-
  44.         q(x[0][i], x[1][j]) * mas[i][j];
  45.     cout << T<< endl;
  46.     system("pause");
  47.     return T;
  48. }
  49. int main() {
  50.     setlocale(LC_ALL, "");
  51.     double** x = new double*[2];
  52.     x[0] = new double[N1 + 1];
  53.     x[1] = new double[N2 + 1];
  54.     for (int i(0); i <= N1; i++) {
  55.         x[0][i] = i * h1;
  56.     }
  57.     for (int j(0); j <= N2; j++) {
  58.         x[1][j] = j * h2;
  59.     }
  60.     double** y = new double*[N1+1];
  61.     double** u = new double*[N1+1];
  62.     for(int i(0); i<=N1; i++) {
  63.         y[i] = new double[N2+1];
  64.         u[i] = new double[N2+1];
  65.         for(int j(0); j<=N2; j++) {
  66.             y[i][j] = 0;
  67.             u[i][j] = 0;
  68.         }
  69.     }
  70.  
  71.     for(int i(0); i<=N1; i++) {
  72.         for(int j(1); j<N2; j++) {
  73.             y[i][0] = A * exp(x[0][i]);
  74.             y[i][N2] = A * exp(l2+x[0][i]);
  75.             y[0][j] = A * exp(x[1][j]);
  76.             y[N1][j] = A * exp(l1+x[1][j]);
  77.             u[i][0] = A * exp(x[0][i]);
  78.             u[i][N2] = A * exp(l2+x[0][i]);
  79.             u[0][j] = A * exp(x[1][j]);
  80.             u[N1][j] = A * exp(l1+x[1][j]);
  81.         }
  82.     }
  83.     int count = 0;
  84.     double norm;
  85.     do {
  86.         count++;
  87.         double** r = new double* [N1];
  88.         for(int i(0); i<=N1; i++) {
  89.             r[i] = new double[N2];
  90.         }
  91.         for(int i(1); i<N1; i++) {
  92.             for(int j(1); j<N2; j++) {
  93.                 r[i][j] = Af(u, x, i, j) - phi;
  94.             }
  95.         }
  96.         double scalR = 0;
  97.         for(int i(1); i<N1; i++) {
  98.             for(int j(1); j<N2; j++) {
  99.                 scalR += r[i][j] * r[i][j] * h1*h1;
  100.             }
  101.         }
  102.         double scalA = 0;
  103.         for(int i(1); i<N1; i++) {
  104.             for(int j(1); j<N2; j++) {
  105.                 scalA += Af(r, x, i, j) * r[i][j] * h1*h1;
  106.             }
  107.         }
  108.         double tau = scalR / scalA;
  109.  
  110.         //Y[i][j]
  111.         for(int i(1); i<N1; i++) {
  112.             for(int j(1); j<N2; j++) {
  113.                 y[i][j] = u[i][j] - r[i][j] * tau;
  114.             }
  115.         }
  116.  
  117.         //Norm
  118.         norm = -1;
  119.         for(int i(1); i<N1; i++) {
  120.             for(int j(1); j<N2; j++) {
  121.                 if(fabs(y[i][j]-u[i][j]) > norm) {
  122.                     norm = fabs(y[i][j]-u[i][j]);
  123.                 }
  124.             }
  125.         }
  126.  
  127.         //
  128.         for(int i(1); i<N1; i++) {
  129.             for(int j(1); j<N2; j++) {
  130.                 u[i][j]=y[i][j];
  131.             }
  132.         }
  133.     } while(norm > Eps);
  134.  
  135.     for(int i(0); i<=N1; i++) {
  136.         for(int j(0); j<=N2; j++) {
  137.             cout<< setw(5) << setprecision(3) << func(x[0][i], x[1][j])<< " ";
  138.         }
  139.         cout<<endl<<endl;
  140.     }
  141.     cout << endl;
  142.     cout<<"Count: " << count <<endl;
  143.     for(int i(0); i<=N1; i++) {
  144.         for(int j(0); j<=N2; j++) {
  145.             cout<< setw(5) << setprecision(3) << y[i][j]<< " ";
  146.         }
  147.         cout<<endl<<endl;
  148.     }
  149.     system("pause");
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement