Skip to content

Commit 0ec8244

Browse files
author
Daniel Precioso
committed
Refactor solve_ivp example in numerical integration module for clarity and completeness
1 parent c93b65f commit 0ec8244

1 file changed

Lines changed: 22 additions & 36 deletions

File tree

modules/numerical-integration/index.qmd

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,38 @@ Where:
2828

2929
# SciPy's `solve_ivp`
3030

31-
The `scipy.integrate.solve_ivp` function is the standard tool for solving ODEs in Python:
31+
The `scipy.integrate.solve_ivp` function is the standard tool for solving ODEs in Python. Try the following code:
3232

3333
```python
34+
import numpy as np
3435
from scipy.integrate import solve_ivp
3536

36-
def dydt(t, y):
37-
# Define your ODE system
38-
return # derivative
39-
40-
sol = solve_ivp(dydt, t_span=(0, 10), y0=[initial_condition])
41-
```
42-
43-
## Key Parameters
44-
45-
- `fun`: The function defining the ODE system
46-
- `t_span`: Tuple `(t_start, t_end)` for integration interval
47-
- `y0`: Initial conditions (array)
48-
- `method`: Integration method (default: 'RK45')
49-
- `t_eval`: Specific time points to return solution
37+
def exponential_decay(t, y):
38+
return -0.5 * y
5039

51-
# Example: Exponential Growth
40+
t_span = [0, 10]
41+
y0 = [2, 4, 8]
42+
sol = solve_ivp(
43+
fun=exponential_decay,
44+
t_span=t_span,
45+
y0=y0)
5246

53-
$$\frac{dy}{dt} = ky, \quad y(0) = y_0$$
47+
print(sol.t)
5448

55-
```python
56-
import numpy as np
57-
import matplotlib.pyplot as plt
58-
from scipy.integrate import solve_ivp
59-
60-
def exponential_growth(t, y, k):
61-
return k * y
49+
print(sol.y)
50+
```
6251

63-
k = 0.5
64-
y0 = [1.0]
65-
t_span = (0, 10)
66-
t_eval = np.linspace(0, 10, 100)
52+
**How does `solve_ivp` work?** Let's understand its parameters (copied from the [documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html)):
6753

68-
sol = solve_ivp(exponential_growth, t_span, y0,
69-
args=(k,), t_eval=t_eval)
54+
- `fun`: Right-hand side of the system: the time derivative of the state $y$ at time $t$. The calling signature is `fun(t, y)`, where `t` is a scalar and `y` is an ndarray with `len(y) = len(y0)`. Additional arguments need to be passed if `args` is used (see documentation of `args` argument). `fun` must return an array of the same shape as `y`.
55+
In our example, `exponential_decay` defines the ODE $\frac{dy}{dt} = -0.5y$, which models exponential decay $y(t) = y_0 e^{-0.5t}$.
56+
- `t_span`: Interval of integration $(t_0, t_f)$. The solver starts with $t=t_0$ and integrates until it reaches $t=t_f$. Both $t_0$ and $t_f$ must be floats or values interpretable by the float conversion function.
57+
- `y0`: Initial state. For problems in the complex domain, pass `y0` with a complex data type (even if the initial value is purely real).
7058

71-
plt.plot(sol.t, sol.y[0])
72-
plt.xlabel('Time')
73-
plt.ylabel('y(t)')
74-
plt.title('Exponential Growth')
75-
plt.show()
76-
```
59+
You can also specify additional parameters:
60+
- `method`: Integration method to use. Common choices include `'RK45'` (default), `'RK23'`, `'DOP853'`, `'Radau'`, `'BDF'`, and `'LSODA'`.
61+
- `t_eval`: Times at which to store the computed solution, must be sorted and lie within `t_span`. If None (default), use points selected by the solver.
62+
- `args`: Additional arguments to pass to the user-defined functions. If, for example, `fun` has the signature `fun(t, y, a, b, c)`, then `args=(a, b, c)`.
7763

7864
# Systems of ODEs
7965

0 commit comments

Comments
 (0)