c++ - 我执行 Verhulst 公式有什么问题吗?

标签 c++ math equations

我被分配了一个程序来获取输入并输出一个表格,该表格计算了 k 年的 Verhulst 公式。我使用了这个等式:

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

等式如下:

p(n+1) = (1+g-h)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

与我从上面链接中的第三张图观察到的输出相比。同样,这些都是猜测,所以我不能保证它们的准确性:

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

关于c++ - 我执行 Verhulst 公式有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236815/

相关文章:

c++ - 幂集 ~ 递归 ~ Hasse 图

c++ - 如何抛出异常并在 Node.js 中捕获它?

javascript - 绘制温度的平均值

c++ - 如何在 WinCE 中创建 .bmp

c++ - C++中的回调 hell 是什么,为什么会泄漏内存?

Ruby - 数学运算

java - 用于绘制数学函数/方程式的简单 Java 库

f# 用方程式查询

matlab - 如何在 MatLab 中求解精确微分方程?