Predator-Prey

from Red Blob Games
1 Jan 2021

Table of Contents

Testing numerical integration:
Heun better than Odex:
Bad:

The wikipedia page[1] has analytic formulas for the wavelength:

period T = 2π / sqrt(λ₁λ₂) λ₁ = i sqrt(α γ) λ₂ = -i sqrt(α γ)

This means period T = 2π / sqrt(α γ). Since I’m using α = 2/3, γ = 1, that means 7.695. But neither my odex nor my heun integration matches!! They’re not even close. It seems to be so close to 8.0 that I’m going to claim it’s exactly 8.

 1  Numerical integration (move to separate page)#

Mike Bostock’s page uses https://github.com/littleredcomputer/odex-js[5] for the differential equation solving. This library includes a predator-prey demo[6].

 1.1 Performance

I’m going to assume Odex gives me the correct answer. The question is: can I get a reasonable approximation that runs faster? For a single run of predator-prey I think it doesn’t matter. The reason I’m curious about performance is that I’m interested in running lots of simulations. It’s nice to be able to set the parameters interactively and see the results, but that shows one at a time. To understand the space of possible solutions, I want to see lots of results simultaneously. See Bret Victor’s page about the Ladder of Abstraction[7].

Initial results: dumb integration is 10-100X faster than Odex.

 1.2 Accuracy

Dumb integration is much faster than Odex but it’s not really stable.

{chart}

What happens here is that in each peak and valley of the curve, dumb integration overshoots. {sketch} This is because it uses the slope from the left side (tangent) instead of the combined slope from the left and right (secant). We can’t calculate the slope using the secant without knowing the value on the right. And we can only calculate the value on the right by using the slope. So we’re kind of stuck.

https://en.wikipedia.org/wiki/Predictor%E2%80%93corrector_method[8]

https://en.wikipedia.org/wiki/Heun%27s_method[9]

Email me , or tweet @redblobgames, or comment: