Skip to content

Commit

Permalink
[Term Entry] Python scipy integrate odeint (#5910)
Browse files Browse the repository at this point in the history
* 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 <mamta.wardhani@gmail.com>

* 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 <mamta.wardhani@gmail.com>

* 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 <mamta.wardhani@gmail.com>

* 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 <mamta.wardhani@gmail.com>

* 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 <mamta.wardhani@gmail.com>

* 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 <mamta.wardhani@gmail.com>

* 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 <mamta.wardhani@gmail.com>

* 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

---------
  • Loading branch information
virachai authored Jan 13, 2025
1 parent ef344c1 commit 30f61d7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions content/scipy/concepts/scipy-integrate/terms/odeint/odeint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
Title: '.odeint()'
Description: 'Solves ordinary differential equations in SciPy using the LSODA method, automatically handling stiff and non-stiff problems.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Data'
- 'Math'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

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)_.

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.

## Syntax

The general syntax for using `odeint()` is as follows:

```pseudo
from scipy.integrate import odeint
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)
```

- `func`: A callable that defines the derivative of the system, ({dy}/{dt} = f(y, t, ...)).
- `y0`: Initial condition(s) (array-like). Represents the initial state of the system.
- `t`: Array of time points at which the solution is to be computed (1D array-like).
- `args` (Optional): Tuple of extra arguments to pass to `func`.

Other parameters are optional and control solver behavior, precision, and output verbosity.

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`.

## Example

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:

```py
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def model(y, t):
dydt = -2 * y + 3
return dydt

# Initial condition
y0 = 5

# Time points
t = np.linspace(0, 5, 100)

# Solve ODE
solution = odeint(model, y0, t)

# Plot results (indexing the solution to get the actual y values)
plt.plot(t, solution[:, 0]) Since solution is a 2D array, we index its first column to extract the solution values.
plt.title('Solution of dy/dt = -2y + 3')
plt.xlabel('Time (t)')
plt.ylabel('y(t)')
plt.grid()
plt.show()
```

The output plot shows the numerical solution of the ODE, illustrating how `y(t)` evolves over time:

![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)

`odeint()` is ideal for many scientific and engineering applications due to its robustness and flexibility.

> For more advanced control or alternative solvers, consider using `scipy.integrate.solve_ivp()`, which offers a more modern API.
Binary file added media/odeint_solution_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 30f61d7

Please sign in to comment.