Posts Tagged ‘rabbits’

Programming Praxis – Wolves And Rabbits

December 1, 2009

The task in today’s Programming Praxis problem is to define a function that models the populations of wolves and rabbits. Let’s get started, shall we?

Like the provided solution, we use the second-order Runge-Kutta method to calculate the next time step. The resulting function is pretty straightforward:

pops :: Fractional a => a -> a -> a -> a -> a -> a -> [(a, a)]
pops r w rg wg rd wd = (r, w) : pops r' w' rg wg rd wd where
    dr x y = rg*x - rd*x*y
    dw x y = wg*x*y - wd*x
    rh     = r + dr r w / 2
    wh     = w + dw w r / 2
    r'     = r + dr rh wh
    w'     = w + dw wh rh

A quick test shows the same output as the provided solution:

main :: IO ()
main = mapM_ print . take 201 $ pops 40 15 0.1 0.005 0.01 0.1