# 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{dx}{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

```

575587757c1b2331dcc9ad79136bde1e8ce4554b
ipynb
Jupyter Notebook
11_ODEs/bounce_ball_numpy.ipynb
nachrisman/PHY494
bac0dd5a7fe6f59f9e2ccaee56ebafcb7d97e2e7
CC-BY-4.0
11_ODEs/bounce_ball_numpy.ipynb
nachrisman/PHY494
bac0dd5a7fe6f59f9e2ccaee56ebafcb7d97e2e7
CC-BY-4.0
11_ODEs/bounce_ball_numpy.ipynb
nachrisman/PHY494
bac0dd5a7fe6f59f9e2ccaee56ebafcb7d97e2e7
CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES