簡體   English   中英

我實施Verhulst公式有什么不對嗎?

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

我被分配了一個程序來獲取輸入並輸出一個表格來計算Verhulst公式的k年數。 我用這個等式:

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

等式如下:

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

這是我制作的節目。 我已經刪除了我的代碼請求輸入的部分,因為我覺得你們篩選的過程會很乏味:

> #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;

}

我被指示使用上面鏈接中的示例來測試我的代碼,該示例將g(增長率)分別設置為125,250和300。 我覺得我的程序對於前兩個數字非常准確(它們相當准確地匹配圖形)但是當我插入300時,我得到的圖表中的值非常不同。 我不確定我在代碼中表達上述內容時是否犯了某種錯誤,或者圖表是否特別糟糕。 我使用上述網站上提到的參數保持其他一切不變。

這是我得到的輸出我設置g = 300.第一列是年,第二列是人口:

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

與我在上面鏈接中的第3個圖表中看到的輸出相比。 再次,這些是猜測,所以我不能保證他們的准確性:

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

我可以獲得與第一和第二圖匹配而不是第三圖的輸出是非常令人困惑的。 我在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;
 } 

請注意,第3個圖表中第0年的總體數最初是120,而不是20.將輸入更改為120,最終得到的數值更接近您所關注的表格。

將數據作為提供的代碼中的類型,輸出變為:

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

我想指出,如果對所有值使用double類型,則不需要添加0.5來解決舍入錯誤:

#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;
}

那會產生

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM