Skip to content

Commit 6b61a6d

Browse files
fix optimization
1 parent 8995e3d commit 6b61a6d

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

docs/pages.jl

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pages = Any["index.md",
22
"getting_started.md",
3-
"Tutorials" => Any[
4-
"tutorials/faster_ode_example.md",
3+
"Tutorials" => Any["tutorials/faster_ode_example.md",
54
"tutorials/advanced_ode_example.md",
65
"tutorials/sde_example.md",
76
"tutorials/rode_example.md",
@@ -11,20 +10,14 @@ pages = Any["index.md",
1110
"tutorials/jump_diffusion.md",
1211
"tutorials/bvp_example.md",
1312
"tutorials/additional.md"],
14-
"Examples" => Any[
15-
"Beginner" => Any[
16-
"examples/classical_physics.md",
17-
"examples/conditional_dosing.md",
18-
"examples/kepler_problem.md",
19-
"examples/outer_solar_system.md",
20-
"examples/min_and_max.md",
21-
],
22-
"Advanced" => Any[
23-
"examples/spiking_neural_systems.md",
24-
"examples/beeler_reuter.md",
25-
"examples/diffusion_implicit_heat_equation.md",
26-
],
27-
],
13+
"Examples" => Any["Beginner" => Any["examples/classical_physics.md",
14+
"examples/conditional_dosing.md",
15+
"examples/kepler_problem.md",
16+
"examples/outer_solar_system.md",
17+
"examples/min_and_max.md"],
18+
"Advanced" => Any["examples/spiking_neural_systems.md",
19+
"examples/beeler_reuter.md",
20+
"examples/diffusion_implicit_heat_equation.md"]],
2821
"Basics" => Any["basics/overview.md",
2922
"basics/common_solver_opts.md",
3023
"basics/solution.md",

docs/src/examples/min_and_max.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ plot(sol, vars=(3,4), leg=false)
5151
Let's fine out what some of the local maxima and minima are. Optim.jl can be used to minimize functions, and the solution type has a continuous interpolation which can be used. Let's look for the local optima for the 4th variable around `t=20`. Thus our optimization function is:
5252

5353
```@example minmax
54-
f = (t) -> sol(t,idxs=4)
54+
f(t,_) = sol(first(t),idxs=4)
5555
```
5656

5757
`first(t)` is the same as `t[1]` which transforms the array of size 1 into a number. `idxs=4` is the same as `sol(first(t))[4]` but does the calculation without a temporary array and thus is faster. To find a local minima, we can solve the optimization problem where the loss
@@ -61,7 +61,7 @@ function is `f`:
6161
using Optimization, OptimizationNLopt, ForwardDiff
6262
optf = OptimizationFunction(f, Optimization.AutoForwardDiff())
6363
min_guess = 18.0
64-
optprob = OptimizationProblem(optf, min_guess)
64+
optprob = OptimizationProblem(optf, [min_guess])
6565
opt = solve(optprob, NLopt.LD_LBFGS())
6666
```
6767

@@ -75,9 +75,11 @@ println(opt.u)
7575
To get the maximum, we just minimize the negative of the function:
7676

7777
```@example minmax
78-
optf = OptimizationFunction(f, Optimization.AutoForwardDiff())
78+
fminus(t,_) = -sol(first(t),idxs=4)
79+
80+
optf = OptimizationFunction(fminus, Optimization.AutoForwardDiff())
7981
min_guess = 22.0
80-
optprob2 = OptimizationProblem(optf, min_guess)
82+
optprob2 = OptimizationProblem(optf, [min_guess])
8183
opt2 = solve(optprob2, NLopt.LD_LBFGS())
8284
```
8385

0 commit comments

Comments
 (0)