Skip to content

Numerical solution for Hamilton's equations #11

Description

@YingboMa

One can compute Hamiltonian from Lagrangian via Legendre transformation, so I propose the following implementation.

function Legendre_transformation(F, w) # Legendre transformation for quadratic function
    wv = a->ForwardDiff.gradient(F, a)
    z = zeros(w)
    M = ForwardDiff.jacobian(wv, z)
    b = wv(z)
    v = bkfact!(M)\(w-b)
    w'v - F(v)
end

function Lagrangian2Hamiltonian(Lagrangian, t, q, p)
    L =-> Lagrangian(t, q, q̇)
    Legendre_transformation(L, p)
end

Finding the Legendre transformation of an arbitrary function is very hard numerically, but Lagrangian is quadratic in most of mechanics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions