Skip to content
DeepForgeby svx
All problems
HardCalculusca-046

RK2 Midpoint Method One Step

Problem

Take one step of the second-order Runge-Kutta midpoint method for the ODE y' = a*y + b*t.

k1 = f(t, y), y_mid = y + 0.5*dt*k1, k2 = f(t + 0.5*dt, y_mid), y_next = y + dt*k2.

Return y_next.

Hint

Evaluate the slope twice: once at the start and once at the predicted midpoint.

Test cases

case 1
in: [1,0,0.1,1,0]
expected: 1.105
case 2
in: [0,1,0.5,0,1]
expected: 0.625
case 3
in: [2,0,0.25,-1,0]
expected: 1.5625
case 4
in: [1,0.5,0.2,2,-1]
expected: 1.34
Starter codePython
def rk2_step(y, t, dt, a, b):
    # Your code here
    pass

4 test cases · run your code against them in the editor