Solving Ordinary Differential Equations with SciPy
Introduction to ODEs and SciPy
Welcome to the next lesson in our SciPy course, focusing on solving Ordinary Differential Equations (ODEs). Building on your previous experiences with integration, we will now explore ODEs, which are equations involving a function and its derivatives. ODEs are widely used in fields like physics, engineering, and biology to model dynamic systems, such as the motion of a pendulum or population growth.
Our tool of choice is SciPy, a powerful library in Python that makes solving these equations manageable. By the end of this lesson, you'll be able to solve a simple first-order ODE and visualize the solution using Python's scientific libraries.
Defining the ODE Problem
Let's start by defining the ODE we aim to solve:
- ODE: An equation involving a function
xand its derivative with respect to timet. - Initial Conditions: Essential for solving ODEs, initial conditions specify the starting value of the function. For our problem, let's use .
- Time Span: The interval over which we want to solve the ODE. For this lesson, we'll consider from 0 to 10.
Understanding these components is crucial for setting up and solving ODEs effectively.
Step 1: Defining ODE
To solve our ODE, we will use SciPy's solve_ivp function. Let's break this process down step-by-step.
First, we define the ODE as a Python function. This function computes the derivative at any given time, .
- Function
modeltakes two arguments: timetand the function valuex. - It returns the derivative , which is .
Step 2: Set Initial Conditions and Time Span
We also need to specify the starting value and the time span for the solution.
x0: A list containing the initial condition.t_span: A tuple defining the start and end times.
Step 3: Specify Evaluation Points
Finally, we'll define the time points where we want to compute the solution.
t_eval is an array of time points using NumPy's linspace to evaluate the solution at specific intervals.
Plotting the ODE Solution with Matplotlib
With the ODE defined and initial conditions specified, we're ready to solve and visualize the solution.
Let's use solve_ivp to compute the solution.
solve_ivp: This function takes our model, time span, initial condition, and evaluation points to compute the solution.solution: An object containing the results, including timesolution.tand solution valuessolution.y.
We'll use Matplotlib to plot the solution over time.
plt.plot: Plots time against the solution.plt.xlabelandplt.ylabel: Set the labels for the x and y axes.plt.title: Provides a title for the plot.plt.legend: Adds a legend for clarity.
Here is the solution:

As you can see, the answer is a quadratic function, as expected.
Defining a More Complex First-Order ODE
Let's consider a slightly more complex first-order ODE that models exponential decay with an external forcing function:
Here's how to implement it in Python using solve_ivp:
exponential_decay_with_forcing: The function defining our ODE. It includes a decay term-alpha * xand an external forcing functionsin(t).alpha: Represents the decay rate.args=(alpha,): Passes the decay rate as an additional argument to the model function.
Here is the result for this code snippet:

Summary and Next Steps
In this lesson, we explored solving an ODE using SciPy's solve_ivp function and visualizing the results with Matplotlib. Starting with defining our ODE, setting initial conditions, solving the equation, and finally plotting the solution, you now have a framework to apply to similar problems.
Practice exercises follow this lesson, which will reinforce your understanding and give you the opportunity to apply these concepts to new situations. Keep exploring, and remember that you've already built a solid foundation for working with scientific computations in Python.
