Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. const double a = 0.05; //częstość narodzin ofiar lub współczynnik przyrostu ofiar
  10. const double b = 0.002; //częstość umierania ofiar na skutek drapieżnictwa
  11. const double c = 0.06; //częstość narodzin drapieżników lub współczynnik przyrostu drapieżników
  12. const double d = 0.004; //częstość umierania drapieżników lub współczynnik ubywania drapieżników
  13.  
  14. const double h = 0.1; //UŻYWAM h ZAMIAST step
  15.  
  16. const int l_krokow = 4000;
  17.  
  18. double dxdt (double x, double y)
  19. {
  20.     return (a - b * y) * x;
  21. }
  22.  
  23. double dydt (double x, double y)
  24. {
  25.     return (c * x - d) * y;
  26. }
  27.  
  28. int main (int argc, char** argv)
  29. {
  30.     // x - ofiary, y - drapieżniki
  31.    
  32.     double y0h = 1;     // h zamiast step
  33.  
  34.     for(int i=0; i<100; i++)        //ten pierwszy for idzie po ilosciach drapieznikow (czyli    analizuje sie caly czas zmiany w czasie ale przy roznych startowych wartosciach drapieznikow)
  35.     {
  36.         double x0 = 100;    // ilosc ofiar
  37.         double y0 = 10+i*y0h;        //zastanawiam sie skad taki wzor… (po co to mnozyc jeszcze razy 1…)
  38.  
  39.         double x=x0;
  40.         double y=y0;
  41.  
  42.         ofstream af;    //  strumien do zapisu wsp X
  43.         stringstream afN;   //strumien do utworzenia nazwy pliku
  44.         afN << "./txt/xt" << setfill('0') << setw(4) << i << ".txt"; //tworzy plik w folderze z programem/txt/xt’iteracja’.txt
  45.         string afName = afN.str();  // .str() zwraca string z przekopiowana zawartoscia
  46.  
  47.         ofstream bf;
  48.         stringstream bfN;
  49.         bfN << "./txt/yt" << setfill('0') << setw(4) << i << ".txt"; //tworzy plik w folderze z programem/txt/yt’iteracja’.txt
  50.         string bfName = bfN.str();
  51.  
  52.         af.open(afName.c_str());
  53.         bf.open(bfName.c_str());
  54.  
  55.         double kx1, ky1, kx2, ky2, kx3, ky3, kx4, ky4;
  56.        
  57.         for (double i1 = 0; i1 < l_krokow * h; i1 += h) // Funkcja zapisująca współrzędne do plików af i bf.
  58.         {
  59.             af << i1 << ' ' << x << endl;
  60.             bf << i1 << ' ' << y << endl;
  61.  
  62.             x0 = x;
  63.             y0 = y;
  64. /*
  65.             x += h * dxdt(x0,y0);
  66.             y += h * dydt(x0,y0);
  67. */
  68.  
  69.             kx1 = h * dxdt(x0,y0);
  70.             ky1 = h * dydt(x0,y0);
  71.  
  72.             kx2 = h * dxdt(x0 + (h/2.0), y0 + (kx1/2.0));
  73.             ky2 = h * dydt(x0 + (h/2.0), y0 + (ky1/2.0));
  74.  
  75.             kx3 = h * dxdt(x0 + (h/2.0), y0 + (kx2/2.0));
  76.             ky3 = h * dydt(x0 + (h/2.0), y0 + (ky2/2.0));
  77.  
  78.             kx4 = h * dxdt(x0 + h, y0 + kx3);
  79.             ky4 = h * dxdt(x0 + h, y0 + ky3);
  80.  
  81.             x += (kx1 + 2.0*kx2 + 2.0*kx3 + kx4)/6.0;
  82.             y += (ky1 + 2.0*ky2 + 2.0*ky3 + ky4)/6.0;
  83.  
  84.            
  85.         }
  86.  
  87.         af.close();
  88.         bf.close();
  89.  
  90.        
  91.     }
  92.  
  93.     if(argc > 1)
  94.     {
  95.         system("rm ./png/* ./txt/*");
  96.     }
  97.  
  98.     return 0;
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement