Overview
This lecture introduces solving differential equations in Python using SciPy, covering first-order, coupled, and higher-order ODEs, and emphasizes proper equation structuring and solver selection.
First-Order Differential Equations in Python
- Most ODE solving in Python is done with SciPy, using functions
odeint and solve_ivp.
- Equations must be written in the form: derivative = function of time and the variable, e.g., dv/dt = f(t, v).
- Define the ODE as a Python function taking time and variable as arguments.
- Initial conditions are specified (e.g., v(0) = 0).
- Both
odeint and solve_ivp require the function, initial condition, and time span/array.
odeint expects the function (with t first if tfirst=True), initial value, and times as arguments.
solve_ivp is more customizable, requiring the function, a time span, initial value (as an array), and evaluation points.
Working with Solution Objects
- Both solvers return objects containing arrays of solutions, structuring data for multi-variable systems.
- Extract single-variable solutions by indexing or transposing the result arrays.
Coupled First-Order Differential Equations
- Systems are written with each variable having its own ODE, often with mutual dependencies.
- Package all variables into a vector (e.g., S = [y1, y2]).
- Define a function returning a vector of derivatives, given the current vector and independent variable.
- The pattern is easily extended to larger systems by expanding the size of the vector.
Higher-Order Differential Equations
- Any higher-order ODE must be rewritten as a system of first-order ODEs.
- For a second-order ODE, introduce new variables to represent each derivative up to one fewer than the highest order.
- Example: x'' = f(x', x, t) becomes two first-order ODEs using v = x'.
- For nth-order (or coupled systems), define new variables recursively for each derivative.
Setting Up and Solving Large Systems
- Package all variables into a state vector S (e.g., for 3rd order: [x1, v1, a1, x2, v2, a2]).
- Define a function returning derivatives for each component, extracting and updating based on definitions.
- Specify all initial conditions in a vector and solve using the same routines.
Precautions for Complex or Chaotic ODEs
- Not all solvers work for every system; some ODEs, especially chaotic ones, require specific solvers and parameter tuning.
- For sensitive systems, use methods like 'DOP853' in
solve_ivp and set tight tolerances (atol, rtol).
- Review the relevant literature for recommended solvers and settings.
Key Terms & Definitions
- ODE (Ordinary Differential Equation) — An equation involving derivatives of a function with respect to one variable.
- Initial Condition — The value of the function (and possibly its derivatives) at the starting point.
- odeint — SciPy function for solving ODEs, uses ODEPACK's LSODA solver.
- solve_ivp — SciPy function for solving initial value problems for ODEs, supports multiple solvers.
- Coupled ODEs — A set of differential equations where each equation depends on multiple unknown functions.
- Tolerance (
atol, rtol) — Parameters controlling numerical precision during ODE solving.
Action Items / Next Steps
- Practice converting higher-order ODEs to systems of first-order ODEs.
- Experiment with both
odeint and solve_ivp for different ODE systems.
- Research solver requirements and parameter tuning for any complex or chaotic system you need to solve.