← Math|33 of 100
Calculus

Differential Equations

Understand ODEs, numerical solvers, and their applications in dynamical systems and neural ODEs.

📂 Dynamics📖 Lesson 33 of 100🎓 Free Course

Advertisement

Differential Equations

ℹ️ Why It Matters

Differential equations are the language of change. They describe how physical, biological, economic, and computational systems evolve over time. In modern AI/ML, differential equations underpin dynamical systems modeling, neural ODEs (continuous-depth networks), diffusion models (score-based generative models), and physics-informed neural networks (PINNs). Mastering both analytical and numerical methods gives you the tools to model complex real-world phenomena and build next-generation machine learning architectures.


What is a Differential Equation

DfDifferential Equation

A differential equation is an equation that relates a function to one or more of its derivatives. It expresses how a quantity changes in response to conditions. Formally, if y=y(x)y = y(x), a differential equation involves yy, yy', yy'', etc.

General Form

F ⁣(x,  y,  y,  y,  ,  y(n))=0F\!\left(x,\; y,\; y',\; y'',\; \dots,\; y^{(n)}\right) = 0

Here,

  • yy=Unknown function (dependent variable)
  • xx=Independent variable
  • y,y,...y', y'', ...=Derivatives of y with respect to x
  • nn=Order of the differential equation

ℹ️ Analogy

Think of a differential equation as a rulebook: it tells you how a system changes at every moment. The solution is the actual trajectory the system follows.


Ordinary vs Partial Differential Equations

DfOrdinary Differential Equation (ODE)

An ODE involves derivatives with respect to a single independent variable. All derivatives are ordinary (total) derivatives.

ODE Example

dydx=3x2+2y\frac{dy}{dx} = 3x^2 + 2y

Here,

  • yy=Function of x only
  • dy/dxdy/dx=Ordinary derivative

DfPartial Differential Equation (PDE)

A PDE involves partial derivatives with respect to two or more independent variables. PDEs describe fields and distributed systems.

PDE Example (Heat Equation)

ut=α2ux2\frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2}

Here,

  • u(x,t)u(x,t)=Temperature at position x and time t
  • αα=Thermal diffusivity constant
  • /t∂/∂t=Partial derivative with respect to time
  • 2/x2∂²/∂x²=Second spatial partial derivative

📋ODE vs PDE Comparison

FeatureODEPDE
Independent variables12+
DerivativesOrdinary (∂)Partial (∂)
Typical usePopulation growth, circuitsHeat flow, wave propagation
SolutionFunction of 1 variableFunction of multiple variables
Common examplesdydx=ky\frac{dy}{dx}=kyut=α2u\frac{\partial u}{\partial t}=\alpha\nabla^2 u

Order and Degree

DfOrder of a Differential Equation

The order is the highest derivative appearing in the equation. For example, y+y=0y''' + y' = 0 is third-order.

DfDegree of a Differential Equation

The degree is the power of the highest-order derivative after the equation is cleared of fractions and radicals in the derivatives. For example, (y)2+y=0(y'')^2 + y = 0 has degree 2.

📝Identifying Order and Degree

Equation: (y)3+(y)2+y+y=0(y''')^3 + (y'')^2 + y' + y = 0

  • Order: 3 (highest derivative is yy''')
  • Degree: 3 (power of yy''' is 3)

Equation: y=1+(y)2y'' = \sqrt{1 + (y')^2}

  • First, square both sides to remove radical: (y)2=1+(y)2(y'')^2 = 1 + (y')^2
  • Order: 2, Degree: 2

Separable Equations

DfSeparable Equation

A first-order ODE is separable if it can be written as:

dydx=g(x)h(y)\frac{dy}{dx} = g(x)\,h(y)

The variables can be separated to opposite sides of the equation.

Separation of Variables

1h(y)dy=g(x)dx1h(y)dy=g(x)dx+C\frac{1}{h(y)}\,dy = g(x)\,dx \quad \Longrightarrow \quad \int \frac{1}{h(y)}\,dy = \int g(x)\,dx + C

Here,

  • g(x)g(x)=Function of x only
  • h(y)h(y)=Function of y only
  • CC=Constant of integration

📝Solve a Separable Equation

Problem: Solve dydx=xy\frac{dy}{dx} = \frac{x}{y} with y(0)=2y(0) = 2.

💡Solution

  1. Separate: ydy=xdxy\,dy = x\,dx
  2. Integrate both sides: ydy=xdx\int y\,dy = \int x\,dx
  3. y22=x22+C\frac{y^2}{2} = \frac{x^2}{2} + C
  4. Apply y(0)=2y(0)=2: 42=0+CC=2\frac{4}{2} = 0 + C \Rightarrow C = 2
  5. Answer: y2=x2+4y^2 = x^2 + 4, or y=x2+4y = \sqrt{x^2 + 4}

📝Another Separable Equation

Problem: Solve dydx=ycosx\frac{dy}{dx} = y\cos x with y(0)=3y(0) = 3.

💡Solution

  1. Separate: dyy=cosxdx\frac{dy}{y} = \cos x\,dx
  2. Integrate: lny=sinx+C\ln|y| = \sin x + C
  3. Exponentiate: y=Aesinxy = Ae^{\sin x} where A=eCA = e^C
  4. Apply y(0)=3y(0)=3: 3=Ae0A=33 = A e^0 \Rightarrow A = 3
  5. Answer: y=3esinxy = 3e^{\sin x}

First-Order Linear Equations

DfFirst-Order Linear ODE

A first-order linear ODE has the standard form:

dydx+P(x)y=Q(x)\frac{dy}{dx} + P(x)\,y = Q(x)

Integrating Factor Method

μ(x)=eP(x)dx\mu(x) = e^{\int P(x)\,dx}

Here,

  • μ(x)μ(x)=Integrating factor
  • P(x)P(x)=Coefficient of y in standard form
  • Q(x)Q(x)=Forcing term (right-hand side)

ℹ️ Method

Multiply both sides by μ(x)\mu(x), then the left side becomes ddx[μ(x)y]\frac{d}{dx}[\mu(x)\,y]. Integrate both sides to obtain the general solution.

General Solution

y(x)=1μ(x)[μ(x)Q(x)dx+C]y(x) = \frac{1}{\mu(x)}\left[\int \mu(x)\,Q(x)\,dx + C\right]

Here,

  • μ(x)μ(x)=Integrating factor
  • Q(x)Q(x)=Forcing term
  • CC=Constant of integration

📝Solve Using Integrating Factor

Problem: Solve dydx+2y=4x\frac{dy}{dx} + 2y = 4x with y(0)=1y(0) = 1.

💡Solution

  1. Identify: P(x)=2P(x) = 2, Q(x)=4xQ(x) = 4x
  2. Integrating factor: μ(x)=e2dx=e2x\mu(x) = e^{\int 2\,dx} = e^{2x}
  3. Multiply both sides: e2xdydx+2e2xy=4xe2xe^{2x}\frac{dy}{dx} + 2e^{2x}y = 4xe^{2x}
  4. Left side = ddx[e2xy]\frac{d}{dx}[e^{2x}y]
  5. Integrate: e2xy=4xe2xdxe^{2x}y = \int 4xe^{2x}\,dx
  6. Integration by parts: 4xe2xdx=2xe2xe2x+C\int 4xe^{2x}\,dx = 2xe^{2x} - e^{2x} + C
  7. e2xy=2xe2xe2x+Ce^{2x}y = 2xe^{2x} - e^{2x} + C
  8. y=2x1+Ce2xy = 2x - 1 + Ce^{-2x}
  9. Apply y(0)=1y(0)=1: 1=1+CC=21 = -1 + C \Rightarrow C = 2
  10. Answer: y=2x1+2e2xy = 2x - 1 + 2e^{-2x}

Exact Equations

DfExact Equation

A first-order ODE of the form M(x,y)dx+N(x,y)dy=0M(x,y)\,dx + N(x,y)\,dy = 0 is exact if:

My=Nx\frac{\partial M}{\partial y} = \frac{\partial N}{\partial x}

This means there exists a potential function Ψ(x,y)\Psi(x,y) such that Ψx=M\frac{\partial \Psi}{\partial x} = M and Ψy=N\frac{\partial \Psi}{\partial y} = N.

Solution of Exact Equation

Ψ(x,y)=C\Psi(x, y) = C

Here,

  • Ψ(x,y)Ψ(x,y)=Potential function
  • MM=∂Ψ/∂x
  • NN=∂Ψ/∂y

📝Solve an Exact Equation

Problem: Solve (2xy+3)dx+(x2+4y)dy=0(2xy + 3)\,dx + (x^2 + 4y)\,dy = 0.

💡Solution

  1. M=2xy+3M = 2xy + 3, N=x2+4yN = x^2 + 4y
  2. Check: My=2x\frac{\partial M}{\partial y} = 2x, Nx=2x\frac{\partial N}{\partial x} = 2x ✓ Exact!
  3. Find Ψ\Psi: Integrate MM w.r.t. xx: Ψ=x2y+3x+g(y)\Psi = x^2y + 3x + g(y)
  4. Differentiate w.r.t. yy: Ψy=x2+g(y)=N=x2+4y\frac{\partial \Psi}{\partial y} = x^2 + g'(y) = N = x^2 + 4y
  5. So g(y)=4yg(y)=2y2g'(y) = 4y \Rightarrow g(y) = 2y^2
  6. Answer: x2y+3x+2y2=Cx^2y + 3x + 2y^2 = C

Second-Order Linear Equations

DfSecond-Order Linear ODE

A second-order linear ODE has the form:

ay+by+cy=0(homogeneous)a\,y'' + b\,y' + c\,y = 0 \quad \text{(homogeneous)}

where aa, bb, cc are constants.

Characteristic Equation

ar2+br+c=0ar^2 + br + c = 0

Here,

  • a,b,ca, b, c=Coefficients from the ODE
  • rr=Root of the characteristic equation

ℹ️ Solution Forms

Discriminant Δ=b24ac\Delta = b^2 - 4acRootsSolution Form
Δ>0\Delta > 0Two real roots r1,r2r_1, r_2y=C1er1x+C2er2xy = C_1 e^{r_1 x} + C_2 e^{r_2 x}
Δ=0\Delta = 0Repeated root rry=(C1+C2x)erxy = (C_1 + C_2 x)e^{rx}
Δ<0\Delta < 0Complex r=α±βir = \alpha \pm \beta iy=eαx(C1cosβx+C2sinβx)y = e^{\alpha x}(C_1 \cos\beta x + C_2 \sin\beta x)

📝Solve a Second-Order ODE

Problem: Solve y5y+6y=0y'' - 5y' + 6y = 0 with y(0)=2y(0)=2, y(0)=3y'(0)=3.

💡Solution

  1. Characteristic equation: r25r+6=0r^2 - 5r + 6 = 0
  2. Factor: (r2)(r3)=0r1=2,r2=3(r-2)(r-3) = 0 \Rightarrow r_1=2, r_2=3
  3. General solution: y=C1e2x+C2e3xy = C_1 e^{2x} + C_2 e^{3x}
  4. Apply y(0)=2y(0)=2: C1+C2=2C_1 + C_2 = 2
  5. y=2C1e2x+3C2e3xy' = 2C_1 e^{2x} + 3C_2 e^{3x}
  6. Apply y(0)=3y'(0)=3: 2C1+3C2=32C_1 + 3C_2 = 3
  7. Solve: C1=3C_1 = 3, C2=1C_2 = -1
  8. Answer: y=3e2xe3xy = 3e^{2x} - e^{3x}

📝Complex Roots

Problem: Solve y+4y=0y'' + 4y = 0 with y(0)=1y(0)=1, y(0)=0y'(0)=0.

💡Solution

  1. Characteristic equation: r2+4=0r=±2ir^2 + 4 = 0 \Rightarrow r = \pm 2i
  2. General solution: y=C1cos2x+C2sin2xy = C_1 \cos 2x + C_2 \sin 2x
  3. Apply y(0)=1y(0)=1: C1=1C_1 = 1
  4. y=2C1sin2x+2C2cos2xy' = -2C_1 \sin 2x + 2C_2 \cos 2x
  5. Apply y(0)=0y'(0)=0: 2C2=0C2=02C_2 = 0 \Rightarrow C_2 = 0
  6. Answer: y=cos2xy = \cos 2x

Homogeneous vs Non-Homogeneous Equations

DfHomogeneous Equation

A linear ODE is homogeneous if the right-hand side (forcing term) is zero:

ay+by+cy=0a\,y'' + b\,y' + c\,y = 0

The solution is a sum of complementary solutions.

DfNon-Homogeneous Equation

A linear ODE is non-homogeneous if there is a non-zero forcing term:

ay+by+cy=f(x)a\,y'' + b\,y' + c\,y = f(x)

The general solution is y=yh+ypy = y_h + y_p, where yhy_h is the homogeneous solution and ypy_p is a particular solution.

Method of Undetermined Coefficients

yp=guess based on form of f(x)y_p = \text{guess based on form of } f(x)

Here,

  • yhy_h=Homogeneous (complementary) solution
  • ypy_p=Particular solution
  • f(x)f(x)=Non-homogeneous forcing term

ℹ️ Undetermined Coefficients Table

Form of f(x)f(x)Guess for ypy_p
ekxe^{kx}AekxAe^{kx}
cos(kx)\cos(kx) or sin(kx)\sin(kx)Acos(kx)+Bsin(kx)A\cos(kx) + B\sin(kx)
Polynomial degree nnanxn++a1x+a0a_nx^n + \dots + a_1x + a_0
ekxcos(mx)e^{kx}\cos(mx)ekx(Acosmx+Bsinmx)e^{kx}(A\cos mx + B\sin mx)

📝Non-Homogeneous Equation

Problem: Solve y3y+2y=e3xy'' - 3y' + 2y = e^{3x}.

💡Solution

  1. Homogeneous: r23r+2=0r=1,2r^2 - 3r + 2 = 0 \Rightarrow r=1,2
    • yh=C1ex+C2e2xy_h = C_1 e^x + C_2 e^{2x}
  2. Particular: Guess yp=Ae3xy_p = Ae^{3x}
  3. yp=3Ae3xy_p' = 3Ae^{3x}, yp=9Ae3xy_p'' = 9Ae^{3x}
  4. Substitute: 9A9A+2A=12A=1A=129A - 9A + 2A = 1 \Rightarrow 2A = 1 \Rightarrow A = \frac{1}{2}
  5. Answer: y=C1ex+C2e2x+12e3xy = C_1 e^x + C_2 e^{2x} + \frac{1}{2}e^{3x}

Existence and Uniqueness Theorem

ThPicard–Lindelöf Theorem (Existence & Uniqueness)

Consider the IVP dydt=f(t,y)\frac{dy}{dt} = f(t, y), y(t0)=y0y(t_0) = y_0. If:

  1. f(t,y)f(t, y) is continuous in some rectangle containing (t0,y0)(t_0, y_0)
  2. fy\frac{\partial f}{\partial y} is continuous in that rectangle

Then there exists a unique solution y(t)y(t) in some interval around t0t_0.

⚠️ Why This Matters

Without satisfying these conditions, an IVP may have no solution, infinitely many solutions, or non-unique solutions. This theorem guarantees well-posedness for most practical ODEs.

📝Non-Uniqueness Example

Problem: dydx=y\frac{dy}{dx} = \sqrt{y}, y(0)=0y(0) = 0.

f(x,y)=yf(x,y) = \sqrt{y} is continuous, but fy=12y\frac{\partial f}{\partial y} = \frac{1}{2\sqrt{y}} is not continuous at y=0y=0. The theorem doesn't apply, and indeed there are infinitely many solutions (e.g., y=0y=0 and y=x24y=\frac{x^2}{4}).


Numerical Methods

Euler's Method

DfEuler's Method

The simplest numerical method for solving ODEs. It takes small steps in the direction of the derivative.

Euler's Method

yn+1=yn+hf(tn,yn)y_{n+1} = y_n + h\,f(t_n, y_n)

Here,

  • yny_n=Current approximation
  • hh=Step size
  • f(tn,yn)f(t_n, y_n)=Derivative at current point

⚠️ Accuracy

Euler's method has local error O(h2)\mathcal{O}(h^2) and global error O(h)\mathcal{O}(h). It is first-order accurate — useful for learning but often too inaccurate for production.

import numpy as np

def euler(f, y0, t_span, h=0.01):
    t = np.arange(t_span[0], t_span[1], h)
    y = np.zeros(len(t))
    y[0] = y0
    for i in range(1, len(t)):
        y[i] = y[i-1] + h * f(t[i-1], y[i-1])
    return t, y

# Example: dy/dt = -y, y(0) = 1
f = lambda t, y: -y
t, y = euler(f, 1.0, [0, 5])

Runge-Kutta (RK4)

ThRunge-Kutta 4th Order

A higher-order method with much better accuracy. It evaluates the derivative at four intermediate points per step.

RK4 Stages

yn+1=yn+16(k1+2k2+2k3+k4)y_{n+1} = y_n + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)

Here,

  • k1k_1=h f(t_n, y_n)
  • k2k_2=h f(t_n + h/2, y_n + k_1/2)
  • k3k_3=h f(t_n + h/2, y_n + k_2/2)
  • k4k_4=h f(t_n + h, y_n + k_3)

ℹ️ Accuracy

RK4 has local error O(h5)\mathcal{O}(h^5) and global error O(h4)\mathcal{O}(h^4). It is the workhorse of numerical ODE solving.

def rk4(f, y0, t_span, h=0.01):
    t = np.arange(t_span[0], t_span[1], h)
    y = np.zeros(len(t))
    y[0] = y0
    for i in range(1, len(t)):
        k1 = h * f(t[i-1], y[i-1])
        k2 = h * f(t[i-1] + h/2, y[i-1] + k1/2)
        k3 = h * f(t[i-1] + h/2, y[i-1] + k2/2)
        k4 = h * f(t[i-1] + h, y[i-1] + k3)
        y[i] = y[i-1] + (k1 + 2*k2 + 2*k3 + k4) / 6
    return t, y

t, y = rk4(lambda t, y: -y, 1.0, [0, 5])

📋Numerical Methods Comparison

MethodOrderLocal ErrorGlobal ErrorCost per Step
Euler1O(h2)\mathcal{O}(h^2)O(h)\mathcal{O}(h)1 function eval
RK2 (Midpoint)2O(h3)\mathcal{O}(h^3)O(h2)\mathcal{O}(h^2)2 function evals
RK44O(h5)\mathcal{O}(h^5)O(h4)\mathcal{O}(h^4)4 function evals
RK45 (Dormand-Prince)4(5)AdaptiveAdaptive6 function evals

Python Implementation with scipy

ℹ️ scipy.integrate.solve_ivp

The scipy.integrate.solve_ivp function is the modern Python interface for solving initial value problems. It supports multiple methods and adaptive step-size control.

from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt

# Define the ODE: dy/dt = -2y + sin(t)
def ode(t, y):
    return -2*y + np.sin(t)

# Solve with default RK45
sol = solve_ivp(ode, [0, 10], [1.0], dense_output=True, max_step=0.1)

# Evaluate solution on a fine grid
t_eval = np.linspace(0, 10, 500)
y_eval = sol.sol(t_eval)[0]

# Compare with Euler for visualization
def euler(f, y0, t_span, h=0.01):
    t = np.arange(t_span[0], t_span[1], h)
    y = np.zeros(len(t))
    y[0] = y0
    for i in range(1, len(t)):
        y[i] = y[i-1] + h * f(t[i-1], y[i-1])
    return t, y

t_euler, y_euler = euler(ode, 1.0, [0, 10], h=0.05)

plt.plot(t_eval, y_eval, label='RK45 (scipy)', linewidth=2)
plt.plot(t_euler, y_euler, '--', label='Euler (h=0.05)', alpha=0.7)
plt.xlabel('t'); plt.ylabel('y')
plt.legend(); plt.title('Comparison of Numerical Methods')
plt.show()

📝Solving a System of ODEs

Problem: Solve the Lotka-Volterra (predator-prey) system:

dxdt=αxβxy,dydt=δxyγy\frac{dx}{dt} = \alpha x - \beta xy, \quad \frac{dy}{dt} = \delta xy - \gamma y

💡Solution

from scipy.integrate import solve_ivp

def lotka_volterra(t, z, alpha=1.0, beta=0.5, delta=0.25, gamma=0.7):
    x, y = z
    dxdt = alpha*x - beta*x*y
    dydt = delta*x*y - gamma*y
    return [dxdt, dydt]

sol = solve_ivp(lotka_volterra, [0, 30], [10, 5], dense_output=True)
t = np.linspace(0, 30, 1000)
z = sol.sol(t)

plt.plot(t, z[0], label='Prey (x)')
plt.plot(t, z[1], label='Predator (y)')
plt.legend(); plt.xlabel('t'); plt.ylabel('Population')
plt.title('Lotka-Volterra Predator-Prey')
plt.show()

Applications in AI/ML

Neural ODEs

DfNeural ODE

A Neural ODE replaces the ODE right-hand side with a neural network, enabling continuous-depth models where the hidden state evolves according to learned dynamics:

Neural ODE

dydt=fθ(t,y)\frac{dy}{dt} = f_\theta(t, y)

Here,

  • fθf_θ=Neural network parameterized by θ
  • yy=Hidden state
  • tt=Continuous depth/time

ℹ️ Advantages

  • Memory efficiency: Backpropagate through the ODE solver using the adjoint method (constant memory)
  • Adaptive computation: Solver controls the number of function evaluations
  • Continuous normalizing flows: Change of variables becomes an ODE
  • Irregular time series: Naturally handle irregularly sampled data
import torch
import torch.nn as nn
from torchdiffeq import odeint  # pip install torchdiffeq

class NeuralODEFunc(nn.Module):
    def __init__(self, input_dim, hidden_dim):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(input_dim, hidden_dim),
            nn.Tanh(),
            nn.Linear(hidden_dim, input_dim)
        )

    def forward(self, t, y):
        return self.net(y)

# Usage
func = NeuralODEFunc(input_dim=2, hidden_dim=16)
y0 = torch.tensor([1.0, 0.0])
t_span = torch.linspace(0, 5.0, 100)
y_traj = odeint(func, y0, t_span)  # Shape: (100, 2)

Diffusion Models

ℹ️ Score-Based Generative Models

Diffusion models (DDPMs, score-based models) use SDEs/ODEs to gradually add and remove noise. The reverse process is an ODE/SDE that transforms noise into data. Understanding ODE solvers is crucial for fast sampling.

Diffusion ODE

dx=[f(x,t)12g(t)2xlogpt(x)]dtdx = \left[f(x,t) - \frac{1}{2}g(t)^2 \nabla_x \log p_t(x)\right]dt

Here,

  • f(x,t)f(x,t)=Drift coefficient
  • g(t)g(t)=Diffusion coefficient
  • xlogpt(x)∇_x log p_t(x)=Score function (learned)

Physics-Informed Neural Networks (PINNs)

ℹ️ PINNs

PINNs embed ODE/PDE residuals into the loss function of a neural network, enforcing physical laws without labeled data. The network learns to satisfy the differential equation at collocation points.

# Simplified PINN for dy/dt = -y, y(0) = 1
class PINN(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(1, 32), nn.Tanh(),
            nn.Linear(32, 32), nn.Tanh(),
            nn.Linear(32, 1)
        )

    def forward(self, t):
        return self.net(t)

model = PINN()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

for epoch in range(5000):
    t = torch.rand(100, 1) * 5  # Collocation points
    t.requires_grad_(True)
    y = model(t)

    # ODE residual: dy/dt + y = 0
    dy_dt = torch.autograd.grad(y, t, grad_outputs=torch.ones_like(y),
                                 create_graph=True)[0]
    ode_loss = torch.mean((dy_dt + y)**2)

    # Initial condition: y(0) = 1
    ic_loss = (model(torch.zeros(1, 1)) - 1.0)**2

    loss = ode_loss + 10 * ic_loss
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

Common Mistakes

📋Common Mistakes Table

MistakeCorrect Approach
Forgetting the constant of integration CCAlways include +C+ C for indefinite integrals
Incorrectly separating variablesEnsure all yy terms are with dydy and all xx terms with dxdx
Sign errors in integrating factorCompute μ=ePdx\mu = e^{\int P\,dx} carefully; verify by differentiating
Confusing order and degreeOrder = highest derivative; degree = power of that derivative (after clearing radicals)
Forgetting initial conditionsAlways apply ICs to determine constants after finding general solution
Assuming existence/uniquenessCheck continuity of ff and f/y\partial f/\partial y before assuming unique solution
Using Euler with large step sizeReduce hh or switch to RK4 for better accuracy
Not checking for exactnessAlways verify M/y=N/x\partial M/\partial y = \partial N/\partial x before assuming exact
Mixing up homogeneous/non-homogeneousf(x)=0f(x)=0 is homogeneous; f(x)0f(x)\neq 0 is non-homogeneous
Neglecting complex rootsWhen Δ<0\Delta < 0, use eαx(C1cosβx+C2sinβx)e^{\alpha x}(C_1\cos\beta x + C_2\sin\beta x)

Interview Questions

📝Interview Question 1

Q: What is the difference between an ODE and a PDE?

A: An ODE involves derivatives with respect to a single independent variable (e.g., dydx=f(x,y)\frac{dy}{dx} = f(x,y)). A PDE involves partial derivatives with respect to two or more independent variables (e.g., ut=α2ux2\frac{\partial u}{\partial t} = \alpha\frac{\partial^2 u}{\partial x^2}). ODEs describe lumped systems; PDEs describe distributed systems.

📝Interview Question 2

Q: Explain the Picard–Lindelöf theorem. Why is it important?

A: It guarantees that for dydt=f(t,y)\frac{dy}{dt} = f(t,y), y(t0)=y0y(t_0)=y_0, if ff and fy\frac{\partial f}{\partial y} are continuous near (t0,y0)(t_0, y_0), then a unique solution exists in some interval. It's crucial because it tells us when numerical solvers will converge to a well-defined answer, and when the problem is well-posed.

📝Interview Question 3

Q: What are Neural ODEs and why use them over ResNets?

A: A ResNet computes yn+1=yn+f(yn)y_{n+1} = y_n + f(y_n), which is a discretization of dydt=f(y)\frac{dy}{dt} = f(y). A Neural ODE makes this continuous, allowing: (1) adaptive computation via ODE solvers, (2) memory-efficient training via the adjoint method, (3) continuous normalizing flows, and (4) handling of irregular time-series data.

📝Interview Question 4

Q: When would you use an integrating factor?

A: For first-order linear ODEs of the form dydx+P(x)y=Q(x)\frac{dy}{dx} + P(x)y = Q(x). The integrating factor μ=ePdx\mu = e^{\int P\,dx} transforms the left side into ddx[μy]\frac{d}{dx}[\mu y], making the equation directly integrable.

📝Interview Question 5

Q: Compare Euler's method and RK4 in terms of accuracy and cost.

A: Euler's method is first-order: global error O(h)\mathcal{O}(h), cost 1 function eval per step. RK4 is fourth-order: global error O(h4)\mathcal{O}(h^4), cost 4 function evals per step. For the same accuracy, RK4 can use much larger step sizes, making it far more efficient in practice.

📝Interview Question 6

Q: How do diffusion models relate to ODEs?

A: Diffusion models define a forward SDE that corrupts data into noise. The reverse process can be formulated as an ODE (the probability flow ODE) or SDE. Fast sampling methods (DDIM, flow matching) solve this ODE with fewer steps, directly leveraging numerical ODE solver techniques.


Practice Problems

📝Problem 1: Separable Equation

Solve dydx=2xy2\frac{dy}{dx} = \frac{2x}{y^2} with y(0)=3y(0) = 3.

💡Solution

  1. Separate: y2dy=2xdxy^2\,dy = 2x\,dx
  2. Integrate: y33=x2+C\frac{y^3}{3} = x^2 + C
  3. Apply y(0)=3y(0)=3: 273=0+CC=9\frac{27}{3} = 0 + C \Rightarrow C = 9
  4. Answer: y3=3x2+27y^3 = 3x^2 + 27, or y=(3x2+27)1/3y = (3x^2 + 27)^{1/3}

📝Problem 2: Integrating Factor

Solve dydxyx=x2\frac{dy}{dx} - \frac{y}{x} = x^2 with y(1)=2y(1) = 2.

💡Solution

  1. P(x)=1xP(x) = -\frac{1}{x}, Q(x)=x2Q(x) = x^2
  2. μ=e1/xdx=elnx=1x\mu = e^{\int -1/x\,dx} = e^{-\ln x} = \frac{1}{x}
  3. Multiply: 1xdydxyx2=x\frac{1}{x}\frac{dy}{dx} - \frac{y}{x^2} = x
  4. Left side = ddx[yx]\frac{d}{dx}\left[\frac{y}{x}\right]
  5. Integrate: yx=x22+C\frac{y}{x} = \frac{x^2}{2} + C
  6. y=x32+Cxy = \frac{x^3}{2} + Cx
  7. Apply y(1)=2y(1)=2: 2=12+CC=322 = \frac{1}{2} + C \Rightarrow C = \frac{3}{2}
  8. Answer: y=x32+3x2y = \frac{x^3}{2} + \frac{3x}{2}

📝Problem 3: Second-Order ODE

Solve y+2y+5y=0y'' + 2y' + 5y = 0 with y(0)=1y(0)=1, y(0)=1y'(0)=1.

💡Solution

  1. Characteristic: r2+2r+5=0r^2 + 2r + 5 = 0
  2. r=2±4202=1±2ir = \frac{-2 \pm \sqrt{4-20}}{2} = -1 \pm 2i
  3. General: y=ex(C1cos2x+C2sin2x)y = e^{-x}(C_1\cos 2x + C_2\sin 2x)
  4. y(0)=1y(0)=1: C1=1C_1 = 1
  5. y=ex(C1cos2xC2sin2x2C1sin2x+2C2cos2x)y' = e^{-x}(-C_1\cos 2x - C_2\sin 2x - 2C_1\sin 2x + 2C_2\cos 2x)
  6. y(0)=1y'(0)=1: C1+2C2=11+2C2=1C2=1-C_1 + 2C_2 = 1 \Rightarrow -1 + 2C_2 = 1 \Rightarrow C_2 = 1
  7. Answer: y=ex(cos2x+sin2x)y = e^{-x}(\cos 2x + \sin 2x)

📝Problem 4: Non-Homogeneous Equation

Solve yy=e2xy'' - y = e^{2x} with y(0)=0y(0)=0, y(0)=1y'(0)=1.

💡Solution

  1. Homogeneous: r21=0r=±1r^2 - 1 = 0 \Rightarrow r = \pm 1
    • yh=C1ex+C2exy_h = C_1 e^x + C_2 e^{-x}
  2. Particular: Guess yp=Ae2xy_p = Ae^{2x}
  3. 4Ae2xAe2x=e2x3A=1A=134Ae^{2x} - Ae^{2x} = e^{2x} \Rightarrow 3A = 1 \Rightarrow A = \frac{1}{3}
  4. General: y=C1ex+C2ex+13e2xy = C_1 e^x + C_2 e^{-x} + \frac{1}{3}e^{2x}
  5. y(0)=0y(0)=0: C1+C2+13=0C_1 + C_2 + \frac{1}{3} = 0
  6. y(0)=1y'(0)=1: C1C2+23=1C1C2=13C_1 - C_2 + \frac{2}{3} = 1 \Rightarrow C_1 - C_2 = \frac{1}{3}
  7. Solve: C1=16C_1 = -\frac{1}{6}, C2=12C_2 = -\frac{1}{2}
  8. Answer: y=16ex12ex+13e2xy = -\frac{1}{6}e^x - \frac{1}{2}e^{-x} + \frac{1}{3}e^{2x}

Quick Reference

📋Differential Equations Quick Reference

TopicKey Formula / Concept
General First-Order ODEdydx=f(x,y)\frac{dy}{dx} = f(x, y)
Separabledyh(y)=g(x)dx+C\int \frac{dy}{h(y)} = \int g(x)\,dx + C
Integrating Factorμ=ePdx\mu = e^{\int P\,dx}, then y=1μμQdxy = \frac{1}{\mu}\int \mu Q\,dx
ExactMy=Nx\frac{\partial M}{\partial y} = \frac{\partial N}{\partial x}, solution Ψ(x,y)=C\Psi(x,y)=C
Characteristic Eq.ar2+br+c=0ar^2 + br + c = 0 for ay+by+cy=0ay''+by'+cy=0
Non-Homogeneousy=yh+ypy = y_h + y_p (homogeneous + particular)
Euler's Methodyn+1=yn+hf(tn,yn)y_{n+1} = y_n + hf(t_n, y_n)
RK4yn+1=yn+16(k1+2k2+2k3+k4)y_{n+1} = y_n + \frac{1}{6}(k_1+2k_2+2k_3+k_4)
Existence/Uniquenessff and f/y\partial f/\partial y continuous ⟹ unique solution
Neural ODEdydt=fθ(t,y)\frac{dy}{dt} = f_\theta(t, y), solve with adjoint method
Diffusion ODEReverse SDE/ODE for generative modeling
scipysolve_ivp(f, t_span, y0, method='RK45')

Cross-References

Lesson Progress33 / 100