Skip to Content

SEIR

Overview

The SEIR function numerically solves the SEIR system of ordinary differential equations for infectious disease modeling, describing the dynamics of susceptible, exposed, infected, and recovered populations. The SEIR model is a classic compartmental model in epidemiology, governed by the following equations:

dSdt=βSI/NdEdt=βSI/NσEdIdt=σEγIdRdt=γI\begin{align*} \frac{dS}{dt} &= -\beta S I / N \\ \frac{dE}{dt} &= \beta S I / N - \sigma E \\ \frac{dI}{dt} &= \sigma E - \gamma I \\ \frac{dR}{dt} &= \gamma I \end{align*}

where SS is the number of susceptible individuals, EE is exposed (infected but not yet infectious), II is infectious, RR is recovered, NN is the total population, β\beta is the infection rate, σ\sigma is the rate at which exposed individuals become infectious, and γ\gamma is the recovery rate. The function uses SciPy’s ODE solver (scipy.integrate.solve_ivp ) to integrate the system over a specified time interval. Only the most commonly used parameters are exposed; features such as stochasticity, age structure, or spatial 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:

=SEIR(s_initial, e_initial, i_initial, r_initial, beta, gamma, sigma, t_start, t_end, [timesteps], [solve_ivp_method])
  • s_initial (float, required): Initial number of susceptible individuals.
  • e_initial (float, required): Initial number of exposed individuals.
  • i_initial (float, required): Initial number of infected individuals.
  • r_initial (float, required): Initial number of recovered individuals.
  • beta (float, required): Infection rate parameter.
  • gamma (float, required): Recovery rate parameter.
  • sigma (float, required): Rate at which exposed individuals become infectious.
  • t_start (float, required): Start time of integration.
  • t_end (float, required): End time of integration.
  • timesteps (int, 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, S, E, I, R, representing time and compartment values 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:

s_initiale_initiali_initialr_initialbetagammasigmat_startt_endtimesteps
990.00.010.00.00.30.10.20.010.010

Excel formula:

=SEIR(990.0, 0.0, 10.0, 0.0, 0.3, 0.1, 0.2, 0.0, 10.0, 10)

Output:

tSEIR
0.000990.0000.00010.0000.000
1.111986.8462.8249.2661.064
2.222983.8394.9579.1232.081
3.333980.8166.6819.3963.107
4.444977.6628.1819.9774.180
5.556974.2889.58010.7995.333
6.667970.62710.96011.8266.588
7.778966.61712.37713.0387.967
8.889962.20713.87214.4299.492
10.000957.34515.47615.99911.180

Example 2: With Optional Method (RK23)

Inputs:

s_initiale_initiali_initialr_initialbetagammasigmat_startt_endtimestepsmethod
990.00.010.00.00.30.10.20.010.010RK23

Excel formula:

=SEIR(990.0, 0.0, 10.0, 0.0, 0.3, 0.1, 0.2, 0.0, 10.0, 10, "RK23")

Output:

tSEIR
0.000990.0000.00010.0000.000
1.111986.8462.8249.2661.064
2.222983.8394.9579.1232.081
3.333980.8166.6819.3963.107
4.444977.6628.1819.9774.180
5.556974.2889.58010.7995.333
6.667970.62710.96011.8266.588
7.778966.61712.37713.0387.967
8.889962.20713.87214.4299.492
10.000957.34515.47615.99911.180

Example 3: Longer Time Span

Inputs:

s_initiale_initiali_initialr_initialbetagammasigmat_startt_endtimesteps
990.00.010.00.00.30.10.20.050.010

Excel formula:

=SEIR(990.0, 0.0, 10.0, 0.0, 0.3, 0.1, 0.2, 0.0, 50.0, 10)

Output:

tSEIR
0.000990.0000.00010.0000.000
5.556974.2889.58010.7995.333
11.111951.98017.21217.75513.054
16.667915.75628.48829.77125.985
22.222858.73545.14148.70947.415
27.778774.71967.17576.37181.735
33.333662.60291.200112.355133.844
38.889531.721109.582151.502207.195
44.444401.445114.072183.605300.878
50.000291.178102.642198.257407.923

Example 4: Different Parameters

Inputs:

s_initiale_initiali_initialr_initialbetagammasigmat_startt_endtimesteps
950.020.030.00.00.50.20.30.010.010

Excel formula:

=SEIR(950.0, 20.0, 30.0, 0.0, 0.5, 0.2, 0.3, 0.0, 10.0, 10)

Output:

tSEIR
0.000950.00020.00030.0000.000
1.111934.06727.89031.2786.765
2.222917.24234.32334.39914.036
3.333898.84040.28638.73222.143
4.444878.45946.24843.97631.317
5.556855.85652.41549.98641.744
6.667830.89458.84156.68153.584
7.778803.52465.49464.00066.982
8.889773.78072.27771.87382.070
10.000741.78779.04780.20698.960

Python Code

from scipy.integrate import solve_ivp import numpy as np def seir(s_initial, e_initial, i_initial, r_initial, beta, gamma, sigma, t_start, t_end, timesteps=10, solve_ivp_method='RK45'): """ Numerically solves the SEIR system of ordinary differential equations for infectious disease modeling. Args: s_initial: Initial number of susceptible individuals. e_initial: Initial number of exposed individuals. i_initial: Initial number of infected individuals. r_initial: Initial number of recovered individuals. beta: Infection rate parameter. gamma: Recovery rate parameter. sigma: Rate at which exposed individuals become infectious. 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, S, E, I, R. Each row contains time and compartment values, or an error message (str) if input is invalid. This example function is provided as-is without any representation of accuracy. """ # Validate inputs try: s0 = float(s_initial) e0 = float(e_initial) i0 = float(i_initial) r0 = float(r_initial) beta_ = float(beta) gamma_ = float(gamma) sigma_ = float(sigma) t0 = float(t_start) t1 = float(t_end) ntp = int(timesteps) except Exception: return "Invalid input: all initial values, parameters, and timesteps 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 any(x < 0 for x in [s0, e0, i0, r0, beta_, gamma_, sigma_]): return "Invalid input: all initial values and parameters must be non-negative." 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) N = s0 + e0 + i0 + r0 def seir_ode(t, y): S, E, I, R = y dSdt = -beta_ * S * I / N dEdt = beta_ * S * I / N - sigma_ * E dIdt = sigma_ * E - gamma_ * I dRdt = gamma_ * I return [dSdt, dEdt, dIdt, dRdt] try: sol = solve_ivp( seir_ode, [t0, t1], [s0, e0, i0, r0], method=solve_ivp_method, dense_output=False, rtol=1e-6, atol=1e-8, t_eval=t_eval ) except Exception as e: return f"ODE integration error: {e}" if not sol.success: return f"ODE integration failed: {sol.message}" result = [["t", "S", "E", "I", "R"]] for i in range(len(sol.t)): t_val = float(sol.t[i]) s_val = float(sol.y[0][i]) e_val = float(sol.y[1][i]) i_val = float(sol.y[2][i]) r_val = float(sol.y[3][i]) # Disallow nan/inf if any([ not isinstance(t_val, float) or not isinstance(s_val, float) or not isinstance(e_val, float) or not isinstance(i_val, float) or not isinstance(r_val, float), t_val != t_val or s_val != s_val or e_val != e_val or i_val != i_val or r_val != r_val, abs(t_val) == float('inf') or abs(s_val) == float('inf') or abs(e_val) == float('inf') or abs(i_val) == float('inf') or abs(r_val) == float('inf') ]): return "Invalid output: nan or inf detected." result.append([t_val, s_val, e_val, i_val, r_val]) return result

Example Workbook

Link to Workbook 

Last updated on