Skip to content

Commit 8793e52

Browse files
committed
Add Hamiltonian verification section
- Define Hamiltonian H(x, p, u) = H0(x, p) + u * H1(x, p) - Compute and display H along direct and indirect trajectories - Show Hamiltonian variation (should be near zero for optimal solutions) - Plot H(t) for both methods for visual comparison
1 parent 3d11fc4 commit 8793e52

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

docs/src/tutorial-goddard.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,44 @@ We now compare numerically the direct and indirect solutions. The comparison inc
505505
print_numerical_comparisons(direct_sol, indirect_sol)
506506
```
507507

508+
## Hamiltonian verification
509+
510+
The Hamiltonian should be constant along the optimal trajectory and equal to zero since the final time is free. Let us verify this for both the direct and indirect solutions.
511+
512+
```@example main-goddard
513+
# Hamiltonian function
514+
H(x, p, u) = H0(x, p) + u * H1(x, p)
515+
516+
# Direct solution
517+
t_dir = time_grid(direct_sol)
518+
x_dir = state(direct_sol)
519+
u_dir = control(direct_sol)
520+
p_dir = costate(direct_sol)
521+
H_direct = [H(x_dir(ti), p_dir(ti), u_dir(ti)) for ti in t_dir]
522+
523+
# Indirect solution
524+
t_ind = time_grid(indirect_sol)
525+
x_ind = state(indirect_sol)
526+
u_ind = control(indirect_sol)
527+
p_ind = costate(indirect_sol)
528+
H_indirect = [H(x_ind(ti), p_ind(ti), u_ind(ti)) for ti in t_ind]
529+
530+
println("Direct method:")
531+
println(" H(t0) = ", H_direct[1])
532+
println(" H variation: max|H(t) - H(t0)| = ", maximum(abs.(H_direct .- H_direct[1])))
533+
534+
println("\nIndirect method:")
535+
println(" H(t0) = ", H_indirect[1])
536+
println(" H variation: max|H(t) - H(t0)| = ", maximum(abs.(H_indirect .- H_indirect[1])))
537+
538+
# Plot
539+
plot(t_dir, H_direct, label="Direct", linewidth=2)
540+
plot!(t_ind, H_indirect, label="Indirect", linewidth=2, linestyle=:dash)
541+
xlabel!("Time")
542+
ylabel!("H(t)")
543+
title!("Hamiltonian along the trajectory")
544+
```
545+
508546
## References
509547

510548
[^1]: R.H. Goddard. A Method of Reaching Extreme Altitudes, volume 71(2) of Smithsonian Miscellaneous Collections. Smithsonian institution, City of Washington, 1919.

0 commit comments

Comments
 (0)