.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/lesson2/plot_rabbitsandfoxes.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_lesson2_plot_rabbitsandfoxes.py: .. _rabbitsandfoxes: Rabbits and foxes ================= In this section we will learn how to write `differential equations `_ models to show the rate of change of populatins of predators and prey. We will simulate the model using Python. Then we draw the phase plane for the model, just as Parker does in the book in the image shown below. .. image:: ../../images/lesson2/ParkerCircles.png Differential equations ---------------------- In the book, I write Lotka's equations in the form of chemical reactions, e.g. .. math:: \mbox{F} + \mbox{R} \rightarrow 2 \mbox{F} This means that an F and R together becomes two F's. My character from Santa Fe, Professor Parker expalins as follows, .. image:: ../../images/lesson2/ParkerExplains.png While in Leipzig, A. J. Lotka learnt how chemical reactions can be used to specify the rate of change of populations, i.e. in terms of the derivates above, using an approach, known as the law of mass action. The idea is to think about the rate at which these chemical reactions take place. For the first reaction .. math:: \mbox{R} \xrightarrow{a} 2 \mbox{R} We can think of :math:`a` as being the rate of reproduction of the rabbits, how many baby rabbits an adult rabbit has per day. So, the rabbits grow according to .. math:: :label: rabbitgrow \frac{dR}{dt} = a R This equation denotes the rate of change of :math:`R` over time. the top part of the fraction :math:`dR` denotes the change in rabbits, :math:`R`, and the bottom part, :math:`dt`, denotes the change in time, :math:`t`. Differentiation in school ------------------------- This section is somewhat of an aside, but it might be useful if you have studied differentiation before. I remember that after learning about differentiation in school, I found the form of differential equations above to be a bit strange when I firstencountered it. In school we might have a function that looks like, for example, .. math:: :label: timeint X(t) = b t^2 then take the derivative to get .. math:: :label: timegrow \frac{dX}{dt} = 2 b t This is also a differential equation. It says that the rate of change of :math:`X` over time is proportional to time. The difference between equation :eq:`timegrow` and :eq:`rabbitgrow` is that :eq:`timegrow` says that rabbits grow in proportion to time, while :eq:`rabbitgrow` says that rabbits grow proportionally to the number of rabbits. In the case that rabbits grow in proportion to time, then we say that :eq:`timeint` is the solution to equation :eq:`timegrow` since it tells us how many rabbits there will be at any point in time. As yet, we haven't found a solution to equation :eq:`rabbitgrow`. I think this is where differential equations can be a bit confusing, because in school we are usually given :eq:`timeint` and asked to find :eq:`timegrow`. For most differential equations it is the other way round. We are given equation :eq:`rabbitgrow` and asked to find the the number of rabbits :math:`R` as a function of time. The solution to equation :eq:`timegrow` is in fact, .. math:: :label: expint R(t) = \exp(at) assuming that :math:`R(0)=1` (see `here `_ for an explanation of how we get this solution). In both these cases, we can find an explicit solution for :math:`R(t)` in terms of :math:`t`. By explicit here I mean that if you tell me a value of :math:`t`, I can put it in to :eq:`expint` and tell you the number of rabbits. It is not always the case that we can find an explicit solution. Indeed, for most differential equation models we can't find solutions of this form. This is the case for the rabbits and foxes model which we now look at. We won't be able to find an explicit mathematical expression for rabbits and foxes at any time, but we can understand the dynamics of rabbits and foxes. Back to rabbits anf foxes ------------------------- We have an equation for growth of rabbits, now we need to have equations that include foxes. In chemical reaction form these are, .. math:: \mbox{F} + \mbox{R} \xrightarrow{b} 2 \mbox{F} for foxes eating rabbits and .. math:: \mbox{F} \xrightarrow{d} \mbox{D} for foxes dying. Converted to differential equations, the rate of change for rabbits becomes .. math:: :label: rabbits \frac{dR}{dt} = \underbrace{a R}_{\mbox{R} \xrightarrow{a} 2 \mbox{R}} - \underbrace{b R F}_{\mbox{F} + \mbox{R} \xrightarrow{b} 2 \mbox{F}} Similarly, we can write the rate of change of foxes as .. math:: :label: foxes \frac{dF}{dt} = \underbrace{c R F}_{\mbox{F} + \mbox{R} \xrightarrow{c} 2 \mbox{F}} - \underbrace{d F}_{\mbox{F} \xrightarrow{d} \mbox{D}} Notice that we have a different rate parameter for the death of rabbits (:math:`b`) than for the birth of foxes (:math:`c`). This is because it takes more than one rabbit to feed a fox and we set the parameters so that :math:`c`. By dividing the rabbit equation by the fox equation he got .. math:: \frac{dR}{dF} = \frac{aR -bRF}{cRF - d F} We can then rearrange this equation to get .. math:: \left(c -d/R \right) dR = \left(a/F -b \right) dF Integrating both sides of this equation we get .. math:: cR -d\log(R) = a \log(F) - b F + C where :math:`C` is the constant of integration. This last equation tells us a relationship that must always hold between rabbits and foxes. To understand what the relationship implies, imagine the equation above was simply :math:`Y+X=C` instead. This would imply the total number of rabbits and foxes is equal to C=10. So, if :math:`C=10` then we could have :math:`Y=3` foxes and :math:`X=7` rabbits (because 3+7=10), or 6 foxes and 4 rabbits (because 6+4=10), but we couldn’t have :math:`Y=6` foxes and :math:`X=7` rabbits (because 6+7≠10). In our case, the relationship in the equation is more complicated, involving logarithms, but the idea is the same: for any particular value of C all values of :math:`R` and :math:`F` must obey the equation .. math:: C = cR + b F -d\log(R) - a \log(F) This is the case for the cycle we created in our numerical solution above. Every point on the predator-prey cycle fulfills the condition above. Here is how Lotka presented his result in the `original article `_. .. image:: ../../images/lesson2/LotkaCurve.png While Lotka didn't have a computer to simulate the equations, he could see using this argument that the cycles would remain finite. Neither the prey or predator populations would increase without bound. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.362 seconds) .. _sphx_glr_download_gallery_lesson2_plot_rabbitsandfoxes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rabbitsandfoxes.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rabbitsandfoxes.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_