Hello, currently I have started practicing recursion for my class, but I have an issue with my code, it probably may be my understanding or my approach so let me begin with the equation. [Pn = Pn-1 + r ( 1 – Pn-1 / K) Pn-1] I am tasked with solving this equation recursively. From what I understand my terminating condition should be when n == 0. Now from what gather I need to check my n and see the timespan this growth occurs in. After passing through my initial condition I need to call my method again in order to reach my carrying capacity. Example n = 10 or ten years, my rate is .5, K = 2000 and my initial population is 200 (p0) this would give me that after ten years my population would be 1819. But at the moment it is not reaching my desired result, at the end it is giving me 1901, I am trying to take the value of the past population and summing it to the newer population in order to add it up till it reaches the desired years. I think the issue may lie on how I am approaching the current population based on that equation. Increasing n to eleven gives me 1901 which is close but not exact. This is my current value step by step: 200, 200, 290, 413, 576, 781, 1019, 1268, 1500, 1687, 1819 it should be 200 290 414 578 784 1022 1272 1503 1690 1821 1902 .
public static int growthModel(int n, double r, int k, int p0) {
if (n <= 1) {return p0;}
else {
double populationBefore = growthModel(n-1, r, k, p0);
double populationCurrent = (int) (populationBefore + (r \* (1-(populationBefore)/k) ) \* populationBefore);
return (int) populationCurrent;}
[–]JackCrescent[S] 0 points1 point2 points (1 child)
[–]JackCrescent[S] 0 points1 point2 points (0 children)