Slide 17
Slide 17 text
In [ ]: def IRKGL(t, r, v, f):
"""
Implicit Runge-Kutta Gauss-Legendre 2-stage
"""
A = numpy.array(...)
b = numpy.array(...)
c = numpy.array(...)
U = numpy.zeros((6, t.shape[0]))
U[0:3, 0] = r
U[3:6, 0] = v
delta_t = t[1] - t[0]
for i in xrange(t.shape[0] - 1):
# Set stage function evaluations y1=0 and y2=0
K = numpy.zeros((6, 2))
D = numpy.dot(K, A.T) # a_11*u1 + a_12*u2
# Initial guess for k1 and k2
K_new = numpy.zeros((6, 2))
K_new[:, 0] = f(t + c[0]*delta_t, U[:,i] + delta_t*D[:, 0])
K_new[:, 1] = f(t + c[1]*delta_t, U[:,i] + delta_t*D[:, 1])
# Solve nonlinear system for stage evaluations
error = numpy.max(numpy.abs(K - K_new))
tolerance = 1e-08
while error > tolerance:
K = K_new.copy()
D = numpy.dot(K, A.T)
K_new[:, 0] = f(t + c[0]*delta_t, U[:,i] + delta_t*D[:, 0])
K_new[:, 1] = f(t + c[1]*delta_t, U[:,i] + delta_t*D[:, 1])
error = numpy.max(numpy.abs(K - K_new))
U[:, i+1] = U[:, i] + delta_t*numpy.dot(K_new, b).reshape(6)
return U