Lesson 8 of 15

Systems of ODEs

Systems of ODEs

A single ODE describes one unknown. Many real problems involve multiple quantities that interact — requiring a system of ODEs:

dy1dt=f1(t,y1,y2,)\frac{dy_1}{dt} = f_1(t, y_1, y_2, \ldots) dy2dt=f2(t,y1,y2,)\frac{dy_2}{dt} = f_2(t, y_1, y_2, \ldots)

We represent the state as a vector y=[y1,y2,]\mathbf{y} = [y_1, y_2, \ldots] and the derivative function returns a vector too:

dydt=f(t,y),yRn\frac{d\mathbf{y}}{dt} = f(t, \mathbf{y}), \quad \mathbf{y} \in \mathbb{R}^n

Vector Euler's Method

Exactly the same formula, applied component-wise:

def euler_system(f, t0, y0, t_end, n):
    h = (t_end - t0) / n
    t = t0
    y = list(y0)
    ys = [list(y)]
    for _ in range(n):
        dydt = f(t, y)
        y = [yi + h * di for yi, di in zip(y, dydt)]
        t += h
        ys.append(list(y))
    return ys

Example: Predator Growth

# Rabbit grows, fox grows proportionally
# dy1/dt = 2*y1,  dy2/dt = y1
def f(t, y):
    return [2*y[0], y[0]]

All future lessons use this vector pattern: simple harmonic motion, predator-prey, epidemic models.

Your Task

Implement euler_system(f, t0, y0, t_end, n) where f(t, y) returns a list of derivatives and y0 is a list of initial values. Return a list of n+1n+1 state vectors.

Pyodide loading...
Loading...
Click "Run" to execute your code.