Is there something wrong with my Verhulsts Formula implementation?

I was assigned a program for inputting data and outputting a table in which the Verhulsts formula was calculated for k number of years. I used this equation:

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

The equation below:

p (n + 1) = (1 + gh) p (n) - gp (n) ^ 2 / M.

Here is the program I made. I deleted the part of my code that asks for input, as I feel you will be bored if you miss:

> #include <iostream> using namespace std; int main() { int k = 20; // number of years to calculate for int pn = 10; // the population of animals for the 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 the ecosystem /* Implementing Verhulst Formula in C++ */ int i; int pop; for (i = 1; i <= k ; i++) { pop = (((1 + (g - h)) * pn) - g*(pn*pn)/M) + .5; // the equation pn = pop; // this takes the new value of pop and inserts it as pn, to be looped until i <= k cout << i << " " << pop << endl; } return 0; } 

I was instructed to check my code using the examples from the link above, which sets g (growth rate) to 125, 250, and 300, respectively. I feel that my program is pretty accurate for the first two numbers (they exactly correspond to the graph), but when I connect 300, I get very different values ​​from the graph presented. I'm not sure if Ive made any mistake in expressing the above in my code, or if the graph is especially horrible. Ive kept everything else constant using the parameters mentioned on the above site.

Here is the result I get: g = 300. The first column is the year, the second is the population:

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

Compared to the output that I looked from the third chart in the link above. Again, these are guesses, so I cannot vouch for their accuracy:

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

The fact that I can get outputs that correspond to the first and second graphs, but not the third, is bewildering. Is my implementation in C ++ equations sound ?:

  int i; int pop; for (i = 1; i <= k ; i++) { pop = (((1 + (g - h)) * pn) - g*(pn*pn)/M) + .5; // the equation pn = pop; // this takes the new value of pop and inserts it as pn, to be looped until i <= k cout << i << " " << pop << endl; } 
+5
source share
1 answer

Note that the population for year 0 on the 3rd chart is initially 120, not 20. Change your entry to 120, and in the end you will get values ​​much closer to the values ​​of the table that you noticed.

Leaving your data as types in the provided code, the output will look like this:

 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 would like to point out that adding 0.5 to account for rounding errors is not required if you use the double type for all your values:

 #include <iostream> using namespace std; int main() { int k = 20; // number of years to calculate for double pn = 120; // the population of animals for the 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 the ecosystem /* Implementing Verhulst Formula in C++ */ int i; double pop; for (i = 1; i <= k ; i++) { pop = (((1 + (g - h)) * pn) - g*(pn*pn)/M); // the equation pn = pop; // this takes the new value of pop and inserts it as pn, to be looped until i <= k cout << i << " " << (int)pop << endl; } return 0; } 

It will create

 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 
+3
source

Source: https://habr.com/ru/post/1242417/


All Articles