Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
 } 
like image 513
Chris T Avatar asked Oct 31 '22 10:10

Chris T


1 Answers

Notice the population for year 0 in the 3rd graph is initially 120, not 20. Change your input to 120 and you'll end up with values much closer to that of the table you eyeballed.

Leaving your data as the types in your provided code, the 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 would like to point out that adding 0.5 to account for rounding error is unnecessary if you use type double 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's 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;
}

That will 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
like image 86
Patrick Roberts Avatar answered Nov 15 '22 06:11

Patrick Roberts