There are three popular methods for solving linear ODEs of the first order.
In this lab, we implement the most popular
In this method, we define the general form as: $$ \begin{eqnarray} k_1 & = & \Delta{t}f(t_n, x(t_n)) \\ k_2 & = & \Delta{t}f(t_n+\alpha\Delta{t}, x(t_n)+\beta{k}_1) \\ k_3 & = & \Delta{t}f(t_n+\gamma\Delta{t}, x(t_n)+\delta{k}_1+\epsilon{k}_2) \\ k_4 & = & \Delta{t}f(t_n+\zeta\Delta{t}, x(t_n) + \eta{k}_1+\theta{k}_2+\iota{k}_3) \end{eqnarray} $$ and finally, $$ x(t_{n+1}) = x(t_n) + ak_1 + bk_2 + ck_3 $$
Thankfully, the most popular RK-4 Method does not use so much of the Greek alphabet but really is quite simple! $$ \begin{eqnarray} k_1 & = & \Delta{t}f(t_n, x(t_n)) \\ k_2 & = & \Delta{t}f(t_n+\frac{\Delta{t}}{2}, x(t_n)+\frac{k_1}{2}) \\ k_3 & = & \Delta{t}f(t_n+\frac{\Delta{t}}{2}, x(t_n)+\frac{k_2}{2}) \\ k_4 & = & \Delta{t}f(t_n+\Delta{t}, x(t_n)+k_3),\,\,\,\,\, \mbox{and}\\ x(t_{n+1}) & = & x(t_n) + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) \end{eqnarray} $$
Implement RK-4 in the cell below
import numpy as np
###################### ADD YOUR RK-4 CODE BELOW
def ode_rk4(func, x0, t0, tn, dt=0.2) :
return xtn
We saw that the solution is obtained by defining $N$ points between the two boundaries (including the boundaries). Then it is solved in our oh-so-loved way using
The solution is done in three steps:
Implement the boundary value problem in the cell(s) below.
def ode_bvalue(func, x1, x2, t1, t2, h=0.2) :
return xvals