Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 4m_lab2.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"//vs2010
- #include <bits\stdc++.h>
- #include "gnuplot_i.hpp"
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- #define cyrillic setlocale(LC_CTYPE, "rus")
- const string path1 = "C:\\Users\\Admin\\Documents\\Visual Studio 2010\\Projects\\4m_lab2\\1.txt";
- const string path2 = "C:\\Users\\Admin\\Documents\\Visual Studio 2010\\Projects\\4m_lab2\\2.txt";
- inline string dtos(double d){ostringstream strs; strs << d; return strs.str();}
- double f(double x , double y);
- double g(double x , double y);
- void wait(){
- FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
- #ifndef FOUT
- cout << endl << "Press any key to continue..." << endl;
- #endif
- _getch();
- }
- vector<pair<double , pair<double , double> > > Runge_Kutta1(double x_ , double y_ , double n , double max_t){
- double h = max_t / n;
- vector<pair<double , pair<double , double> > > v;
- v.push_back(make_pair(0 , make_pair(x_ , y_)));
- for(int i = 0 ; i < n ; ++i){
- double t = 1.0 * (i + 1) * h;
- x_ += h * f(x_ , y_);
- y_ += h * g(x_ , y_);
- v.push_back(make_pair(t , make_pair(x_ , y_)));
- }
- return v;
- }
- vector<pair<double , pair<double , double> > > Runge_Kutta4(double x_ , double y_ , double n , double max_t){
- double h = max_t / n;
- vector<pair<double , pair<double , double> > > v;
- v.push_back(make_pair(0 , make_pair(x_ , y_)));
- for(int i = 0 ; i < n ; ++i){
- double t = 1.0 * (i + 1) * h;
- double k1 = h * f(x_ , y_);
- double m1 = h * g(x_ , y_);
- double k2 = h * f(x_ + k1 / 2 , y_ + m1 / 2);
- double m2 = h * g(x_ + k1 / 2 , y_ + m1 / 2);
- double k3 = h * f(x_ + k2 / 2 , y_ + m2 / 2);
- double m3 = h * g(x_ + k2 / 2 , y_ + m2 / 2);
- double k4 = h * f(x_ + k3 , y_ + m3);
- double m4 = h * g(x_ + k3 , y_ + m3);
- x_ += (k1 + 2 * k2 + 2 * k3 + k4) / 6.0;
- y_ += (m1 + 2 * m2 + 2 * m3 + m4) / 6.0;
- v.push_back(make_pair(t , make_pair(x_ , y_)));
- }
- return v;
- }
- double f(double x , double y){
- return 2 * x - 0.02 * x * y; // 3*x-y;
- }
- double g(double x , double y){
- return 0.0002 * x * y - 0.8 * y; // 4*x-y;
- }
- //
- // {x' = a*x(t) - b*x(t)*y(t)
- // {y' = c*x(t)*y(t) - d*y(t)
- //
- // a = 2 ; b = 0.02 ; c = 0.0002 ; d = 0.8
- //
- //
- // {x' = 3x - y
- // {y' = 4x - y
- //
- // x(0) = 5 ; y(0) = 8
- //
- // {x = (5 + 2t)e^t
- // {y = (8 + 4t)e^t
- //
- int main(int argc, _TCHAR* argv[]){
- cyrillic;
- cout.setf(ios::fixed);
- cout.precision(5);
- double t;
- cin >> t;
- double x = 5000;// 5 //жертви
- double y = 100;// 8 //хижаки
- int n = 1000;
- vector<pair<double , pair<double , double> > > v = Runge_Kutta4(x , y , n , t);
- FILE * file1 = fopen(&path1[0] , "w");
- FILE * file2 = fopen(&path2[0] , "w");
- pair<double , double> mx1 , mx2;
- mx1 = mx2 = make_pair(DBL_MAX , DBL_MIN);
- for(int i = 0 ; i < v.size() ; ++i){
- fprintf(file1 , " %lf %lf\n" , v[i].first , v[i].second.first);
- fprintf(file2 , " %lf %lf\n" , v[i].first , v[i].second.second);
- mx1.first = min(mx1.first , v[i].second.first);
- mx1.second = max(mx1.second , v[i].second.first);
- mx2.first = min(mx2.first , v[i].second.second);
- mx2.second = max(mx2.second , v[i].second.second);
- }
- fclose(file1);
- fclose(file2);
- Gnuplot gnu("lines");
- gnu.set_grid();
- string aa = "";
- aa += "set yrange [" + dtos(mx1.first - 50) + ":" + dtos(mx1.second + 50) + "]\n";
- aa += "set y2range [" + dtos(mx2.first - 50) + ":" + dtos(mx2.second + 50) + "]\n";
- aa += "set xrange [0:" + dtos(t) + "]\n";
- aa += "set y2tics\n";
- aa += "set ylabel \"Жертви \"\n";
- aa += "set y2label \"Хижаки \"\n";
- aa += "Жертви = '" + path1 + "'\n";
- aa += "Хижаки = '" + path2 + "'\n";
- string bb = "plot Жертви axes x1y1 with lines , Хижаки axes x1y2 with lines";
- gnu.plot_points(bb , aa);
- wait();
- remove(&path1[0]);
- remove(&path2[0]);
- gnu.remove_tmpfiles();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement