Message Boards Message Boards

New SystemModeler Example: Hare-Lynx, Interactively Explore Dynamics

GROUPS:

A new example has been added to the collection of ready-made example models available on the SystemModeler site: Hare-Lynx: Interactively Explore Population Dynamics. The model requires the free SystemDynamics Modelica library, which can be downloaded from here.

The model uses predator-prey equations, also known as Lotka-Volterra equations. It aims to describe a biological system consisting of two species, a predator (here lynx) and a pray (here hares) and the interaction between them. A large number of predators in the system will increase the mortality of the prey due to predation. A low number of prey will decrease the viability of the predator due to starvation.

SystemModeler diagram of the hare-lynx interaction

While they can be useful for describing phenomenon in nature, what is very interesting about these systems is their mathematical properties. Solving the system will yield a periodic solution that is something akin to a harmonic motion:

Needs["WSMLink`"]

WSMPlot["HareLynxPopulation", "Population Levels", Ticks -> None]

Hare and lynx population over time shows a cyclic behavior

The predator is lagging the population growth of the prey by 90° in the period solution, but otherwise the populations are stable over time.

There are important parameters that affect these equations. The initial conditions are one of them, the rate at which hares and lynx are born is another. If we want to explore these four parameters, we can make use of one of SystemModeler 5s latest features, WSMParametricSimulateValue.

In essence, what it does is it treats a SystemModeler model just as any other Wolfram Language function. You give it a set of arguments and from the function you get the trajectories of some variables over time. It is easy to set up these functions, you just give it the model name, the variables that you want to get back when you call the function, and the parameters that you want to give as arguments:

sim = WSMParametricSimulateValue["HareLynxPopulation", {"hare", "lynx"}, 
    {"HareInitialPopulation", "LynxInitialPopulation", "HareBirthFactor", "LynxBirthFactor"}, 
    WSMProgressMonitor -> False]

Parametric function returned by WSMParametricSimulateValue

Now you can call the function with arbitrary arguments and get back a result:

sim[30000, 2000, 1.25, 0.25]

enter image description here

You can use this directly to plot the two populations:

Map[ListLinePlot, sim[30000, 2000, 1.25, 0.25]]

result returned from running the parametric simulation

Of even more interest could be to look at a parametric plot of the system, with the population of hares (x-axis) versus the population of lynx (y-axis).

ParametricPlot[
 Evaluate[Through[sim[30000, 2000, 1.25, 0.25][t]]], {t, 0, 15},
 AspectRatio -> 1, PlotRange -> {{0, Automatic}, {0, Automatic}}
 ]

parametric plot generated from the parametric simulation

Since it works as any other Wolfram Language function, you can put it inside of a manipulate. For example, here you can explore how the model responds to changing the birth rates for the two species:

Manipulate[
 ParametricPlot[
  Evaluate[Through[sim[30000, 2000, hareBirthFactor, lynxBirthFactor][t]]], {t, 0, 15},
  AspectRatio -> 1, PlotRange -> {{0, Automatic}, {0, Automatic}}
  ],
 {{hareBirthFactor, 1.25}, 1.2, 1.5},
 {{lynxBirthFactor, 0.25}, 0.2, 0.5},
 ContinuousAction -> False
 ]

parametric plot generated from the parametric simulation with manipulate elements to control the output

The downloadable example shows how a much more advanced interactive exploration can be constructed in the same vein. Here is a sneak peak of it using a locator pane:

animation of an interactive exploration, with a locator pane used to set initial conditions

Learn about this and many more new SystemModeler 5 features on the What's New page.

POSTED BY: Patrik Ekenberg
Answer
2 months ago

enter image description here - Congratulations! This post is now a Staff Pick as distinguished on your profile! Thank you, keep it coming!

POSTED BY: Moderation Team
Answer
2 months ago

Group Abstract Group Abstract