All problems
Hamiltonian from Lagrangian
Problem
For L(q, qdot) = 0.5*m*qdot^2 + c*q*qdot - 0.5*k*q^2, the conjugate momentum is p = m*qdot + c*q.
Eliminating qdot = (p - c*q) / m gives H(q, p) = (p - c*q)^2 / (2*m) + 0.5*k*q^2. Given the parameters and (q, p), return H.
Hint
The Hamiltonian is the Legendre transform of the Lagrangian in the velocity.
Test cases
case 1
in: [1,0,1,1,0]
expected: 0.5
case 2
in: [2,0,0,0,4]
expected: 4
case 3
in: [1,1,2,1,3]
expected: 3
case 4
in: [2,-1,4,2,1]
expected: 10.25
Starter codePython
def hamiltonian_from_lagrangian(m, c, k, q, p):
# Your code here
pass4 test cases · run your code against them in the editor