#Table of Contents


# Lecture 1



This lecture introduces ordinary differential equations, and some techniques for solving first order equations. This notebook uses the computer algebra via Sympy (<http://sympy.org/>) to solve some ODE examples from the lecture notes.

## Solving ordinary differential equations

To use Sympy, we first need to import it and call `init_printing()` to get nicely typeset equations:


```
from sympy import *

# This initialises pretty printing
init_printing()
from IPython.display import display

# This command makes plots appear inside the browser window
%matplotlib inline
```

### Example: car breaking

During braking a car’s velocity is given by $v = v_{0} e^{−t/\tau}$. Calculate the distance travelled.

We first define the symbols in the equation ($t$, $\tau$ abd $v_{0}$), and the function ($x$, for the displacement):


```
t, tau, v0 = symbols("t tau v0")
x = Function("x")
```

Next, we define the differential equation, and print it to the screen:


```
eqn = Eq(Derivative(x(t), t) , v0*exp(-t/(tau)))
display(eqn)
```

The `dsolve` function solves the differential equation symbolically:


```
x = dsolve(eqn, x(t))
display(x)
```

where $C_{1}$ is a constant. As expected for a first-order equation, there is one constant.

SymPy is not yet very good at eliminating constants from initial conditions, so we will do this manually assuming that $x = 0$ and $t = 0$:


```
v0 = symbols('v0')
x = x.subs('C1', v0*tau)
display(x)
```

Specifying values for $v_{0}$ and $\tau$, when can plot the velocity as a function of time:


```
# Specify values for v0 and tau
x = x.subs(v0, 100)
x = x.subs(tau, 2)

# Plot velocity vs time
plot(x.args[1], (t, 0.0, 10.0), xlabel="time", ylabel="velocity")
```

#### Classification

We can ask SymPy to classify our ODE, e.g. show that it is first order):


```
classify_ode(eqn)
```




    ('separable',
     '1st_exact',
     '1st_linear',
     'Bernoulli',
     '1st_power_series',
     'lie_group',
     'nth_linear_constant_coeff_undetermined_coefficients',
     'nth_linear_constant_coeff_variation_of_parameters',
     'separable_Integral',
     '1st_exact_Integral',
     '1st_linear_Integral',
     'Bernoulli_Integral',
     'nth_linear_constant_coeff_variation_of_parameters_Integral')



### Parachutist

Find the variation of speed with time of a parachutist subject to a drag force of $kv^{2}$.

The equations to solve is

$$
\frac{m}{k} \frac{dv}{dt} = \alpha^{2} - v^{2}
$$

where $m$ is mass, $k$ is a prescribed constant, $v$ is the velocity, $t$ is time and $\alpha^{2} = mg/k$ ($g$ is acceleration due to gravity).

We specify the symbols, unknown function $v$ and the differential equation


```
t, m, k, alpha = symbols("t m k alpha")
v = Function("v")
eqn = Eq((m/k)*Derivative(v(t), t), alpha*alpha - v(t)*v(t))
display(eqn)
```

First, let's classify the ODE:


```
classify_ode(eqn)
```




    ('separable', '1st_power_series', 'lie_group', 'separable_Integral')



We see that it is not linear, but it is separable. Using `dsolve` again,


```
v = dsolve(eqn, v(t))
display(v)
```

SymPy can verify that an expression is a solution to an ODE:


```
print("Is v a solution to the ODE: {}".format(checkodesol(eqn, v)))
```

    Is v a solution to the ODE: (True, 0)


49a0b13e22612745415b87d21ccccbff7dc55b70
ipynb
Jupyter Notebook
notebooks/Lecture1.ipynb
quang-ha/IA-maths-Ipython
8ff8533d64a3d8db8e4813a7b6dfee39339fd846
BSD-3-Clause
notebooks/Lecture1.ipynb
quang-ha/IA-maths-Ipython
8ff8533d64a3d8db8e4813a7b6dfee39339fd846
BSD-3-Clause
notebooks/Lecture1.ipynb
quang-ha/IA-maths-Ipython
8ff8533d64a3d8db8e4813a7b6dfee39339fd846
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES