Skip to content

Commit 30f61d7

Browse files
authored
[Term Entry] Python scipy integrate odeint (#5910)
* Explanation for using scipy.integrate.odeint() to solve ODEs * Add documentation for odeint() function in SciPy for solving ODEs * Enhance .odeint() function description Refined the description of the .odeint() function to emphasize its role in solving ordinary differential equations using the LSODA method. The updated description highlights its capability to automatically handle both stiff and non-stiff problems. A big thank you to @mamtawardhani for the valuable suggestions! Co-authored-by: Mamta Wardhani <[email protected]> * Add hyperlink to .odeint() function description Included a hyperlink in the description of the .odeint() function to provide direct access to the SciPy integrate module documentation. This update improves the usability of the documentation. A big thank you to @mamtawardhani for the valuable suggestion! Co-authored-by: Mamta Wardhani <[email protected]> * Update code block language to pseudo for .odeint() Changed the code block language from python to pseudo in the .odeint() function documentation to accurately reflect the content. Thank you so much to @mamtawardhani for the helpful suggestion! Co-authored-by: Mamta Wardhani <[email protected]> * Clarify parameter descriptions for .odeint() documentation Improved the parameter descriptions for the .odeint() function documentation by adding detailed explanations and clarifications. This update enhances the overall clarity and usability of the documentation. Thank you so much to @mamtawardhani for the insightful suggestions! Co-authored-by: Mamta Wardhani <[email protected]> * Enhance return description for .odeint() documentation Updated the return description for the .odeint() function documentation to specify that it returns a 2D NumPy array, with each row corresponding to the solution at a specific time point in t. Thanks to @mamtawardhani for the helpful suggestion! Co-authored-by: Mamta Wardhani <[email protected]> * Update plotting code for .odeint() documentation Modified the plotting code in the .odeint() function documentation to correctly index the solution array and retrieve the actual y values. A big thank you to @mamtawardhani for the valuable suggestion! Co-authored-by: Mamta Wardhani <[email protected]> * Update code block language to py for .odeint() documentation Changed the code block language from python to py in the .odeint() function documentation for improved readability and consistency. Many thanks to @mamtawardhani for the suggestion! Co-authored-by: Mamta Wardhani <[email protected]> * Add explanation for odeint usage and output image Added a brief explanation of the code and the usage of odeint, emphasizing its role in solving differential equations numerically. Included an output visualization as an image stored under the media folder in the docs directory for better understanding. * Attach the image of the output * explanation of the code and how is odeint used in it moved explanation * Update odeint.md minor fixes * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md * Update odeint.md * Update odeint.md * fix lint errors ---------
1 parent ef344c1 commit 30f61d7

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
Title: '.odeint()'
3+
Description: 'Solves ordinary differential equations in SciPy using the LSODA method, automatically handling stiff and non-stiff problems.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Data'
9+
- 'Math'
10+
- 'Python'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`odeint()`** function from SciPy's [`integrate`](https://www.codecademy.com/resources/docs/scipy/scipy-integrate) module is a powerful tool for solving initial value problems for _Ordinary Differential Equations (ODEs)_.
17+
18+
It integrates a system of ordinary differential equations using the LSODA method from the FORTRAN library `odepack`, which automatically switches between stiff and non-stiff methods based on the problem's characteristics.
19+
20+
## Syntax
21+
22+
The general syntax for using `odeint()` is as follows:
23+
24+
```pseudo
25+
from scipy.integrate import odeint
26+
27+
solution = odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=500, mxhnil=10, mxordn=12, mxords=5)
28+
```
29+
30+
- `func`: A callable that defines the derivative of the system, ({dy}/{dt} = f(y, t, ...)).
31+
- `y0`: Initial condition(s) (array-like). Represents the initial state of the system.
32+
- `t`: Array of time points at which the solution is to be computed (1D array-like).
33+
- `args` (Optional): Tuple of extra arguments to pass to `func`.
34+
35+
Other parameters are optional and control solver behavior, precision, and output verbosity.
36+
37+
It returns a 2D [NumPy](https://www.codecademy.com/resources/docs/numpy) array, where each row corresponds to the solution at a specific time point in `t`.
38+
39+
## Example
40+
41+
The code below numerically solves a first-order ordinary differential equation using `odeint`. The model function defines the ODE, and `odeint` integrates it over specified time points starting from the initial condition, and the results are plotted to visualize the solution:
42+
43+
```py
44+
import numpy as np
45+
from scipy.integrate import odeint
46+
import matplotlib.pyplot as plt
47+
48+
def model(y, t):
49+
dydt = -2 * y + 3
50+
return dydt
51+
52+
# Initial condition
53+
y0 = 5
54+
55+
# Time points
56+
t = np.linspace(0, 5, 100)
57+
58+
# Solve ODE
59+
solution = odeint(model, y0, t)
60+
61+
# Plot results (indexing the solution to get the actual y values)
62+
plt.plot(t, solution[:, 0]) Since solution is a 2D array, we index its first column to extract the solution values.
63+
plt.title('Solution of dy/dt = -2y + 3')
64+
plt.xlabel('Time (t)')
65+
plt.ylabel('y(t)')
66+
plt.grid()
67+
plt.show()
68+
```
69+
70+
The output plot shows the numerical solution of the ODE, illustrating how `y(t)` evolves over time:
71+
72+
![A plot showing the solution of an ODE using odeint, with time on the x-axis and y(t) on the y-axis.](https://raw.githubusercontent.com/Codecademy/docs/main/media/odeint_solution_plot.png)
73+
74+
`odeint()` is ideal for many scientific and engineering applications due to its robustness and flexibility.
75+
76+
> For more advanced control or alternative solvers, consider using `scipy.integrate.solve_ivp()`, which offers a more modern API.

media/odeint_solution_plot.png

21.4 KB
Loading

0 commit comments

Comments
 (0)