Skip to content
DeepForgeby svx
All problems
HardOptimizationop-152

RK4 Step

Problem

Take one classical fourth-order Runge-Kutta step of the gradient flow on f(x) = x^2, where dx/dt = -2x.

Compute k1 = -2x, k2 = -2(x + 0.5 * dt * k1), k3 = -2(x + 0.5 * dt * k2), and k4 = -2(x + dt * k3), then return x + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4).

Hint

The four stages estimate the slope at the start, two midpoints, and the end.

Test cases

case 1
in: [1,0.1]
expected: 0.8187333333333333
case 2
in: [2,0.5]
expected: 0.75
case 3
in: [0,0.3]
expected: 0
case 4
in: [-1,0.2]
expected: -0.6704
Starter codePython
def rk4_step(x, dt):
    # Your code here
    pass

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