Java种群增长循环与方法;输出不正确

时间:2018-03-04 02:12:05

标签: java for-loop methods population

我的任务是创建一个程序,使用公式x_new = r * x_previous * (1-x_previous)对人口增长进行建模,其中r是生育力参数,x是人口。我有一个工作程序和方法。但是,我的数学并没有结账。我应该能够测试.01的初始人口,1.11000时间步长的生育力参数并获得.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

.
.
.

任何想法可能是疼痛的原因?提前谢谢!

1 个答案:

答案 0 :(得分:0)

r达到5.2关于迭代43时,您的增长函数会变为负数,因为x_previous is > 1 ...所以1-x_previous < 0,您确定模型是对的...这似乎是一个概念问题。尝试在excel中对其进行建模。

另一方面,在计算下一个r值之前,您需要递增p。你确定是吗?