-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.jl
98 lines (73 loc) · 2.13 KB
/
temp.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#
#using Pkg
#Pkg.activate(".")
#
using Formatting
using ForwardDiff
using MINPACK
using OptimalControl
using Plots
using Plots.PlotMeasures # for leftmargin, bottommargin
println("OCP")
t0 = 0
tf = 1
x0 = -1
xf = -0.451475
α = 0.0
@def ocp begin
t ∈ [ t0, tf ], time
x ∈ R, state
u ∈ R, control
x(t0) == x0
x(tf) == xf
ẋ(t) == -α*x(t) + u(t) * x(t)
∫( 0.5u(t)^2 + 0.5*(1-x(t))^2) → min
end
println("Shooting function")
u(x, p) = p;
φ = Flow(ocp, u); # flow from the optimal control problem ocp and maximising control u
# definition of the state projection
π(x, p) = x
π(z::Tuple{Number, Number}) = π(z...) # z = (x, p)
# shooting function
S(p0) = π( φ(t0, x0, p0, tf) ) - xf;
Jₛ(p0) = ForwardDiff.jacobian(x -> [S(x[1])], [p0])[1, 1]; # S'(p0)
println("Shooting method")
p0 = 1.5 # initial costate for the Newton solver
# iterates = [p0] # list of iterates
# iterations = 5 # number of iterations
# for i ∈ 1:iterations
# d = - Jₛ(p0) \ S(p0) # Newton direction
# p0 = p0 + d # update
# push!(iterates, p0) # save
# end
println("Shooting method: MINPACK")
# Resolution with Minpack hybrj solver
nle = (s, ξ) -> s[1] = S(ξ[1]) # auxiliary function
ξ = [ p0 ] # initial guess
indirect_sol = fsolve(nle, ξ) # resolution of S(p0) = 0
p0_solution = indirect_sol.x[1] # costate solution
println(indirect_sol)
p0 = p0_solution
println("p0 solution = ", p0_solution)
println("JS(p0 sol) = ", Jₛ(p0))
#
println("Plot")
#
include("temp_plot.jl")
#
idx = 3
# copy the plt_flow
plt_flow_copy = deepcopy(plt_flow)
plt_shoot_copy = deepcopy(plt_shoot)
# add extremals
# if idx ≥ 0
# plot_extremals!(plt_flow_copy, iterates, idx)
# end
# add iterates on the plot of shooting function
# if idx ≥ 0
# plot_iterates!(plt_shoot_copy, iterates, idx)
# end
# plot all
plot(plt_flow_copy, plt_shoot_copy, layout=(1,2), size=(900, 450),
leftmargin=5mm, bottommargin=5mm)