Code
"""
ab4(ivp,n)
Apply the Adams-Bashforth 4th order method to solve the given IVP
using `n` time steps. Returns a vector of times and a vector of
solution values.
"""
function ab4(ivp,n)
# Time discretization.
a,b = ivp.tspan
h = (b-a)/n
t = [ a + i*h for i in 0:n ]
# Constants in the AB4 method.
k = 4; σ = [55,-59,37,-9]/24;
# Find starting values by RK4.
u = fill(float(ivp.u0),n+1)
rkivp = ODEProblem(ivp.f,ivp.u0,(a,a+(k-1)*h),ivp.p)
ts,us = rk4(rkivp,k-1)
u[1:k] .= us
# Compute history of u' values, from newest to oldest.
f = [ ivp.f(u[k-i],ivp.p,t[k-i]) for i in 1:k-1 ]
# Time stepping.
for i in k:n
f = [ ivp.f(u[i],ivp.p,t[i]), f[1:k-1]... ] # new value of du/dt
u[i+1] = u[i] + h*sum(f[j]*σ[j] for j in 1:k) # advance a step
end
return t,u
endab4