Skip to content

Commit 29f2790

Browse files
committed
goddard + iss tuto fix
1 parent a2e28c3 commit 29f2790

2 files changed

Lines changed: 12 additions & 111 deletions

File tree

docs/src/tutorial-goddard.md

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ using OrdinaryDiffEq # to get the Flow function from OptimalControl
4949
using NonlinearSolve # interface to NLE solvers
5050
using MINPACK # NLE solver: use to solve the shooting equation
5151
using Plots # to plot the solution
52-
using BenchmarkTools # do some benchmarking
5352
```
5453

5554
## Optimal control problem
@@ -287,52 +286,9 @@ We aggregate the data to define the initial guess vector.
287286
ξ = [p0; t1; t2; t3; tf] # initial guess
288287
```
289288

290-
### NonlinearSolve.jl
291-
292-
We first use the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package to solve the shooting
293-
equation. Let us define the problem.
294-
295-
```@example main
296-
# auxiliary function with aggregated inputs
297-
nle! = (s, ξ, λ) -> shoot!(s, ξ[1:3], ξ[4], ξ[5], ξ[6], ξ[7])
298-
299-
# NLE problem with initial guess
300-
prob = NonlinearProblem(nle!, ξ)
301-
nothing # hide
302-
```
303-
304-
Let us do some benchmarking. This will be useful to compare the performance with the MINPACK.jl package below.
305-
306-
```@setup main
307-
function nlsolve(prob; kwargs...)
308-
try
309-
NonlinearSolve.solve(prob; kwargs...)
310-
catch e
311-
println("Error using NonlinearSolve")
312-
println(e)
313-
end
314-
end
315-
@benchmark nlsolve(prob; abstol=1e-8, reltol=1e-8, show_trace=Val(false))
316-
```
317-
318-
For small nonlinear systems, it could be faster to use the
319-
[`SimpleNewtonRaphson()` descent algorithm](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/code_optimization/).
320-
321-
```@setup main
322-
function nlsolve(prob, meth; kwargs...)
323-
try
324-
NonlinearSolve.solve(prob, meth; kwargs...)
325-
catch e
326-
println("Error using NonlinearSolve")
327-
println(e)
328-
end
329-
end
330-
@benchmark nlsolve(prob, SimpleNewtonRaphson(); abstol=1e-8, reltol=1e-8, show_trace=Val(false))
331-
```
332-
333289
### MINPACK.jl
334290

335-
Instead of the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package we can use the
291+
We can use [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package or, instead, the
336292
[MINPACK.jl](https://github.com/sglyon/MINPACK.jl) package to solve
337293
the shooting equation. To compute the Jacobian of the shooting function we use the
338294
[DifferentiationInterface.jl](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface) package with
@@ -370,13 +326,8 @@ nothing # hide
370326
```
371327

372328
We are now in position to solve the problem with the `hybrj` solver from MINPACK.jl through the `fsolve`
373-
function, providing the Jacobian. Let us do some benchmarking.
374-
375-
```@example main
376-
@benchmark fsolve(nle!, jnle!, ξ; show_trace=true) # initial guess given to the solver
377-
```
378-
379-
Now, let us solve the problem and retrieve the initial costate solution.
329+
function, providing the Jacobian.
330+
Let us solve the problem and retrieve the initial costate solution.
380331

381332
```@example main
382333
# resolution of S(ξ) = 0

docs/src/tutorial-iss.md

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Let us consider the following optimal control problem:
3030
with $t_0 = 0$, $t_f = 1$, $x_0 = -1$, $x_f = 0$, $\alpha=1.5$ and $\forall\, t \in [t_0, t_f]$, $x(t) \in \R$.
3131

3232
```@example main
33-
t0 = 0
34-
tf = 1
35-
x0 = -1
36-
xf = 0
37-
α = 1.5
33+
const t0 = 0
34+
const tf = 1
35+
const x0 = -1
36+
const xf = 0
37+
const α = 1.5
3838
ocp = @def begin
3939
4040
t ∈ [t0, tf], time
@@ -148,48 +148,13 @@ nothing # hide
148148
At the end, solving (BVP) is equivalent to solve $S(p_0) = 0$. This is what we call the **indirect simple shooting method**. We define an initial guess.
149149

150150
```@example main
151-
ξ = [ 0.1 ] # initial guess
151+
ξ = [0.1] # initial guess
152152
nothing # hide
153153
```
154154

155-
### NonlinearSolve.jl
156-
157-
We first use the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package to solve the shooting equation. Let us define the problem.
158-
159-
```@example main
160-
nle! = (s, ξ, λ) -> s[1] = S(ξ[1]) # auxiliary function
161-
prob = NonlinearProblem(nle!, ξ) # NLE problem with initial guess
162-
nothing # hide
163-
```
164-
165-
Let us do some benchmarking. This will be useful to compare the performance with the MINPACK.jl package below.
166-
167-
```@example main
168-
using SciMLSensitivity
169-
using BenchmarkTools
170-
@benchmark solve(prob, SimpleNewtonRaphson(); show_trace=Val(false))
171-
```
172-
173-
!!! note
174-
175-
For small nonlinear systems, the [`SimpleNewtonRaphson()` descent algorithm](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/code_optimization/) may be faster.
176-
177-
```@example main
178-
@benchmark solve(prob, SimpleNewtonRaphson(); show_trace=Val(false))
179-
```
180-
181-
Now, let us solve the problem and retrieve the initial costate solution.
182-
183-
```@example main
184-
indirect_sol = solve(prob; show_trace=Val(true)) # resolution of S(p0) = 0
185-
p0_sol = indirect_sol.u[1] # costate solution
186-
println("\ncostate: p0 = ", p0_sol)
187-
println("shoot: |S(p0)| = ", abs(S(p0_sol)), "\n")
188-
```
189-
190155
### MINPACK.jl
191156

192-
Instead of the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package we can use [MINPACK.jl](https://github.com/sglyon/MINPACK.jl) to solve the shooting equation. To compute the Jacobian of the shooting function we use [DifferentiationInterface.jl](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface) with [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) backend.
157+
We can use [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package or, instead, [MINPACK.jl](https://github.com/sglyon/MINPACK.jl) to solve the shooting equation. To compute the Jacobian of the shooting function we use [DifferentiationInterface.jl](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface) with [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) backend.
193158

194159
```@setup main
195160
using MINPACK
@@ -220,21 +185,8 @@ jnle! = (js, ξ) -> jacobian!(nle!, similar(ξ), js, backend, ξ) # Jacobian
220185
nothing # hide
221186
```
222187

223-
We are now in position to solve the problem with the `hybrj` solver from MINPACK.jl through the `fsolve` function, providing the Jacobian. Let us do some benchmarking to compare the performance with the NonlinearSolve.jl package above.
224-
225-
```@example main
226-
@benchmark fsolve(nle!, jnle!, ξ; show_trace=false) # initial guess given to the solver
227-
```
228-
229-
We can also use the [preparation step](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface/stable/tutorial1/#Preparing-for-multiple-gradients) of DifferentiationInterface.jl.
230-
231-
```@example main
232-
extras = prepare_jacobian(nle!, similar(ξ), backend, ξ)
233-
jnle_prepared!(js, ξ) = jacobian!(nle!, similar(ξ), js, backend, ξ, extras)
234-
@benchmark fsolve(nle!, jnle_prepared!, ξ; show_trace=false)
235-
```
236-
237-
Now, let us solve the problem and retrieve the initial costate solution.
188+
We are now in position to solve the problem with the `hybrj` solver from MINPACK.jl through the `fsolve` function, providing the Jacobian.
189+
Let us solve the problem and retrieve the initial costate solution.
238190

239191
```@example main
240192
indirect_sol = fsolve(nle!, jnle!, ξ; show_trace=true) # resolution of S(p0) = 0
@@ -400,5 +352,3 @@ nothing # hide
400352
```@example main
401353
pretty_plot(S, p0_sol; size=(800, 450))
402354
```
403-
404-

0 commit comments

Comments
 (0)