## Using Sympy for Biological ODEs Exponential and Logistic Growth solutions

I’ve been getting more into theoretical/analytical modeling lately, and I’ve been playing around with software to help me do some of the more complicated calculus involved (for two reasons: 1) I’m lazy, 2) My skills are very rusty). Python (of course) provides excellent symbolic capabilities through the Sympy module. I played around with it to try and get a feel for how it works, but I couldn’t find any help online, nor anyone who has posted a tutorial, on analyzing basic biological/ecological models with Sympy. So here is my version. Below, I solve both the exponential growth and logistic growth models using Sympy, then plot the results. Here is a step-by-step tutorial for Bio-Sympy.

Exponential Growth

Exponential growth is defined by the differential equation

$$\frac{dy(t)}{dt} = k*y(t)$$

and this ODE has the analytical solution of

$$y(t) = Y_0 e^{kt}$$

So how do we use Sympy to go from the ODE to the general solution? Well, like this.

First, gotta import the modules we need, and then initialize pretty printing just so the output is readable (see the Sympy docs for this):

import sympy as sm
import numpy as np
import matplotlib.pyplot as plt
sm.init_printing()

Next, define the variables k (the intrinsic growth rate) and t (for time). Then, make y a function of t

from sympy.abc import t, k
y = sm.Function('y')(t)

Then, define the derivative of y with respect to t (the left-hand side of the ODE), and then define the right-hand side of the ODE.

dy = y.diff(t)
rhs = k*y

We need to set these two quantities equal to one another, as in the ODE above, which we can do using a Sympy Equality

eq = sm.Eq(dy, rhs)

If you print eq, it should give you the differential equation. Now that we have the differential equation set up, all we need to do is solve. Since this is a simple one, Sympy can do it on its own with no hints or guesses:

sol = sm.dsolve(eq)
sol

Where sol should give you the analytical solution $$y(t) = C_1 e^{kt}$$. But $$C_1$$ here is just a constant, we want to put it in terms of the initial conditions. (I know this is trivial in this case, but bear with me). First, we need to find the initial conditions. We do so by substituting 0 in for t, and then setting that equal to n0.

t0 = sol.args[1].subs({'t': 0})
n0 = sm.symbols('n0')
eq_init = sm.Eq(n0, t0)

The object eq_init should now be $$n_0 = C_1$$. Great. Now, solve that in terms of $$C_1$$ and substitute it back into the original equation.

C1 = eq_init.args[1] # this chunk just isolates the C1 variable as a symbol so we can use it in Sympy
sm.solve(eq_init, C1)
init_solve= sm.solve(eq_init, C1)
final = sol.subs(C1, init_solve[0])

final is now $$y(t) = n_0 e^{kt}$$. Just like it should be! I’ll leave plotting that one up to you.

Logistic Growth

Logistic growth is defined by the differential equation

$$\frac{dy(t)}{dt} = ry(t)\left ( 1 -\frac{y(t)}{K} \right )$$

The steps for solving this are identical to the steps above:

# define the needed parameters
from sympy.abc import r, K, t
y = sm.Function('y')(t)
# define the differential equation
dy = y.diff(t)
rhs = r*y*(1 - y/K)
# define the equality
eq = sm.Eq(dy, rhs)
# find the general solution
sol = sm.dsolve(eq))

sol is now more complex:

$$y(t) = \frac{Ke^{C_1 K + rt}}{e^{C_1 K + rt} – 1}$$
# find out what C is in terms of y0
t0 = sol.args[1].subs({'t':0})
n0 = sm.symbols('n0')
eq_init = sm.Eq(n0, t0)
# it takes a little more work to isolate C1 here
# try each step for yourself to see what it does
C1 = t0.args[2].args[0].args[0]
t0_sol = sm.solve(eq_init, C1)

The initial conditions are also rather complex:

$$C_1 =\frac{\log \left ( \frac{-n_0)}{K-n_0} \right )}{K}$$

But we can substitute that back into the original solution to get the general solution in terms of the initial conditions

# substitute the expression for C1 back into the equation
final = sol.args[1].subs(C1, t0_sol[0])

where final is now

$$y(t) = \frac{-K n_0 e^{rt}}{ (K-n_0) \left ( – \frac{n_0 e^{rt}}{K-n_0} – 1 \right ) }$$

Holy cow! That’s a doozy, and it doesn’t look anything like the general solutions you’ll find elsewhere. Easy fix, just ask Sympy to simplify it!

final.simplify()

and we have

$$y(t) = \frac{ K n_0 e^{rt} }{ K + n_0 e^{rt} – n_0 }$$

which we can easily simplify further in the denominator ourselves:

$$y(t) = \frac{ K n_0 e^{rt} }{ K + n_0 (e^{rt} -1) }$$

and we have the general solution to logistic growth that everyone knows and love!

Viola, Sympy for solving biological ODEs

## Warming alters herbivore control of plant life history New paper in Ecology

Insect herbivores play an important role in plant populations. The can determine population size by consuming and killing plants. They determine plant fitness, either by consuming seeds, reducing photosynthesis rates by consuming leaves, or by forcing plants to invest limited resources into defensive chemistry rather than seed production.

Warming, caused by climate change, affects both plants and herbivores. Rising temperatures increase plant growth rates, affect the production of plant defensive chemicals, and can also affect plant seed production. On top of that, warming often increases herbivore metabolic rates, causing herbivores to increase their consumption of plant material. Such increased consumption rates may strengthen the effect of herbivores on plants, but the direct effects of warming on plants (i.e. higher growth rates), could offset the increased herbivore damage. My co-authors and I just published a new paper in the journal Ecology, titled “Responses of plant phenology, growth, defense, and reproduction to interactive effects of warming and insect herbivory“, that examines whether rising temperatures might disrupt the ability of insects to influence plant life history.

What we found surprised us. At normal temperatures, plants were able to compensate for herbivory.  Sure, plants exposed to insects were shorter and produced fewer fruits, but each fruit was packed with more seeds. As a result, total seed biomass was unaffected by insects (although seeds were smaller).

Warming changed all that.  Plants were shorter and they produced more fruits. However, fruits were smaller, and packed with many small seeds. It was quite astonishing, however, because herbivores no longer had any effect. Despite high leaf damage, plant life history was unaffected by the presence of herbivores.

Why would this happen, why would warming negate any influence of insects on plants? We don’t know for sure, but we suspect that warming reduced soil moisture (a common effect of rising temperatures). Low soil water content appears to have induced a stress response, causing stunted growth in the plants and altering their life history strategy. Interestingly, the stress response to low soil moisture appears to have superseded any effect of herbivores.

Herbivores play an incredibly important role in plant ecology; they drive plant population dynamics and evolution. Our results suggest that climate warming might drastically alter the role of insects in plant communities.

## Undergraduate Research Paper – Phosphorus and Grasshoppers

I’d like to congratulate my REU from last summer, Maddi Rode, whose paper “Prospective evidence for independent nitrogen and phosphorus limitation of grasshopper (Chorthippus curtipennis) growth in a tallgrass prairie” was just published in PLOS One

Nitrogen, a critical component of amino acids and proteins, has long been considered the primary limiting nutrient of terrestrial insects. Other nutrients have generally received much less attentions. However, phosphorus is a crucial component of larval growth, given the tight coupling between phosphorus-rich RNA and growth rates. Indeed, the strength of phosphorus limitation in terrestrial insects is often just as strong as nitrogen limitation. However, few studies have enriched plants with both nitrogen and phosphorus (separately and together) to determine the relative strengths of nutrient limitation.

Maddi spent the summer at Konza Prairie Biological Station doing just that. She enriched plots of Andropogon gerardii, or big bluestem, with nitrogen, phosphorus, or a combination of the two. We then tracked the growth of the marsh meadow grasshopper, Chorthippus curtipennis, under all conditions.

She found that nitrogen enrichment led to higher grasshopper growth rates. Surprisingly (or unsurprisingly to us), phosphorus enrichment stimulated grasshopper growth by nearly the exact same amount as nitrogen enrichment.

This work adds to the building body of literature that grasshoppers, and indeed most terrestrial insects, are limited by a suite of nutrients beyond simply phosphorus. What this means for herbivore feeding behavior and climate change remains to be seen…

## Effects of Herbivory on Ecology of Treefall Gaps

Naturally-occurring treefall gaps are an important part of forest ecology, playing a prominent role in the regeneration of both pioneer and non-pioneer tree species. Nate Lemoine is setting out to understand how insect herbivory plays a role in the growth and health of plants at treefall gaps. By caging small plots within gaps, he is deterring deer and other animals from eating the plants. He is also using herbicide to deter insects from some plots to compare to controlled plots where only water is sprayed.

By Dejeanne Doublet

## Caterpillar Food 101

By Dejeanne Doublet, intern Conducting research with insects means that you must take on the roles and duties of a caretaker.

This summer we worked with the caterpillar species Spodoptera exigua (beet armyworms) and their close relative Spodoptera frugiperda (fall armyworms). Both species were shipped to us in sheets of eggs containing roughly 1,000 caterpillars. Most caterpillars start out their lives as eggs on leaves. They usually don’t get to pick and choose their food at the beginning of their lives, and are usually forced to eat from the plant or tree where they hatched.

Some caterpillars will enter a wandering stage once they’re big enough. They may wander about from plant to plant, picking and choosing what they like best or they may stay put at one type of plant for the rest of their life.

• The first step involves mixing the agar into boiling water until it is completely dissolved. Then, the temperature should to reduced to 85 degrees before adding in the remaining ingredients.

When you’re a caterpillar shipped to a lab with 999 other caterpillars, though, you don’t necessarily get to have an all-you-can-buffet of cuisines. Our lab doesn’t quite function like a Subway and you can’t always have it your way. However, we do make sure you’re eating all your nutrients and vitamins with a homemade concoction of Lepidoptera food.

The recipe consists of more than a dozen ingredients that are simply mixed into boiling water. Click here for the full recipe. Once the ingredients are mixed together, they form a thick substance that cools down to form a tapioca pudding-like concoction.

We poured the finished food goo into 8 oz. plastic containers, allowed it to cool, then placed small pieces of the sheets containing caterpillars eggs on top of the food. We then placed the containers in a 30 ºC chamber under lights that mimic a 16-hour day and 8-hour night cycle. The caterpillars usually begin to hatch a day from when they arrive as eggs. Within a few days, they’re growing and eating and growing and eating.

## Good year for finding monarchs

Monarch caterpillars are not easy to come by. In the last five summers I’ve spent at the Smithsonian Environmental Research Center in Maryland, I’ve only come across several caterpillars per season. This year, our lab is full of monarch caterpillars with a count currently up to 13 caterpillars, three of which are forming their chrysalis shells and undergoing metamorphosis.

## Cheap and Effective Homemade Insect Clip Cages

### 2. Chop up the tube

The tube should be cut into 2″ wide slices. You can use scissors, a razor blade, Exacto knife, or anything else. I used a chop saw to slice through six tubes in a matter of minutes. Also, the chop saw gave me a nice clean, straight cut. A band saw would work even better than a chop saw.

### 3. Glue the slices onto window screening

You can do this however you like. My preferred way is to lay the window screen on a table, coat one side of the foam slice with contact cement, and simply lay the slice onto the screen. Wait a few minutes for the cement to dry. This works incredibly well if you’re mass producing hundreds of clip cages like I was. Make sure you do this in a well-ventilated area. You’ll want to give the glue several days to dry and air out all of the fumes before putting insects inside.

### 4. Cut the foam slices out of the window screen

This is pretty self-explanatory.

### 5. Use staples to connect two slices

I use three staples per clip cage. Put the two halves together (window screen facing out on both sides), and use the staples to fasten them together.

### 6. Place on plant with insect. Enjoy

Here is the final product in action. Each cage should cost about a dollar, if that.