Skip to Content

LOTKA_VOLTERRA

Overview

The LOTKA_VOLTERRA function numerically solves the classic Lotka-Volterra predator-prey system of ordinary differential equations, modeling the interaction between two species: prey and predator. The equations describe how the populations of prey (xx) and predator (yy) change over time:

dxdt=αxβxydydt=δxyγy\begin{align*} \frac{dx}{dt} &= \alpha x - \beta x y \\ \frac{dy}{dt} &= \delta x y - \gamma y \end{align*}

where α\alpha is the prey birth rate, β\beta is the predation rate coefficient, δ\delta is the predator reproduction rate per prey eaten, and γ\gamma is the predator death rate. The function uses SciPy’s ODE solver (scipy.integrate.solve_ivp ) to integrate the system from t_start to t_end. Only the basic two-species model is exposed; extensions for additional species, harvesting, or stochastic effects are not supported. The integration method can be selected from several common solvers, but only the most used options are exposed. This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=LOTKA_VOLTERRA(prey_initial, predator_initial, alpha, beta, delta, gamma, t_start, t_end, [timesteps], [solve_ivp_method])
  • prey_initial (float, required): Initial number of prey.
  • predator_initial (float, required): Initial number of predators.
  • alpha (float, required): Prey birth rate.
  • beta (float, required): Predation rate coefficient.
  • delta (float, required): Predator reproduction rate per prey eaten.
  • gamma (float, required): Predator death rate.
  • t_start (float, required): Start time for integration.
  • t_end (float, required): End time for integration.
  • timesteps (float, optional, default=10): Number of timesteps to solve for.
  • solve_ivp_method (string (enum), optional, default=‘RK45’): Integration method. Valid options: RK45, RK23, DOP853, Radau, BDF, LSODA.

The function returns a 2D array (table) with columns: t, Prey, Predator, representing time and the two populations at each step. If the input is invalid or integration fails, a string error message is returned.

Examples

Example 1: Basic Case (Default Method)

Inputs:

prey_initialpredator_initialalphabetadeltagammat_startt_endtimestepsmethod
40.09.00.10.020.010.10.020.010RK45

Excel formula:

=LOTKA_VOLTERRA(40.0, 9.0, 0.1, 0.02, 0.01, 0.1, 0.0, 20.0, 10)

Expected output:

tPreyPredator
0.00040.0009.000
2.22228.93015.700
4.44415.90020.570
6.6677.73721.200
8.8893.91419.210
11.112.21216.430
13.331.41613.680
15.561.01811.250
17.780.8089.190
20.000.6987.482

Example 2: RK23 Method

Inputs:

prey_initialpredator_initialalphabetadeltagammat_startt_endtimestepsmethod
40.09.00.10.020.010.10.020.010RK23

Excel formula:

=LOTKA_VOLTERRA(40.0, 9.0, 0.1, 0.02, 0.01, 0.1, 0.0, 20.0, 10, "RK23")

Expected output:

tPreyPredator
0.00040.0009.000
2.22228.93015.700
4.44415.90020.570
6.6677.73721.200
8.8893.91419.210
11.112.21216.430
13.331.41613.680
15.561.01811.250
17.780.8089.190
20.000.6987.482

Example 3: Longer Time Span

Inputs:

prey_initialpredator_initialalphabetadeltagammat_startt_endtimestepsmethod
50.010.00.10.020.010.10.040.010RK45

Excel formula:

=LOTKA_VOLTERRA(50.0, 10.0, 0.1, 0.02, 0.01, 0.1, 0.0, 40.0, 10)

Expected output:

tPreyPredator
0.00050.00010.000
4.44413.67026.570
8.8892.22922.360
13.330.66515.140
17.780.3469.911
22.220.2646.438
26.670.2584.176
31.110.2982.710
35.560.3821.764
40.000.5251.154

Example 4: All Arguments Specified

Inputs:

prey_initialpredator_initialalphabetadeltagammat_startt_endtimestepsmethod
30.05.00.150.0250.0150.120.010.010RK45

Excel formula:

=LOTKA_VOLTERRA(30.0, 5.0, 0.15, 0.025, 0.015, 0.12, 0.0, 10.0, 10, "RK45")

Expected output:

tPreyPredator
0.00030.0005.000
1.11129.9607.231
2.22227.80010.270
3.33323.51013.820
4.44418.04017.120
5.55612.81019.350
6.6678.70620.220
7.7785.87419.950
8.8894.03818.940
10.002.87217.550

Python Code

from scipy.integrate import solve_ivp import numpy as np def lotka_volterra(prey_initial, predator_initial, alpha, beta, delta, gamma, t_start, t_end, timesteps=10, solve_ivp_method='RK45'): """ Numerically solves the Lotka-Volterra predator-prey system of ordinary differential equations. Wraps: scipy.integrate.solve_ivp (SciPy ODE integration). See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html Args: prey_initial: Initial number of prey. predator_initial: Initial number of predators. alpha: Prey birth rate. beta: Predation rate coefficient. delta: Predator reproduction rate per prey eaten. gamma: Predator death rate. t_start: Start time of integration. t_end: End time of integration. timesteps: Number of timesteps to solve for. Default is 10. solve_ivp_method: Integration method ('RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA'). Default is 'RK45'. Returns: 2D list with header row: t, Prey, Predator. Each row contains time and population values, or an error message (str) if input is invalid. This example function is provided as-is without any representation of accuracy. """ # Validate input types try: prey0 = float(prey_initial) predator0 = float(predator_initial) a = float(alpha) b = float(beta) d = float(delta) g = float(gamma) t0 = float(t_start) t1 = float(t_end) ntp = int(timesteps) except Exception: return "Invalid input: all parameters must be numbers." if t1 <= t0: return "Invalid input: t_end must be greater than t_start." if ntp <= 0: return "Invalid input: timesteps must be a positive integer." if prey0 < 0 or predator0 < 0: return "Invalid input: initial populations must be non-negative." if a < 0 or b < 0 or d < 0 or g < 0: return "Invalid input: rate parameters must be non-negative." # Validate solve_ivp_method allowed_methods = ['RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA'] if solve_ivp_method not in allowed_methods: return f"Invalid input: solve_ivp_method must be one of {allowed_methods}." # Create time array for evaluation t_eval = np.linspace(t0, t1, ntp) # Lotka-Volterra ODE system def lv_ode(t, y): prey, predator = y dprey_dt = a * prey - b * prey * predator dpredator_dt = d * prey * predator - g * predator return [dprey_dt, dpredator_dt] # Integrate try: sol = solve_ivp( lv_ode, [t0, t1], [prey0, predator0], method=solve_ivp_method, t_eval=t_eval, vectorized=False, rtol=1e-6, atol=1e-8 ) except Exception as e: return f"solve_ivp error: {e}" if not sol.success: return f"Integration failed: {sol.message}" # Prepare output: header row + data rows result = [["t", "Prey", "Predator"]] for i in range(len(sol.t)): t_val = float(sol.t[i]) prey_val = float(sol.y[0][i]) predator_val = float(sol.y[1][i]) # Disallow nan/inf if any([ t_val != t_val or t_val == float('inf') or t_val == float('-inf'), prey_val != prey_val or prey_val == float('inf') or prey_val == float('-inf'), predator_val != predator_val or predator_val == float('inf') or predator_val == float('-inf') ]): return "Invalid output: nan or inf encountered in results." result.append([t_val, prey_val, predator_val]) return result

Example Workbook

Link to Workbook 

Last updated on