You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/numerical-integration/index.qmd
+22-36Lines changed: 22 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -28,52 +28,38 @@ Where:
28
28
29
29
# SciPy's `solve_ivp`
30
30
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:
32
32
33
33
```python
34
+
import numpy as np
34
35
from scipy.integrate import solve_ivp
35
36
36
-
defdydt(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
+
defexponential_decay(t, y):
38
+
return-0.5* y
50
39
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)
52
46
53
-
$$\frac{dy}{dt} = ky, \quad y(0) = y_0$$
47
+
print(sol.t)
54
48
55
-
```python
56
-
import numpy as np
57
-
import matplotlib.pyplot as plt
58
-
from scipy.integrate import solve_ivp
59
-
60
-
defexponential_growth(t, y, k):
61
-
return k * y
49
+
print(sol.y)
50
+
```
62
51
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)):
67
53
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).
70
58
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)`.
0 commit comments