我的任务是创建一个使用公式模拟人口增长的程序x_new = r * x_previous * (1-x_previous)
,其中r
是繁殖力参数,x
是人口。我有一个工作程序和方法。但是,我的数学没有检查出来。我应该能够测试 的初始种群.01
、繁殖力参数1.1
、1000
时间步长并得到答案.09
)。
这是我的程序:
import java.util.Scanner;
public class Lab6Question3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("The population, p: ");
double p = input.nextDouble();
System.out.println("The fecundity rate, r: ");
double r = input.nextDouble();
System.out.println("Number of Time Steps, n: ");
int n = input.nextInt();
int i; //iterations
System.out.println("-------------------------------------");
System.out.println("Simulation with starting population " + p);
System.out.println("Running " + n + " steps");
System.out.println("Varying fecundicity 1, 1.1, ..., 4.9, 5");
for (i = 0; i < n; i++) {
System.out.println("r = " + r + " final population = " + p);
r = r + 0.1;
p = populationGrowth(p, r);
}
}
public static double populationGrowth(double population, double fecundicity)
{
double p;
return (fecundicity * population * (1 - population));
}
}
而且我的输出使用迭代正确格式化,但填充结果是错误的:
Simulation with starting population 0.01
Running 10000 steps
Varying fecundicity 1, 1.1, ..., 4.9, 5
r = 1.0 final population = 0.01
r = 1.1 final population = 0.01089
r = 1.2000000000000002 final population = 0.012925689480000004
r = 1.3000000000000003 final population = 0.01658620084090661
.
.
.
有什么想法可能是造成疼痛的原因吗?提前致谢!