Advertisement
Guest User

chenged proga

a guest
Mar 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.10 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<vector>
  6. #include<string>
  7. #include<sstream>
  8. #include<fstream>
  9.  
  10. using namespace std;
  11.  
  12.  //const double alpha = 0;
  13.  const double N = 20;
  14.  const double h = 1.0 / N;
  15.  const double PI = 3.141592653589793238463;
  16.  const double eps = 0.01;
  17.  //const double tau = h * h / sin(PI * h);
  18.  const double tau = h;
  19.  
  20.  double alpha(double x, double y) {
  21.      //return 0;
  22.      //return x;
  23.      if(x == 0) return sin(PI * y);
  24.      else if(y == 0) return sin(PI * x);
  25.      else return 0;
  26.      
  27.  }
  28.  double f(double x, double y)
  29.  {
  30.      return x * y;
  31.      //return 2 * PI * PI * sin(PI * x) * sin(PI * y);
  32.      //return x*y;
  33.      
  34.      //return 2 * PI * sin(PI * x) * sin(PI * y) - y * PI * cos(PI * x) * sin(PI * y) - x * PI * cos(PI * y) * sin(PI * x);
  35.  }
  36.  double f(int i, int j)
  37.  {
  38.      return f((double)i * h, (double)j * h);
  39.  }
  40.  double Real_U(double x, double y)
  41.  {
  42.      return sin(PI * x) * sin(PI * y);
  43.      //return sin(PI * x) * sin(PI * y) * x * y;
  44.  }
  45.  double Real_U(int i, int j)
  46.  {
  47.      return Real_U((double)i * h, (double)j * h);
  48.  }
  49.  
  50.  int indic(int i, int j) {
  51.      if(i == 0 || i == N || j == 0 || j == N) return 1;
  52.      else return 0;
  53.  }
  54.  string to_string(double a){
  55.      ostringstream strs;
  56.      strs << a;
  57.      string str = strs.str();
  58.      return str;
  59.  }
  60.  void PrintToFile(string filename,string str){
  61.     const   char* s = filename.c_str();
  62.      std::ofstream out(s);
  63.     out << str;
  64.     out.close();
  65.  }
  66.  
  67.  vector<double> ThomasAlgoritm(vector<double> A, vector<double> C, vector<double> B, vector<double> F)
  68.  {
  69.      vector<double> C_;   C_.push_back(C[0]);
  70.      vector<double> F_;   F_.push_back(F[0]);
  71.      vector<double> item;   item.push_back(0.0);
  72.      int n = C.size();
  73.      for (int i = 1; i < n; i++)
  74.      {
  75.          C_.push_back(C[i] - A[i - 1] * B[i - 1] / C_[i - 1]);
  76.          F_.push_back(F[i] - A[i - 1] * F_[i - 1] / C_[i - 1]);
  77.          item.push_back(0.0);
  78.      }
  79.      item[n - 1] = F_[n - 1] / C_[n - 1];
  80.      for (int i = n - 2; i >= 0; i--)
  81.      {
  82.          item[i] = (F_[i] - B[i] * item[i + 1]) / C_[i];
  83.      }
  84.      return item;
  85.  }
  86.  class approx
  87.  {
  88.      vector<vector<double> > sh_k;
  89.      vector<vector<double> > sh_k_half;
  90.  
  91.  public:
  92.      approx()
  93.      {
  94.          sh_k = *(new vector<vector<double> >);
  95.          sh_k_half = *(new vector<vector<double> >);
  96.  
  97.          int i, j;
  98.          for (i = 0; i <= N; i++)
  99.          {
  100.              sh_k.push_back(*(new vector<double>));
  101.              sh_k_half.push_back(*(new vector<double>));
  102.              for (j = 0; j <= N; j++)
  103.              {
  104.                  sh_k[i].push_back(alpha(i * h, j * h));
  105.                  sh_k_half[i].push_back(alpha(i * h, j * h));
  106.              }
  107.          }
  108.      }
  109.      void step_2(){
  110.          int i, j;
  111.          vector<double> solve = *(new vector<double>);
  112.          vector<double> A = *(new vector<double>);
  113.          vector<double> C = *(new vector<double>); C.push_back(1.0);
  114.          vector<double> B = *(new vector<double>); B.push_back(0.0);
  115.          vector<double> F = *(new vector<double>);
  116.          for (i = 0; i < N - 1; i++)
  117.          {
  118.              A.push_back(tau / (2 * h * h));
  119.              B.push_back(tau / (2 * h * h));
  120.              C.push_back(-(tau / (h * h) + 1));
  121.          }
  122.          A.push_back(0.0); C.push_back(1.0);
  123.          for (j = 1; j < N; j++)
  124.          {
  125.              // заполняем F
  126.              F.clear();
  127.              F.push_back(alpha(0, j * h));
  128.              //for (i = 1; i < N; i++) F.push_back(alpha(i * h, j * h));
  129.              for (i = 1; i < N; i++)
  130.              {
  131.                  F.push_back(-sh_k[i][j] - tau * ((sh_k[i][j + 1] - 2 * sh_k[i][j] + sh_k[i][j - 1]) / (h * h) + f(i, j)) / 2);
  132.              }
  133.              F.push_back(alpha(1, j * h));
  134.              // решаем систему
  135.              solve = ThomasAlgoritm(A, C, B, F);
  136.              for (i = 0; i <= N; i++)
  137.                  sh_k_half[i][j] = solve[i];
  138.          }
  139.      }
  140.      void step_3()
  141.      {
  142.          int i, j;
  143.          vector<double> solve = *(new vector<double>);
  144.          vector<double> A = *(new vector<double>);
  145.          vector<double> C = *(new vector<double>); C.push_back(1.0);
  146.          vector<double> B = *(new vector<double>); B.push_back(0.0);
  147.          vector<double> F = *(new vector<double>);
  148.          for (i = 0; i < N - 1; i++)
  149.          {
  150.              A.push_back(tau / (2 * h * h));
  151.              B.push_back(tau / (2 * h * h));
  152.              C.push_back(-(tau / (h * h) + 1));
  153.          }
  154.          A.push_back(0.0); C.push_back(1.0);
  155.          for (i = 1; i < N; i++)
  156.          {
  157.              // заполняем F
  158.              F.clear();
  159.              F.push_back(alpha(i * h, 0));
  160.              for (j = 1; j < N; j++)
  161.              {
  162.                  F.push_back(-sh_k_half[i][j] - tau * ((sh_k_half[i + 1][j] - 2 * sh_k_half[i][j] + sh_k_half[i - 1][j]) / (h * h) + f(i, j)) / 2);
  163.              }
  164.              F.push_back(alpha(i * h, 1));
  165.              // решаем систему
  166.              solve = ThomasAlgoritm(A, C, B, F);
  167.              for (j = 0; j <= N; j++)
  168.                  sh_k[i][j] = solve[j];
  169.          }
  170.      }
  171.      void step_4()
  172.      {
  173.          step_2();
  174.          step_3();
  175.      }
  176.  
  177.  
  178.      string GiveWolframString()
  179.      {
  180.          string s = "";
  181.          int i, j;
  182.          s += "ListPlot3D[{";
  183.          for (i = 0; i <= N; i++)
  184.              for (j = 0; j <= N; j++)
  185.              {
  186.                  s += "{";
  187.                  s += to_string(i * h);
  188.                  s += ",";
  189.                  s += to_string(j * h);
  190.                  s += ",";
  191.                  s += to_string(sh_k[i][j]);
  192.                  if(i + j < 2 * N) s += "}, ";
  193.                  else s += "}}]";
  194.              }
  195.          return s;
  196.      }
  197.      string GiveWolframErrorString()
  198.      {
  199.          string s = "";
  200.          int i, j;
  201.          for (i = 0; i <= N; i++)
  202.              for (j = 0; j <= N; j++)
  203.              {
  204.                  s += "{";
  205.                  s += to_string(i * h);
  206.                  s += ",";
  207.                  s += to_string(j * h);
  208.                  s += ",";
  209.                  s += to_string(abs(Real_U(i, j) - sh_k[i][j]));
  210.                  s += "}, ";
  211.              }
  212.          return s;
  213.      }
  214.      double error(){
  215.          int i, j;
  216.          double er = 0.0;
  217.          for (i = 0; i <= N; i++)
  218.              for (j = 0; j <= N; j++)
  219.                  er += abs(sh_k[i][j] - Real_U(i, j)) * abs(sh_k[i][j] - Real_U(i, j));
  220.          return sqrt(er)/N;
  221.      }
  222.  
  223.  };
  224.  
  225. int main(void)
  226. {
  227.     class approx chema;
  228.     double er = chema.error();
  229.     int i;
  230.    
  231.    
  232.  
  233.     for ( i = 0; i < 200; i++)
  234.     {
  235.         chema.step_4();
  236.         if (abs(er - chema.error()) < eps) break;
  237.         //else
  238.         er = chema.error();
  239.         cout << i << "["<< er<< "] , ";
  240.         //Console.WriteLine("{0} - {1}",i, er);
  241.     }
  242.  
  243.     chema.step_4();
  244.    
  245.     string wolf_str = chema.GiveWolframString();
  246.     string wolf_err = chema.GiveWolframErrorString();
  247.     string wolf_item = "{" + to_string(log(N)) + "," + to_string(-log(er)) + "}";
  248.    
  249.     PrintToFile("wolf_str.txt",wolf_str);
  250.     PrintToFile("wolf_err.txt",wolf_err);
  251.     PrintToFile("wolf_item.txt",wolf_item);
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement