Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples_oc/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
OptimalControl = "5f98b655-cc9a-415a-b60e-744165666948"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
54 changes: 54 additions & 0 deletions examples_oc/ex1.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using OptimalControl
using Plots
using OrdinaryDiffEq
import BifurcationKit: BifurcationProblem, continuation, PALC, ContinuationPar

# parameters
t0 = 0
x0 = -2
tf = 1

# model
ocp = @def begin
λ ∈ R, variable
t ∈ [t0, tf], time
x ∈ R, state
u ∈ R, control
x(t0) == x0
x(tf)^2 - λ^2 == 0
ẋ(t) == u(t)
∫( u(t)^2 ) → min
end

# maximising control
u(x, p, λ) = p/2

# Hamiltonian flow
f = Flow(ocp, u)

# state projection
π((x, p)) = x

# shooting function
S(p0::Number, λ::Number) = π( f(t0, x0, p0, tf, λ) )^2 - λ^2
S(p0::AbstractVector, λ::AbstractVector) = [S(p0[1], λ[1])]

# continuation: branch 1
p0_init = 2
λ_init = -1
λ_final = 1
prob = BifurcationProblem(S, Float64[p0_init], Float64[λ_init], 1;
record_from_solution = (x,p; k...) -> x[1])
br = continuation(prob, PALC(), ContinuationPar(p_min = Float64(λ_init), p_max = Float64(λ_final)))
plt = plot(br; dpi=200)

# continuation: branch 2
p0_init = 6
λ_init = -1
λ_final = 1
prob = BifurcationProblem(S, Float64[p0_init], Float64[λ_init], 1;
record_from_solution = (x,p; k...) -> x[1])
br = continuation(prob, PALC(), ContinuationPar(p_min = Float64(λ_init), p_max = Float64(λ_final)))
plot!(plt, br; dpi=200)

savefig(plt, "ex1_plot.png")
Binary file added examples_oc/ex1_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading