|
23 | 23 | using OptimalControl
|
24 | 24 | using NLPModelsIpopt
|
25 | 25 | include("smooth.jl")</code></pre><pre><code class="language-julia hljs">fNC(x) = fNC_unboundedminus(x, 0, 0.018)
|
26 |
| -plot(fNC, -1, 1, label="fNC")</code></pre><img src="ho-3c9adc70.svg" alt="Example block output"/><pre><code class="language-julia hljs">const ε = 1e-3 |
| 26 | +plot(fNC, -1, 1, label="fNC")</code></pre><img src="ho-f88cf81d.svg" alt="Example block output"/><pre><code class="language-julia hljs">const ε = 1e-3 |
27 | 27 |
|
28 | 28 | @def ocp begin
|
29 | 29 |
|
|
93 | 93 | Number of equality constraint Jacobian evaluations = 378
|
94 | 94 | Number of inequality constraint Jacobian evaluations = 0
|
95 | 95 | Number of Lagrangian Hessian evaluations = 377
|
96 |
| -Total seconds in IPOPT = 14.080 |
| 96 | +Total seconds in IPOPT = 13.912 |
97 | 97 |
|
98 |
| -EXIT: Optimal Solution Found.</code></pre><pre><code class="language-julia hljs">plot(sol; layout=:group, size=(800, 300))</code></pre><img src="ho-6fa41275.svg" alt="Example block output"/><pre><code class="language-julia hljs">tf = variable(sol) |
| 98 | +EXIT: Optimal Solution Found.</code></pre><pre><code class="language-julia hljs">plot(sol; layout=:group, size=(800, 300))</code></pre><img src="ho-1b2bb116.svg" alt="Example block output"/><pre><code class="language-julia hljs">tf = variable(sol) |
99 | 99 | tt = (0:N+1) * (tf/(N+1))
|
100 | 100 |
|
101 | 101 | x1(t) = state(sol)(t)[1]
|
|
105 | 105 | p1(t) = costate(sol)(t)[1]
|
106 | 106 | p2(t) = costate(sol)(t)[2]
|
107 | 107 | a = λ(tf)</code></pre><pre><code class="language-julia hljs">plot(x1, x2, 0, tf, label="optimal trajectory", color="blue", linewidth=2)
|
108 |
| -plot!([-4, 5], [0, 0], color=:black, label=false, linewidth=2)</code></pre><img src="ho-b53ab9f6.svg" alt="Example block output"/><pre><code class="language-julia hljs">plot(tt, u, label="optimal control", color="red", linewidth=2) |
109 |
| -plot!(tt, λ, label="state λ", color="green", linewidth=2)</code></pre><img src="ho-e5558d62.svg" alt="Example block output"/><pre><code class="language-julia hljs"># Find the crossing times based on conditions for x1 |
| 108 | +plot!([-4, 5], [0, 0], color=:black, label=false, linewidth=2)</code></pre><img src="ho-24a04553.svg" alt="Example block output"/><pre><code class="language-julia hljs">plot(tt, u, label="optimal control", color="red", linewidth=2) |
| 109 | +plot!(tt, λ, label="state λ", color="green", linewidth=2)</code></pre><img src="ho-7bd21620.svg" alt="Example block output"/><pre><code class="language-julia hljs"># Find the crossing times based on conditions for x1 |
110 | 110 | t1_index = findfirst(t -> x2(t) ≤ 0, tt)
|
111 | 111 | t2_index = nothing
|
112 | 112 | t3_index = nothing
|
|
258 | 258 | x22 = [ xx[i][2] for i=1:m ]
|
259 | 259 | p11 = [ pp[i][1] for i=1:m ]
|
260 | 260 | p22 = [ pp[i][2] for i=1:m ]</code></pre><pre><code class="language-julia hljs">plot(x11, x22, label="optimal trajectory", linecolor=:blue , linewidth=2)
|
261 |
| -plot!([-4, 5], [0, 0], color=:black, label=false, linewidth=2)</code></pre><img src="ho-c517b199.svg" alt="Example block output"/><pre><code class="language-julia hljs">plot(tt0, uu, label="optimal control", linecolor=:red , linewidth=2)</code></pre><img src="ho-90688599.svg" alt="Example block output"/><pre><code class="language-julia hljs">plot(tt0, p11, label="costate p1", linecolor=:purple , linewidth=2) |
262 |
| -plot!(tt0, p22, label="costate p2", linecolor=:violet , linewidth=2)</code></pre><img src="ho-5314a923.svg" alt="Example block output"/><pre><code class="language-julia hljs"># create an animation |
| 261 | +plot!([-4, 5], [0, 0], color=:black, label=false, linewidth=2)</code></pre><img src="ho-c07cf0c2.svg" alt="Example block output"/><pre><code class="language-julia hljs">plot(tt0, uu, label="optimal control", linecolor=:red , linewidth=2)</code></pre><img src="ho-7da94578.svg" alt="Example block output"/><pre><code class="language-julia hljs">plot(tt0, p11, label="costate p1", linecolor=:purple , linewidth=2) |
| 262 | +plot!(tt0, p22, label="costate p2", linecolor=:violet , linewidth=2)</code></pre><img src="ho-06036e37.svg" alt="Example block output"/><pre><code class="language-julia hljs"># create an animation |
263 | 263 | animx = @animate for i = 1:length(tt0)
|
264 | 264 | plot(x11[1:i], x22[1:i], xlim=(-3.,5.), ylim=(-4.,4.3), label="optimal trajectory",
|
265 | 265 | linecolor=:blue, linewidth=2, legend=:topleft)
|
|
0 commit comments