# Bouncing ball with the Euler integrator
As an introduction to solving ODEs we are solving the problem of the bouncing ball with the simplest of all integrators: the forward Euler scheme.

We have to solve the *second order ODE* (Newton's equations of motion with constant acceleration)
$$
\frac{d^2 y}{dt^2} = -g.
$$

The Euler scheme for any *first order ODE* 

$$
\frac{dy}{dt} = f(y, t)
$$

is
$$
y(t + h) = y(t) + h f(y(t), t).
$$

In order to solve the original 2nd order equation of motion we make use of the fact that one $n$-th order ODE can be written as $n$ coupled first order ODEs, namely 

\begin{align}
\frac{dy}{dt} &= v\\
\frac{dv}{dt} &= -g.
\end{align}

Solve each of the first order ODEs with Euler:

\begin{align}
y(t + h) &= y(t) + h v(t)\\
v(t + h) &= v(t) - h g.
\end{align}


```python
import numpy as np
```

## Free fall 

Start with free fall


```python
g = -9.81
y = 0.0
v = 0.0

t = 0
dt = 0.01

data = []

while t < 10:
    y = y + v*dt
    v = v + g*dt
    data.append([t, y, v]) 
    t += dt

data = np.array(data) 
```


```python
data.shape
```




    (1001, 3)




```python
data = data.transpose()
data.shape  # t, y, v
```




    (3, 1001)




```python
import matplotlib.pyplot as plt
%matplotlib inline
```


```python
plt.plot(data[0], data[1])
plt.xlabel("time (s)")
plt.ylabel("position (m)")
```

## Bouncing
Add a floor at $y = -5$.

What happens at the floor? – The velocity changes (elastic collision).


```python
g = -9.81
y = 0.0
v = 0.0

t = 0
dt = 0.01

y_floor = -5

data = []

while t < 10:
    y += v*dt
    if y > y_floor:
        v += g*dt
    else:
        v = -v   # bounce off floor
    data.append([t, y, v]) 
    t += dt

data = np.array(data).transpose() 
```


```python
plt.plot(data[0], data[1])
plt.xlabel("time (s)")
plt.ylabel("position (m)")
```


```python

```

ipynb
6944d9f21728ca24c5cca2b8055e2b46f7c1546d
Jupyter Notebook
635a6678569406e11865c8a583a56f4a3cf2bdc4
CC-BY-4.0
ASU-CompMethodsPhysics-PHY494/PHY494-resources-2018
10_ODEs/bounce_ball_numpy.ipynb
635a6678569406e11865c8a583a56f4a3cf2bdc4
CC-BY-4.0
ASU-CompMethodsPhysics-PHY494/PHY494-resources-2018
10_ODEs/bounce_ball_numpy.ipynb
635a6678569406e11865c8a583a56f4a3cf2bdc4
CC-BY-4.0
ASU-CompMethodsPhysics-PHY494/PHY494-resources-2018
10_ODEs/bounce_ball_numpy.ipynb