I got assigned a program to take inputs and output a table that calculated Verhulst’s Formula for k number of years. I used this equation:
http://www.resnet.wm.edu/~jxshix/math410/Verhulst.html
The equation is below:
p(n+1) = (1+g-h)p(n) - gp(n)^2/M.
Here is the program I made. I've removed the portion of my code that requests input as I feel that would be tedious for you guys to sift through :
> #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's 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 test my code using examples from the link above, which sets g (rate of growth) at 125, 250 and 300 respectively. I feel like my program is pretty accurate for the first two numbers (they match the graph fairly accurately) but when I plug in 300 I get very different values from the graph presented. I’m not sure if I’ve made some kind of mistake in expressing the above in my code, or if the graph is especially terrible. I’ve kept everything else constant, using the parameters mentioned on the aforementioned site.
Here is the output I get I set g = 300. First column is year, second is population:
1 35
2 96
3 88
4 102
5 75
6 116
7 37
8 100
9 80
10 112
Versus the output I've eyeballed from the 3rd graph in the above link. Again, these are guesses, so I can't vouch for their accuracy:
1 25
2 70
3 120
4 33
5 94
6 90
7 98
8 86
9 92
10 70
That I can get outputs that match the first and second graphs but not the third is quite perplexing. Is my implementation in C++ of the equation 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;
}