# Problem with HamiltonianProblem in Differential

**URL:** https://discourse.julialang.org/t/problem-with-hamiltonianproblem-in-differential/91161
**Category:** Modelling & Simulations
**Tags:** question, differentialequation
**Created:** [December 2, 2022, 10:31pm UTC](https://discourse.julialang.org/t/problem-with-hamiltonianproblem-in-differential/91161 "2022-12-02T22:31:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mpirke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpirke/32/44837_2.png) [@mpirke](https://discourse.julialang.org/u/mpirke)
#### Post date: [December 2, 2022, 10:31pm UTC](https://discourse.julialang.org/t/problem-with-hamiltonianproblem-in-differential/91161/1 "2022-12-02T22:31:05Z")

</div>

Hi there!

Im playing around with some easy physics problems. Here I tried to simulate a simple pendulum. I wanted to solve it via the HamiltonianProblem functionality from DifferentialEquations.jl.  
Im not sure what’s the problem with the code below…  
Thanks for help and advice!

```julia
using DifferentialEquations, DiffEqPhysics

struct Pendulum
    m
    g
    l
    ϕ₀
    dϕ₀
end

function (pen::Pendulum)(q, p) #hamiltonian of a pendulum 
    p^2 / (2 * pen.m * (pen.l)^2) - pen.m * pen.g * pen.l * cos(q)
end

function startvalues(p::Pendulum)
    q₀ = p.ϕ₀
    p₀ = p.dϕ₀ * p.m * (p.l)^2
    q₀, p₀
end

function simulate(p::Pendulum, tspan)
    q₀, p₀ = startvalues(p)
    prob = HamiltonianProblem(p, p₀, q₀, tspan)
    sol = solve(prob, Tsit5())

    sol
end

p = Pendulum(1.0, 9.81, 1.0, pi/2, 0.0)
tspan = (0.0, 1.0)
simulate(p, tspan)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 3, 2022, 3:13am UTC](https://discourse.julialang.org/t/problem-with-hamiltonianproblem-in-differential/91161/2 "2022-12-03T03:13:02Z")

</div>

> [@mpirke](#):
>
> ```julia
> function (pen::Pendulum)(q, p) #hamiltonian of a pendulum 
> p^2 / (2 * pen.m * (pen.l)^2) - pen.m * pen.g * pen.l * cos(q)
> end
> 
> ```

That’s not one of the allowed forms.

```julia
Define a physical system by its Hamiltonian function `H(p, q, param)` or the function pair
`dp = -∂H/∂q` and `dq = ∂H/∂p`.

```

You’re missing `param`. The following works:

```julia
using DifferentialEquations, DiffEqPhysics

struct Pendulum
    m
    g
    l
    ϕ₀
    dϕ₀
end

function (pen::Pendulum)(p, q, param) #hamiltonian of a pendulum
    p^2 / (2 * pen.m * (pen.l)^2) - pen.m * pen.g * pen.l * cos(q)
end

function startvalues(p::Pendulum)
    q₀ = p.ϕ₀
    p₀ = p.dϕ₀ * p.m * (p.l)^2
    q₀, p₀
end

function simulate(p::Pendulum, tspan)
    q₀, p₀ = startvalues(p)
    prob = HamiltonianProblem(p, p₀, q₀, tspan)
    sol = solve(prob, Tsit5())

    sol
end

p = Pendulum(1.0, 9.81, 1.0, pi/2, 0.0)
tspan = (0.0, 1.0)
simulate(p, tspan)

```

(also notice the fix to the `p,q` order)

Note that this now has a better error message:

```julia
ERROR: All methods for the Hamiltonian `H` had too few arguments. A
Hamiltonian `H` must define either `H(p, q, param)` or `H(p, q, param, t)`. This error
can be thrown if you define an Hamiltonian for example as `H(p, q)`.
Note that `param` must be in the arguments list even if it's not used.
For more information on the required number of arguments for the function
you were defining, consult the documentation for the `HamiltonianProblem`.

Offending function: f
Methods:
# 1 method for callable object:
[1] (pen::Pendulum)(q, p) in Main at d:\OneDrive\Computer\Desktop\test.jl:120

Stacktrace:
 [1] HamiltonianProblem{false}(H::Pendulum, p0::Float64, q0::Float64, tspan::Tuple{Float64, Float64}, param::Nothing; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ DiffEqPhysics C:\Users\accou\.julia\dev\DiffEqPhysics\src\hamiltonian.jl:122
 [2] HamiltonianProblem
   @ C:\Users\accou\.julia\dev\DiffEqPhysics\src\hamiltonian.jl:115 [inlined]
 [3] #HamiltonianProblem#1
   @ C:\Users\accou\.julia\dev\DiffEqPhysics\src\hamiltonian.jl:95 [inlined]
 [4] HamiltonianProblem (repeats 2 times)
   @ C:\Users\accou\.julia\dev\DiffEqPhysics\src\hamiltonian.jl:93 [inlined]
 [5] simulate(p::Pendulum, tspan::Tuple{Float64, Float64})
   @ Main d:\OneDrive\Computer\Desktop\test.jl:132
 [6] top-level scope
   @ d:\OneDrive\Computer\Desktop\test.jl:140

```

---

<div class="post-metadata">

### Author: ![mpirke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpirke/32/44837_2.png) [@mpirke](https://discourse.julialang.org/u/mpirke)
#### Post date: [December 3, 2022, 9:58am UTC](https://discourse.julialang.org/t/problem-with-hamiltonianproblem-in-differential/91161/3 "2022-12-03T09:58:48Z")

</div>

Thank you for the quick response Chris! Much appreciate your work! Nice that you also changed the error message, with that will be nice for someone in the future who does the same mistake as I did! The error message I received was a bit cryptic 😃
