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 , a differential equation involves , , , etc.
General Form
Here,
- =Unknown function (dependent variable)
- =Independent variable
- =Derivatives of y with respect to x
- =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
Here,
- =Function of x only
- =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)
Here,
- =Temperature at position x and time t
- =Thermal diffusivity constant
- =Partial derivative with respect to time
- =Second spatial partial derivative
📋ODE vs PDE Comparison
| Feature | ODE | PDE |
|---|---|---|
| Independent variables | 1 | 2+ |
| Derivatives | Ordinary (∂) | Partial (∂) |
| Typical use | Population growth, circuits | Heat flow, wave propagation |
| Solution | Function of 1 variable | Function of multiple variables |
| Common examples |
Order and Degree
DfOrder of a Differential Equation
The order is the highest derivative appearing in the equation. For example, 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, has degree 2.
📝Identifying Order and Degree
Equation:
- Order: 3 (highest derivative is )
- Degree: 3 (power of is 3)
Equation:
- First, square both sides to remove radical:
- Order: 2, Degree: 2
Separable Equations
DfSeparable Equation
A first-order ODE is separable if it can be written as:
The variables can be separated to opposite sides of the equation.
Separation of Variables
Here,
- =Function of x only
- =Function of y only
- =Constant of integration
📝Solve a Separable Equation
Problem: Solve with .
💡Solution
- Separate:
- Integrate both sides:
- Apply :
- Answer: , or
📝Another Separable Equation
Problem: Solve with .
💡Solution
- Separate:
- Integrate:
- Exponentiate: where
- Apply :
- Answer:
First-Order Linear Equations
DfFirst-Order Linear ODE
A first-order linear ODE has the standard form:
Integrating Factor Method
Here,
- =Integrating factor
- =Coefficient of y in standard form
- =Forcing term (right-hand side)
ℹ️ Method
Multiply both sides by , then the left side becomes . Integrate both sides to obtain the general solution.
General Solution
Here,
- =Integrating factor
- =Forcing term
- =Constant of integration
📝Solve Using Integrating Factor
Problem: Solve with .
💡Solution
- Identify: ,
- Integrating factor:
- Multiply both sides:
- Left side =
- Integrate:
- Integration by parts:
- Apply :
- Answer:
Exact Equations
DfExact Equation
A first-order ODE of the form is exact if:
This means there exists a potential function such that and .
Solution of Exact Equation
Here,
- =Potential function
- =∂Ψ/∂x
- =∂Ψ/∂y
📝Solve an Exact Equation
Problem: Solve .
💡Solution
- ,
- Check: , ✓ Exact!
- Find : Integrate w.r.t. :
- Differentiate w.r.t. :
- So
- Answer:
Second-Order Linear Equations
DfSecond-Order Linear ODE
A second-order linear ODE has the form:
where , , are constants.
Characteristic Equation
Here,
- =Coefficients from the ODE
- =Root of the characteristic equation
ℹ️ Solution Forms
| Discriminant | Roots | Solution Form |
|---|---|---|
| Two real roots | ||
| Repeated root | ||
| Complex |
📝Solve a Second-Order ODE
Problem: Solve with , .
💡Solution
- Characteristic equation:
- Factor:
- General solution:
- Apply :
- Apply :
- Solve: ,
- Answer:
📝Complex Roots
Problem: Solve with , .
💡Solution
- Characteristic equation:
- General solution:
- Apply :
- Apply :
- Answer:
Homogeneous vs Non-Homogeneous Equations
DfHomogeneous Equation
A linear ODE is homogeneous if the right-hand side (forcing term) is zero:
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:
The general solution is , where is the homogeneous solution and is a particular solution.
Method of Undetermined Coefficients
Here,
- =Homogeneous (complementary) solution
- =Particular solution
- =Non-homogeneous forcing term
ℹ️ Undetermined Coefficients Table
| Form of | Guess for |
|---|---|
| or | |
| Polynomial degree | |
📝Non-Homogeneous Equation
Problem: Solve .
💡Solution
- Homogeneous:
- Particular: Guess
- ,
- Substitute:
- Answer:
Existence and Uniqueness Theorem
ThPicard–Lindelöf Theorem (Existence & Uniqueness)
Consider the IVP , . If:
- is continuous in some rectangle containing
- is continuous in that rectangle
Then there exists a unique solution in some interval around .
⚠️ 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: , .
is continuous, but is not continuous at . The theorem doesn't apply, and indeed there are infinitely many solutions (e.g., and ).
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
Here,
- =Current approximation
- =Step size
- =Derivative at current point
⚠️ Accuracy
Euler's method has local error and global error . 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
Here,
- =h f(t_n, y_n)
- =h f(t_n + h/2, y_n + k_1/2)
- =h f(t_n + h/2, y_n + k_2/2)
- =h f(t_n + h, y_n + k_3)
ℹ️ Accuracy
RK4 has local error and global error . 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
| Method | Order | Local Error | Global Error | Cost per Step |
|---|---|---|---|---|
| Euler | 1 | 1 function eval | ||
| RK2 (Midpoint) | 2 | 2 function evals | ||
| RK4 | 4 | 4 function evals | ||
| RK45 (Dormand-Prince) | 4(5) | Adaptive | Adaptive | 6 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:
💡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
Here,
- =Neural network parameterized by θ
- =Hidden state
- =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
Here,
- =Drift coefficient
- =Diffusion coefficient
- =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
| Mistake | Correct Approach |
|---|---|
| Forgetting the constant of integration | Always include for indefinite integrals |
| Incorrectly separating variables | Ensure all terms are with and all terms with |
| Sign errors in integrating factor | Compute carefully; verify by differentiating |
| Confusing order and degree | Order = highest derivative; degree = power of that derivative (after clearing radicals) |
| Forgetting initial conditions | Always apply ICs to determine constants after finding general solution |
| Assuming existence/uniqueness | Check continuity of and before assuming unique solution |
| Using Euler with large step size | Reduce or switch to RK4 for better accuracy |
| Not checking for exactness | Always verify before assuming exact |
| Mixing up homogeneous/non-homogeneous | is homogeneous; is non-homogeneous |
| Neglecting complex roots | When , use |
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., ). A PDE involves partial derivatives with respect to two or more independent variables (e.g., ). 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 , , if and are continuous near , 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 , which is a discretization of . 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 . The integrating factor transforms the left side into , 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 , cost 1 function eval per step. RK4 is fourth-order: global error , 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 with .
💡Solution
- Separate:
- Integrate:
- Apply :
- Answer: , or
📝Problem 2: Integrating Factor
Solve with .
💡Solution
- ,
- Multiply:
- Left side =
- Integrate:
- Apply :
- Answer:
📝Problem 3: Second-Order ODE
Solve with , .
💡Solution
- Characteristic:
- General:
- :
- :
- Answer:
📝Problem 4: Non-Homogeneous Equation
Solve with , .
💡Solution
- Homogeneous:
- Particular: Guess
- General:
- :
- :
- Solve: ,
- Answer:
Quick Reference
📋Differential Equations Quick Reference
| Topic | Key Formula / Concept |
|---|---|
| General First-Order ODE | |
| Separable | |
| Integrating Factor | , then |
| Exact | , solution |
| Characteristic Eq. | for |
| Non-Homogeneous | (homogeneous + particular) |
| Euler's Method | |
| RK4 | |
| Existence/Uniqueness | and continuous ⟹ unique solution |
| Neural ODE | , solve with adjoint method |
| Diffusion ODE | Reverse SDE/ODE for generative modeling |
| scipy | solve_ivp(f, t_span, y0, method='RK45') |
Cross-References
- Previous: 032 - Calculus: Series & Sequences — Convergence tests, Taylor series, power series
- Next: 034 - Linear Algebra: Vectors & Spaces — Vector operations, basis, dimension
- Related:
- 020 - Probability: Continuous Distributions — PDFs, CDFs, expectation
- 025 - Statistics: Regression — Curve fitting, least squares
- 040 - Deep Learning: Optimization — Gradient descent, Adam optimizer
- 042 - Deep Learning: Generative Models — GANs, VAEs, diffusion models