C++ 我的Verhulst’的实现有什么问题吗;s公式?
我被分配了一个程序来获取输入和输出一个表,该表计算了Verhulst的k年公式。我用了这个等式: 方程式如下: p(n+1)=(1+g-h)p(n)-gp(n)^2/M 这是我制作的程序。我已经删除了我的代码中请求输入的部分,因为我觉得这对你们来说是一个乏味的筛选过程:C++ 我的Verhulst’的实现有什么问题吗;s公式?,c++,math,equations,C++,Math,Equations,我被分配了一个程序来获取输入和输出一个表,该表计算了Verhulst的k年公式。我用了这个等式: 方程式如下: 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 calcu
> #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;
}
与我从上面链接的第三张图中看到的输出相比。同样,这些都是猜测,所以我不能保证它们的准确性:
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;
}
inti;
int-pop;
对于(i=1;i请注意,第三张图中第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
我想指出,如果您对所有值使用typedouble
,则无需为舍入误差添加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;
}
不,它不是。OP真的要求我们找到(可能的)bug。codereview网站是为>>工作的,我想这很好地解释了我输出中的不匹配。
#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