c++ - Is there anything wrong with my implementation of Verhulst’s Formula? -


i got assigned program take inputs , output table calculated verhulst’s formula k number of years. used equation:

http://www.resnet.wm.edu/~jxshix/math410/verhulst.html

the equation below:

p(n+1) = (1+g-h)p(n) - gp(n)^2/m.

here program made. i've removed portion of code requests input feel tedious guys sift through :

> #include <iostream> using namespace std;     int main() {      int k = 20; // number of years calculate    int pn = 10; // population of animals first year    double g = 275; // rate of growth    g = g/100.00;     double h = 20; // rate of animal death/animals leaving population    h = h/100.00;    int m = 100; // carrying capacity of ecosystem     /* implementing verhulst's formula in c++ */     int i;  int pop;  (i = 1; <= k ; i++)  {  pop = (((1 + (g - h)) * pn) - g*(pn*pn)/m) + .5; // equation  pn = pop; // takes new value of pop , inserts pn, looped until <= k  cout << << " " << pop << endl;  }     return 0;  } 

i instructed test code using examples link above, sets g (rate of growth) @ 125, 250 , 300 respectively. feel program pretty accurate first 2 numbers (they match graph accurately) when plug in 300 different values graph presented. i’m not sure if i’ve made kind of mistake in expressing above in code, or if graph terrible. i’ve kept else constant, using parameters mentioned on aforementioned site.

here output set g = 300. first column year, second population:

1 35 2 96 3 88 4 102 5 75 6 116 7 37 8 100 9 80 10 112 

versus output i've eyeballed 3rd graph in above link. again, these guesses, can't vouch accuracy:

1 25 2 70 3 120 4 33 5 94 6 90 7 98 8 86 9 92 10 70 

that can outputs match first , second graphs not third quite perplexing. implementation in c++ of equation sound?:

 int i;  int pop;  (i = 1; <= k ; i++)  {  pop = (((1 + (g - h)) * pn) - g*(pn*pn)/m) + .5; // equation  pn = pop; // takes new value of pop , inserts pn, looped until <= k  cout << << " " << pop << endl;  }  

notice population year 0 in 3rd graph 120, not 20. change input 120 , you'll end values closer of table eyeballed.

leaving data types in provided code, output becomes:

1 24 2 74 3 117 4 34 5 95 6 90 7 99 8 82 9 110 10 55 11 118 12 31 13 89 14 101 15 78 16 114 17 43 18 108 19 60 20 120 

i point out adding 0.5 account rounding error unnecessary if use type double values:

#include <iostream> using namespace std;  int main() {   int k = 20; // number of years calculate   double pn = 120; // population of animals first year   double g = 300; // rate of growth   g = g/100.00;   double h = 20; // rate of animal death/animals leaving population   h = h/100.00;   double m = 100; // carrying capacity of ecosystem  /* implementing verhulst's formula in c++ */    int i;   double pop;   (i = 1; <= k ; i++)   {     pop = (((1 + (g - h)) * pn) - g*(pn*pn)/m); // equation     pn = pop; // takes new value of pop , inserts pn, looped until <= k     cout << << " " << (int)pop << endl;   }    return 0; } 

that produce

1 24 2 73 3 116 4 34 5 94 6 91 7 97 8 85 9 105 10 67 11 119 12 25 13 76 14 115 15 39 16 102 17 73 18 117 19 32 20 91 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -