r/Numpy•
Educational-Loan2938

How to loop over different values of c/r and solve the ODE once for each intervention

import numpy as np

from scipy.integrate import odeint

import matplotlib.pyplot as plt

# Parameters

r = 0.1

K = 10000

# Initial population

N0 = 100

# Time

tspan = np.linspace(0, 200, 1000)

# Values of c/r that we want to compare

ratios = [0, 0.5, 0.9, 1.0, 1.1, 1.5]

for ratio in ratios:

# Convert c/r into c

c = ratio * r

# Define ODE

def RHSodes(N, t):

dNdt = r*N*(1 - N/K) - c*N

return dNdt

# Solve ODE

N = odeint(RHSodes, N0, tspan)

# Plot this intervention

plt.plot(tspan, N[:,0], label=f'c/r = {ratio}')

plt.xlabel('Time')

plt.ylabel('Population N')

plt.legend()

plt.grid()

plt.show()

Posted Sep 30, 2026, 3:39 AM UTC

10
Loading comments…