Skip to content

Commit

Permalink
Using the Runge-Kutta-Integrator of second order instead of the Euler…
Browse files Browse the repository at this point in the history
… integrator. Thanks to Stephan Burkhardt for the hintgit diff
  • Loading branch information
Carsten Niehaus committed Jul 3, 2009
1 parent f3ee7b3 commit c84a670
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions raeuber-beute/predatorpreycalculator.py
Expand Up @@ -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)
Expand All @@ -66,3 +82,5 @@ def calculate(self):
return r_string, b_string




0 comments on commit c84a670

Please sign in to comment.