---
author: Nathan Carter (ncarter@bentley.edu)
---

This answer assumes you have imported SymPy as follows.


```python
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output
```

Let's re-use here the code from how to write an ordinary differential equation,
to write $\frac{dy}{dx}=y$.



```python
var( 'x' )
y = Function('y')(x)
dydx = Derivative( y, x )
ode = dydx - y
ode
```




$\displaystyle - y{\left(x \right)} + \frac{d}{d x} y{\left(x \right)}$



You can solve an ODE by using the `dsolve` command.


```python
solution = dsolve( ode )
solution
```




$\displaystyle y{\left(x \right)} = C_{1} e^{x}$



If there are initial conditions that need to be substituted in for $x$ and $y$,
it is crucial to substitute for $y$ first and then $x$.  Let's assume we have the
initial condition $(3,5)$.  We might proceed as follows.


```python
with_inits = solution.subs( y, 5 ).subs( x, 3 )
with_inits
```




$\displaystyle 5 = C_{1} e^{3}$




```python
solve( with_inits )
```




$\displaystyle \left[ \frac{5}{e^{3}}\right]$



To substitute $C_1=\frac{5}{e^3}$ into the solution, note that $C_1$ is written as `var('C1')`.


```python
solution.subs( var('C1'), 5/E**3 )
```




$\displaystyle y{\left(x \right)} = \frac{5 e^{x}}{e^{3}}$



02bfcd6a9d0d6a72068afd06cccc660586859658
ipynb
Jupyter Notebook
database/tasks/How to solve an ordinary differential equation/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to solve an ordinary differential equation/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to solve an ordinary differential equation/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
2021-07-18T19:01:29.000Z
2022-03-29T06:47:11.000Z
Qwen/Qwen-72B
1. YES
2. YES