Programming Praxis – Wolves And Rabbits

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
About these ads

Tags: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: