Simulation of natural systems – Part II

In my previous article I was explaining the swarming behavior of fish in a predator-prey system. There you can read about basic rules underlying the mechanism of swarm formation. The next step now is to make a computer simulation of such a system by using the Matlab software.

Without any previous experience with programming languages, this first step in writing a programming code is the most difficult one. How to define living organisms? How to make them move? Or how to tell them to form swarms? Answers to those questions will be presented in this article.


Assign specific characteristics to fish and make them move….

The Matlab program for this predator-prey system of different fish will consist of three main parts – “Parameters”, “Initial conditions” and the “Main time loop”. These are the sections where prey and predator fish are firstly defined and given some initial properties, such as location, direction of movement, speed of movement and instructions to form groups (to avoid the predator or to chase the prey). In this particular case, the initial distribution and velocities for both fish types will be random, enabling the fish to swim in all directions, instead of starting collectively in the same direction. In order to achieve a predator-prey dynamics, all forces of attraction and repulsion have to be defined among prey fish alone, among predator fish alone and also inbetween them. A simple linear force calculation based on the distance is sufficient to create the basic rule of swarming. The following figures represent different forces of attraction or repulsion between fish.

2016-02-25 (2)

Force calculations required for the simulation

  • Graph 1 shows forces between prey fish. When the distance between them is bigger than 50 m, they do not “see each other”. Therefore, this force is zero. As soon as they “see each other” (distance < 50 m) an attractive force is „bringing them together“ resulting in swarm formation. When they come as close as 10 m, they stay in the swarm by adapting their speed and direction of movement to the mutual one of the swarm.
  • Graph 2 represents forces between predator fish. As in the previous case, the force is zero when the predator fish do not “see each other” (distance > 50 m). Otherwise, there is a repulsion force which makes them to swim away from each other in opposite directions (too many predators in one place results in less food for each of them).
  • Graph 3 explains the attractive forces from prey to predator fish. When predator fish “see” prey fish (distance < 100 m), an attraction force represents their hunting behavior (they follow the prey fish).
  • Graph 4 shows the repulsion force from predator to prey fish (in comparison to Graph 3). In this case, a repulsive force makes the prey fish escape from the predators.


After defining these rules of attraction and repulsion, some mathematical definitions of movement are needed. The accelerations, velocities and positions are written in a way that for every calculation step, the previous value is used and recalculated. This can be done by using first and second order derivatives in basic equations for calculating acceleration, velocity and speed. I do not want to go much in details here, let us rather check what happens after running such program (Figure 2).


The beginning of a Matlab code simulating a predator-prey system


After  every “error message” is fixed, the simulation works….

As the simulation begins, the prey fish swim towards each other and away from the predators in order to form swarms as fast as possible. There is no “eat fish” function in the program – therefore an individual fish or a swarm never disappears once it gets caught, it only escapes. In the moment when a fish enters a swarm, its individual characteristics disappear – a new body (the swarm with an additional fish) is created. Due to a simple starting definition of forces, a non-realistic effect is observed: big swarms simply go “through” a predator without trying to avoid it. A better force calculation (e.g. quadratic or exponential functions) would probably result in fish swarms trying to avoid collisions with predators. Behavior of the predators is in some aspects similar to the one of the prey. In the beginning they try to avoid meeting each other because they have enough choice to chase many other fish. A soon as they meet bigger swarms, they do not care anymore if other predators are close to them. This resembles a natural system where lesser food availability brings competitors together.


What is this useful for?

The simulation described in this article serves for a basic understanding of what is possible in terms of simulation natural systems with computer programmes and in which direction the research is going. There are many different possibilities to apply swarming behavior in technology and unsurprisingly, numerous projects have already shown some progress. Examples are swarm robotics, artificial intelligence, cooperative control mechanisms [1], etc…

All natural systems, not only predator-prey relations, exhibit some kind of intelligence of a superorganism (leafcutter ants, termites, bees, different symbiosis, rainforest, ecosystem,…) [2]. Characteristics of individuals in such systems become less important as the cumulative behavior has beneficial effects. Studying that is not only interesting, it also has a useful potential for technical developments – if only we are able to recognize and fully understand the functioning principles of natural systems.




Anja Boisselet

My name is Anja and I come from one of the most beautiful places on the world – Bled, a small town in a very small country called Slovenia but very beautiful! Imagine that in two hours you can traverse this country, passing high mountains, numerous rivers and lakes, valleys, vineyard hills, and finish on the coast. No wonder why I decided to start my studies in relation to nature.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert