Fill in any place that says `# YOUR CODE HERE` or YOUR ANSWER HERE, as well as your name and collaborators below.
Grading for pre-lecture assignments is all or nothing. Partial credit is available for in-class assignments and checkpoints, but **only when code is commented**.


```python
NAME = ""
COLLABORATORS = ""
```

---

# Learning Objectives

This lecture will show you how to:
1. Solve 1st order ODEs with Euler's method
2. Solve 1st order ODEs with the Runge-Kutta method
3. Solve systems of 1st order ODEs
4. Solve 2nd order (and above) ODEs


```python
# imports
import numpy as np
import matplotlib.pyplot as plt

import grading_helper as _test
```

# Euler's Method


```python
%video _ulNf7dF1Fc
```

Summary:

Write a 1st order ODE in the form

$$\frac{dx}{dt}=f(x,t)\,.$$

Then, starting with **initial condition** $x_0=x(t_0)$, repeated apply

$$x(t+h)=x(t)+h\,f(x,t)\,,$$

with a small step size $h$.


```python

```

## Your Turn

Let
$$\frac{dx}{dt} = x\cos t\,,$$
with $x(0) = 1$.
1. Solve this equation by hand (separate variables and integrate). Plot $x(t)$ for $0 \le t \le 2\pi$.
2. Use Euler's method with $h=\pi/10$. Plot a dot at each value of $x$ that the method computes. Store these values in an array named `x`.

> The graphs will be similar, but not match exactly. The step size $h$ is pretty large.


```python
%%graded # 1 point

# YOUR CODE HERE
```


```python
%%tests

_test.equal(len(x), 21)
_test.equal(x[0], 1) # initial condition
_test.similar(x, [1.00, 1.31, 1.71, 2.14, 2.54, 2.78, 2.78, 2.51, 2.05, 1.53, 1.07,
                  0.735, 0.515, 0.384, 0.313, 0.283,  0.283,  0.310, 0.368, 0.46, 0.600])
_test.plot_shown()
```

# Runge-Kutta Method


```python
%video qUnPw5ZUuUs
```

Summary:

- **1st Order Runge-Kutta**<br>
We've already seen it. It's Euler's method.


- **2nd Order Runge-Kutta (A.K.A Midpoint Method)**

$$\begin{align} k_1 &= h\,f(x,t)\\
k_2 &= h\,f\big(x+\tfrac12 k_1, t+\tfrac12 h\big)\\
x(t+h) &= x(t) + k_2
\end{align}$$

 (The first line is used to estimate where the midpoint is, and the rest uses Euler's method to find the next value of $x$.)


- **4th Order Runge-Kutta**

$$\begin{align}
k_1 &= h\,f(x,t)\\
k_2 &= h\,f\big(x+\tfrac12 k_1, t+\tfrac12 h\big)\\
k_3 &= h\,f\big(x+\tfrac12 k_2, t+\tfrac12 h\big)\\
k_4 &= h\,f\big(x+k_3, t+h\big)\\
x(t+h) &= x(t) + \tfrac16\big(k_1+2k_2+2k_3+k_4\big)
\end{align}$$


- By far, the 4th order Runge-Kutta is the most popular of these methods. It hits a sweet spot between accuracy and complexity.


```python

```

## Your Turn

Write a function `RK4(f, x, t, h)` that takes one step of the 4th Order Runge-Kutta method for a function `f(x, t)`. You'll use this code for the in-class assignment.


```python
%%graded # 2 points

# YOUR CODE HERE
```


```python
%%tests

def _f(x, t): return x*np.cos(t) # function from previous part

_test.similar(RK4(_f, 1, 0, np.pi/10), 1.36) # 1st step
_test.similar(RK4(_f, 1.36, np.pi/10, np.pi/10), 1.80) # 2nd step
```

# Simultaneous 1st Order ODEs


```python
%video _aA9m8IKfVM
```

Summary:

- Write equations in the form

 $$\frac{dx}{dt}=f_x(x, y, t)\,,\qquad \frac{dy}{dt}=f_y(x, y, t)\,.$$

 With two equations, we need two initial conditions: $x_0=x(t_0)$ and $y_0=y(t_0)$.

- This set of equations can be written as a single vector equation:

 $$\frac{d\mathbf{r}}{dt}=f(\mathbf{r}, t)\,,$$
 
 where $\mathbf{r}=(x,y)$. Likewise, $\mathbf{r_0}=\mathbf{r}(t_0)$.

- Using arrays, we can generalize the 4th order Runge-Kutta method:

 $$\begin{align}
 \mathbf{k_1} &= h\,f(\mathbf{r},t)\\
 \mathbf{k_2} &= h\,f\big(\mathbf{r}+\tfrac12 \mathbf{k_1}, t+\tfrac12 h\big)\\
 \mathbf{k_3} &= h\,f\big(\mathbf{r}+\tfrac12 \mathbf{k_2}, t+\tfrac12 h\big)\\
 \mathbf{k_4} &= h\,f\big(\mathbf{r}+\mathbf{k_3}, t+h\big)\\
 \mathbf{r}(t+h) &= \mathbf{r}(t) + \tfrac16\big(\mathbf{k_1}+2\mathbf{k_2}+2\mathbf{k_3}+\mathbf{k_4}\big)
 \end{align}$$

 In fact, the code we wrote for this should work as-is!


```python

```

## Your Turn

The Lotka-Volterra equations are a simplified model of predator-prey population size. Let

$$\frac{dR}{dt} = R - \frac{FR}{2}$$

control the number of rabbits (in units of 1000 rabbits/year) and

$$\frac{dF}{dt} = \frac{FR}{2} - 2F$$

control the number of foxes (in units of 1000 foxes/year). Plot the numbers of rabbits and foxes over 10 years, starting with 2000 of each (i.e. $R_0=F_0=2$). Save the results in a pair of arrays named `R` and `F`.


```python
%%graded # 1 point

# YOUR CODE HERE
```


```python
%%tests

_test.similar(min(R), 2.00)
_test.similar(max(R), 7.03)
_test.similar(min(F), 0.715)
_test.similar(max(F), 4.31)
_test.plot_shown()
```

# 2nd Order ODEs


```python
%video dUmFpcDvQpk
```

Summary:

- Given

 $$\frac{d^2x}{dt^2}=f\bigg(x, \frac{dx}{dt}, t \bigg)\,,$$
 
 the trick is to define $v=dx/dt$, then we can write a pair of 1st order equations:

 $$\begin{aligned}
 \frac{dv}{dt} &=f(x, v, t)\\
 \frac{dx}{dt} &=v\,.
 \end{aligned}$$


- We can solve these using a 4th-order Runge Kutta or similar method.

- We need initial conditions for both $x$ and $v$.


```python

```

## Your Turn

A pendulum driven by an oscillating horizontal force is described using the equation

$$\frac{d^2\theta}{dt^2}=-\frac{g}{\ell}\sin\theta + C\cos\theta\sin(\omega_dt)\,,$$

where we'll use $\ell=10$ cm, $C=2$ s$^{-2}$ and $\omega_d=\sqrt{g/\ell}$.

Complete the code below to make a plot of $\theta(t)$ for $0<t<30$ s, with $\theta_0=0$ and $\omega_0=0$.


```python
%%graded # 1 point


g, l, C, wd = 9.8, 0.1, 2, np.sqrt(g/l)

# your job is to write this function
def f(r, t):
# YOUR CODE HERE

h = 0.01
time = np.arange(0, 30, h)
th, w = 0, 0
th_list = []
for t in time:
    th_list.append(th)
    th, w = RK4(f, (th, w), t, h) # you'll need a working version of RK4 from above
    
plt.plot(time, th_list)
plt.show()
```


```python
%%tests

_test.plot_shown() # the graph should look like a fish
```

# Additional Resources

- Textbook sections 8.1 - 8.4

96eac9df8393949f0d04cf7dda6ebb6ccc5ebd1e
ipynb
Jupyter Notebook
Assignments/10.1 Lecture - Basic Differential Equations.ipynb
davidnero/phys1321-lectures
328222be3de1d68de531f0cc008a6607032e6721
BSD-4-Clause-UC
Assignments/10.1 Lecture - Basic Differential Equations.ipynb
davidnero/phys1321-lectures
328222be3de1d68de531f0cc008a6607032e6721
BSD-4-Clause-UC
Assignments/10.1 Lecture - Basic Differential Equations.ipynb
davidnero/phys1321-lectures
328222be3de1d68de531f0cc008a6607032e6721
BSD-4-Clause-UC
Qwen/Qwen-72B
1. YES
2. YES