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

问题描述 投票:0回答:1

我的任务是创建一个程序,使用公式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

.
.
.

什么想法可能是疼痛的原因?提前致谢!

java for-loop methods population
1个回答
0
投票

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

另一件事,你在计算下一个r值之前增加你p。你确定是吗?

© www.soinside.com 2019 - 2024. All rights reserved.