diff --git a/raeuber-beute/predatorpreycalculator.py b/raeuber-beute/predatorpreycalculator.py index 7c6c5a6..d16fdd1 100644 --- a/raeuber-beute/predatorpreycalculator.py +++ b/raeuber-beute/predatorpreycalculator.py @@ -52,11 +52,27 @@ def calculate(self): while i < self.iterations: t = i * self.dt - xnew = x + self.dx(x,y) * self.dt - ynew = y + self.dy(x,y) * self.dt - x = xnew - y = ynew + #One way to integrate would be the Euler-Method, + #but that would lead to a growth of both populations + #as with each step the error would be quite big. The + #following four lines show the Eurler-integration for + #educational puposes: + + #xnew = x + self.dx(x,y) * self.dt + #ynew = y + self.dy(x,y) * self.dt + #x = xnew + #y = ynew + + #Using an Runga-Kutta 2nd order integrator, c.f. + #http://mathworld.wolfram.com/Runge-KuttaMethod.html + xk_1 = self.dx(x,y) * self.dt + yk_1 = self.dy(x,y) * self.dt + xk_2 = self.dt * self.dx(x+xk_1,y+yk_1) + yk_2 = self.dt * self.dy(x+xk_1,y+yk_1) + + x = x + (xk_1 + xk_2)/2 + y = y + (yk_1 + yk_2)/2 r_string.append(x) b_string.append(y) @@ -66,3 +82,5 @@ def calculate(self): return r_string, b_string + +