Advertisement
osipyonok

4m_lab2_s2

Mar 9th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. // 4m_lab2.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"//vs2010
  5. #include <bits\stdc++.h>
  6. #include "gnuplot_i.hpp"
  7. #include <conio.h>
  8. #include <windows.h>
  9.  
  10. using namespace std;
  11.  
  12. #define cyrillic setlocale(LC_CTYPE, "rus")
  13.  
  14. const string path1 = "C:\\Users\\Admin\\Documents\\Visual Studio 2010\\Projects\\4m_lab2\\1.txt";
  15. const string path2 = "C:\\Users\\Admin\\Documents\\Visual Studio 2010\\Projects\\4m_lab2\\2.txt";
  16.  
  17. inline string dtos(double d){ostringstream strs; strs << d; return strs.str();}
  18.  
  19. double f(double x , double y);
  20. double g(double x , double y);
  21.  
  22. void wait(){
  23.     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  24.     #ifndef FOUT
  25.         cout << endl << "Press any key to continue..." << endl;
  26.     #endif
  27.     _getch();
  28. }
  29.  
  30. vector<pair<double , pair<double , double> > > Runge_Kutta1(double x_ , double y_ , double n , double max_t){
  31.     double h = max_t / n;
  32.  
  33.     vector<pair<double , pair<double , double> > > v;
  34.     v.push_back(make_pair(0 , make_pair(x_ , y_)));
  35.    
  36.     for(int i = 0 ; i < n ; ++i){
  37.         double t = 1.0 * (i + 1) * h;
  38.  
  39.         x_ += h * f(x_ , y_);
  40.         y_ += h * g(x_ , y_);
  41.  
  42.         v.push_back(make_pair(t , make_pair(x_ , y_)));
  43.     }
  44.  
  45.     return v;
  46. }
  47.  
  48. vector<pair<double , pair<double , double> > > Runge_Kutta4(double x_ , double y_ , double n , double max_t){
  49.     double h = max_t / n;
  50.  
  51.     vector<pair<double , pair<double , double> > > v;
  52.     v.push_back(make_pair(0 , make_pair(x_ , y_)));
  53.  
  54.     for(int i = 0 ; i < n ; ++i){
  55.         double t = 1.0 * (i + 1) * h;
  56.        
  57.         double k1 = h * f(x_ , y_);
  58.         double m1 = h * g(x_ , y_);
  59.  
  60.         double k2 = h * f(x_ + k1 / 2 , y_ + m1 / 2);
  61.         double m2 = h * g(x_ + k1 / 2 , y_ + m1 / 2);
  62.  
  63.         double k3 = h * f(x_ + k2 / 2 , y_ + m2 / 2);
  64.         double m3 = h * g(x_ + k2 / 2 , y_ + m2 / 2);
  65.  
  66.         double k4 = h * f(x_ + k3 , y_ + m3);
  67.         double m4 = h * g(x_ + k3 , y_ + m3);
  68.  
  69.         x_ += (k1 + 2 * k2 + 2 * k3 + k4) / 6.0;
  70.         y_ += (m1 + 2 * m2 + 2 * m3 + m4) / 6.0;
  71.  
  72.         v.push_back(make_pair(t , make_pair(x_ , y_)));
  73.     }
  74.     return v;
  75. }
  76.  
  77. double f(double x , double y){
  78.     return 2 * x - 0.02 * x * y; // 3*x-y;
  79. }
  80.  
  81. double g(double x , double y){
  82.     return 0.0002 * x * y - 0.8 * y; // 4*x-y;
  83. }
  84.  
  85. //
  86. //      {x' = a*x(t) - b*x(t)*y(t)
  87. //      {y' = c*x(t)*y(t) - d*y(t)
  88. //
  89. //      a = 2 ; b = 0.02 ; c = 0.0002 ; d = 0.8
  90. //
  91.  
  92. //
  93. //      {x' = 3x - y
  94. //      {y' = 4x - y  
  95. //
  96. //      x(0) = 5 ; y(0) = 8
  97. //
  98. //      {x = (5 + 2t)e^t
  99. //      {y = (8 + 4t)e^t
  100. //
  101.  
  102. int main(int argc, _TCHAR* argv[]){
  103.     cyrillic;
  104.     cout.setf(ios::fixed);
  105.     cout.precision(5);
  106.     double t;
  107.     cin >> t;
  108.     double x = 5000;// 5 //жертви
  109.     double y = 100;// 8 //хижаки
  110.  
  111.     int n = 1000;
  112.  
  113.     vector<pair<double , pair<double , double> > > v = Runge_Kutta4(x , y , n , t);
  114.  
  115.     FILE * file1 = fopen(&path1[0] , "w");
  116.     FILE * file2 = fopen(&path2[0] , "w");
  117.  
  118.     pair<double , double> mx1 , mx2;
  119.     mx1 = mx2 = make_pair(DBL_MAX , DBL_MIN);
  120.  
  121.     for(int i = 0 ; i < v.size() ; ++i){
  122.         fprintf(file1 , "  %lf   %lf\n" , v[i].first , v[i].second.first);
  123.         fprintf(file2 , "  %lf   %lf\n" , v[i].first , v[i].second.second);
  124.  
  125.         mx1.first = min(mx1.first , v[i].second.first);
  126.         mx1.second = max(mx1.second , v[i].second.first);
  127.  
  128.         mx2.first = min(mx2.first , v[i].second.second);
  129.         mx2.second = max(mx2.second , v[i].second.second);
  130.     }
  131.  
  132.     fclose(file1);
  133.     fclose(file2);
  134.  
  135.     Gnuplot gnu("lines");
  136.  
  137.     gnu.set_grid();
  138.  
  139.     string aa = "";
  140.     aa += "set yrange [" + dtos(mx1.first - 50) + ":" + dtos(mx1.second + 50) + "]\n";
  141.     aa += "set y2range [" + dtos(mx2.first - 50) + ":" + dtos(mx2.second + 50) + "]\n";
  142.     aa += "set xrange [0:" + dtos(t) + "]\n";
  143.     aa += "set y2tics\n";
  144.     aa += "set ylabel \"Жертви \"\n";
  145.     aa += "set y2label \"Хижаки \"\n";
  146.     aa += "Жертви = '" + path1 + "'\n";
  147.     aa += "Хижаки = '" + path2 + "'\n";
  148.     string bb = "plot Жертви axes x1y1 with lines , Хижаки axes x1y2 with lines";
  149.  
  150.     gnu.plot_points(bb , aa);
  151.  
  152.     wait();
  153.  
  154.     remove(&path1[0]);
  155.     remove(&path2[0]);
  156.  
  157.     gnu.remove_tmpfiles();
  158.  
  159.     system("pause");
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement