xXx_Fortis_xXx

Untitled

Feb 20th, 2021
824
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // g++ lab19.cpp -o lab19 -lboost_iostreams -lboost_system -lboost_filesystem
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <cmath>
  6. #include <utility>
  7. #include <gnuplot-iostream.h>
  8.  
  9. typedef std::vector<std::vector<double>> Matrix;
  10.  
  11. int main (int argc, char **argv){
  12.     double a = 1;
  13.     double b = 1;
  14.     double lx = 0;
  15.     double rx = M_PI;
  16.     size_t N = 10;
  17.     size_t K = 1001;
  18.     double T = 10;
  19.     double h = (rx - lx) / N;
  20.     double tau = T / K;
  21.  
  22.     auto left = [](double t, double a, double b){
  23.         return exp(-a * t) * (-cos(b * t) + sin(b * t));
  24.         // return cos(b * t);
  25.     };
  26.     auto right = [](double t, double a, double b){
  27.         return -exp(-a * t) * (cos(b * t) + sin(b * t));
  28.         // return cos(b * t - M_PI);
  29.     };
  30.     Matrix res(1, std::vector<double>());
  31.  
  32.     for (size_t i = 0; i <= N; i++){
  33.         res[0].push_back(cos(i * h));
  34.     }
  35.  
  36.     for (size_t i = 1; i <= K; i++){
  37.         std::vector<double> newRow;
  38.         newRow.push_back(0);
  39.         for (size_t j = 1; j < N; j++){
  40.             newRow.push_back(
  41.             tau *
  42.             (a * (res[i - 1][j + 1] - 2 * res[i - 1][j] + res[i - 1][j - 1])
  43.             / h / h - b * (res[i - 1][j + 1] - res[i - 1][j - 1]) / 2 / h) +
  44.             res[i - 1][j]);
  45.         }
  46.         newRow[0] = (newRow[1] - h * left(i * tau, a, b)) / (h + 1);
  47.         newRow.push_back((newRow[N - 1] + h * right(i * tau, a, b)) / (1 - h));
  48.  
  49.         res.push_back(newRow);
  50.     }
  51.  
  52.     Gnuplot gp;
  53.     gp << "set ticslevel 0" << std::endl;
  54.     gp << "set xrange[0:3.14159265358979323846]" << std::endl;
  55.     gp << "set yrange[0:10]" << std::endl;
  56.     gp << "set xlabel \"X\"" << std::endl;
  57.     gp << "set ylabel \"Temperature\"" << std::endl;
  58.     gp << "set dgrid3d 20,11" << std::endl;
  59.     // gp << "set isosamples 20,11" << std::endl;
  60.     gp << "set hidden3d" << std::endl;
  61.     // gp << "set pm3d" << std::endl;
  62.     // gp << "set logscale z" << std::endl;
  63.     gp << "splot '-' with lines title \"U(x, t)\"" << std::endl;
  64.     for (size_t i = 0; i <= K; i++){
  65.         for (size_t j = 0; j <= N; j++){
  66.             // gp.send(boost::make_tuple(j * h + lx, i * tau, res[i][j]));
  67.             gp << j * h + lx << " " << i * tau << " " << res[i][j] << std::endl;
  68.             // std::cout << j * h + lx << " " << i * tau << " " << res[i][j] << std::endl;
  69.         }
  70.     }
  71.     gp << "e" << std::endl;
  72.     std::cin.get();
  73. }
RAW Paste Data