Skip to content
DeepForgeby svx
All problems
HardCalculusca-140

Hamilton Equations Step

Problem

Take one explicit Euler step of Hamilton's equations for H(q, p) = p^2/(2m) + 0.5*k*q^2 + 0.25*lam*q^4.

The equations are q' = p/m and p' = -(k*q + lam*q^3). Return [q + dt*q', p + dt*p'].

Hint

Update the position with the current momentum and the momentum with the current force.

Test cases

case 1
in: [1,0,1,1,0,0.1]
expected: [1,-0.1]
case 2
in: [2,1,2,3,0.5,0.25]
expected: [2.125,-1.5]
case 3
in: [0,2,1,4,1,0.5]
expected: [1,2]
case 4
in: [1,-1,1,1,0,0.5]
expected: [0.5,-1.5]
case 5
in: [0,0,1,1,1,1]
expected: [0,0]
Starter codePython
def hamilton_step(q, p, m, k, lam, dt):
    # Return [q_next, p_next]
    # Your code here
    pass

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