Skip to content

Commit 8f67ec9

Browse files
format
1 parent dda4031 commit 8f67ec9

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

docs/src/tutorials/initialization.md

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ x' = f(x,y,t)\\
1919
```
2020

2121
where ``x`` are the differential variables and ``y`` are the algebraic variables.
22-
An initial condition ``u0 = [x(t_0) y(t_0)]`` is said to be consistent if
22+
An initial condition ``u0 = [x(t_0) y(t_0)]`` is said to be consistent if
2323
``g(x(t_0),y(t_0),t_0) = 0``.
2424

2525
For ODEs, this is trivially satisfied. However, for more complicated systems it may
@@ -64,13 +64,13 @@ This solves via:
6464

6565
```@example init
6666
sol = solve(prob, Rodas5P())
67-
plot(sol, idxs = (x,y))
67+
plot(sol, idxs = (x, y))
6868
```
6969

7070
and we can check it satisfies our conditions via:
7171

7272
```@example init
73-
conditions = getfield.(equations(pend)[3:end],:rhs)
73+
conditions = getfield.(equations(pend)[3:end], :rhs)
7474
```
7575

7676
```@example init
@@ -93,48 +93,50 @@ We can similarly choose `λ = 0` and solve for `y` to start the system:
9393
```@example init
9494
prob = ODEProblem(pend, [x => 1, λ => 0], (0.0, 1.5), [g => 1], guesses = [y => 1])
9595
sol = solve(prob, Rodas5P())
96-
plot(sol, idxs = (x,y))
96+
plot(sol, idxs = (x, y))
9797
```
9898

9999
or choose to satisfy derivative conditions:
100100

101101
```@example init
102-
prob = ODEProblem(pend, [x => 1, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1])
102+
prob = ODEProblem(
103+
pend, [x => 1, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1])
103104
sol = solve(prob, Rodas5P())
104-
plot(sol, idxs = (x,y))
105+
plot(sol, idxs = (x, y))
105106
```
106107

107-
Notice that since a derivative condition is given, we are required to give a
108+
Notice that since a derivative condition is given, we are required to give a
108109
guess for `y`.
109110

110111
We can also directly give equations to be satisfied at the initial point by using
111112
the `initialization_eqs` keyword argument, for example:
112113

113114
```@example init
114115
prob = ODEProblem(pend, [x => 1], (0.0, 1.5), [g => 1], guesses = [λ => 0, y => 1],
115-
initialization_eqs = [y ~ 1])
116+
initialization_eqs = [y ~ 1])
116117
sol = solve(prob, Rodas5P())
117-
plot(sol, idxs = (x,y))
118+
plot(sol, idxs = (x, y))
118119
```
119120

120121
Additionally, note that the initial conditions are allowed to be functions of other
121122
variables and parameters:
122123

123124
```@example init
124-
prob = ODEProblem(pend, [x => 1, D(y) => g], (0.0, 3.0), [g => 1], guesses = [λ => 0, y => 1])
125+
prob = ODEProblem(
126+
pend, [x => 1, D(y) => g], (0.0, 3.0), [g => 1], guesses = [λ => 0, y => 1])
125127
sol = solve(prob, Rodas5P())
126-
plot(sol, idxs = (x,y))
128+
plot(sol, idxs = (x, y))
127129
```
128130

129131
## Determinability: Underdetermined and Overdetermined Systems
130132

131133
For this system we have 3 conditions to satisfy:
132134

133135
```@example init
134-
conditions = getfield.(equations(pend)[3:end],:rhs)
136+
conditions = getfield.(equations(pend)[3:end], :rhs)
135137
```
136138

137-
when we initialize with
139+
when we initialize with
138140

139141
```@example init
140142
prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 1.5), [g => 1], guesses = [y => 0, λ => 1])
@@ -155,21 +157,22 @@ and thus the solution is not necessarily unique. It can still be solved:
155157

156158
```@example init
157159
sol = solve(prob, Rodas5P())
158-
plot(sol, idxs = (x,y))
160+
plot(sol, idxs = (x, y))
159161
```
160162

161163
and the found initial condition satisfies all constraints which were given. In the opposite
162164
direction, we may have an overdetermined system:
163165

164166
```@example init
165-
prob = ODEProblem(pend, [x => 1, y => 0.0, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1])
167+
prob = ODEProblem(
168+
pend, [x => 1, y => 0.0, D(y) => 0], (0.0, 1.5), [g => 1], guesses = [λ => 1])
166169
```
167170

168171
Can that be solved?
169172

170173
```@example init
171174
sol = solve(prob, Rodas5P())
172-
plot(sol, idxs = (x,y))
175+
plot(sol, idxs = (x, y))
173176
```
174177

175178
Indeed since we saw `D(y) = 0` at the initial point above, it turns out that this solution
@@ -178,7 +181,8 @@ aren't that lucky. If the set of initial conditions cannot be satisfied, then yo
178181
a `SciMLBase.ReturnCode.InitialFailure`:
179182

180183
```@example init
181-
prob = ODEProblem(pend, [x => 1, y => 0.0, D(y) => 2.0, λ => 1], (0.0, 1.5), [g => 1], guesses = [λ => 1])
184+
prob = ODEProblem(
185+
pend, [x => 1, y => 0.0, D(y) => 2.0, λ => 1], (0.0, 1.5), [g => 1], guesses = [λ => 1])
182186
sol = solve(prob, Rodas5P())
183187
```
184188

@@ -216,18 +220,19 @@ with observables, those observables are too treated as initial equations. We can
216220
resulting simplified system via the command:
217221

218222
```@example init
219-
isys = structural_simplify(isys; fully_determined=false)
223+
isys = structural_simplify(isys; fully_determined = false)
220224
```
221225

222226
Note `fully_determined=false` allows for the simplification to occur when the number of equations
223227
does not match the number of unknowns, which we can use to investigate our overdetermined system:
224228

225229
```@example init
226-
isys = ModelingToolkit.generate_initializesystem(pend, u0map = [x => 1, y => 0.0, D(y) => 2.0, λ => 1], guesses = [λ => 1])
230+
isys = ModelingToolkit.generate_initializesystem(
231+
pend, u0map = [x => 1, y => 0.0, D(y) => 2.0, λ => 1], guesses = [λ => 1])
227232
```
228233

229234
```@example init
230-
isys = structural_simplify(isys; fully_determined=false)
235+
isys = structural_simplify(isys; fully_determined = false)
231236
```
232237

233238
```@example init
@@ -252,8 +257,8 @@ constructor which acts just like an `ODEProblem` or `NonlinearProblem` construct
252257
creates the special initialization system for a given `sys`. This is done as follows:
253258

254259
```@example init
255-
iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
256-
[x => 1, y => 0.0, D(y) => 2.0, λ => 1], [g => 1], guesses = [λ => 1])
260+
iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
261+
[x => 1, y => 0.0, D(y) => 2.0, λ => 1], [g => 1], guesses = [λ => 1])
257262
```
258263

259264
We can see that because the system is overdetermined we receive a NonlinearLeastSquaresProblem,
@@ -266,6 +271,7 @@ sol = solve(iprob)
266271
```
267272

268273
!!! note
274+
269275
For more information on solving NonlinearProblems and NonlinearLeastSquaresProblems,
270276
check out the [NonlinearSolve.jl tutorials!](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/getting_started/).
271277

@@ -293,8 +299,8 @@ to see the problem is not equation 2 but other equations in the system. Meanwhil
293299
some of the conditions:
294300

295301
```@example init
296-
iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
297-
[x => 1, y => 0.0, D(y) => 0.0, λ => 0], [g => 1], guesses = [λ => 1])
302+
iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
303+
[x => 1, y => 0.0, D(y) => 0.0, λ => 0], [g => 1], guesses = [λ => 1])
298304
```
299305

300306
gives a NonlinearLeastSquaresProblem which can be solved:
@@ -309,10 +315,9 @@ sol.resid
309315

310316
In comparison, if we have a well-conditioned system:
311317

312-
313318
```@example init
314-
iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
315-
[x => 1, y => 0.0], [g => 1], guesses = [λ => 1])
319+
iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
320+
[x => 1, y => 0.0], [g => 1], guesses = [λ => 1])
316321
```
317322

318323
notice that we instead obtained a NonlinearSystem. In this case we have to use
@@ -348,7 +353,7 @@ by initializing the derivatives to zero:
348353

349354
```@example init
350355
prob = ODEProblem(simpsys, [D(x) => 0.0, D(y) => 0.0], tspan, guesses = [x => 1, y => 1])
351-
sol = solve(prob, Tsit5(), abstol= 1e-16)
356+
sol = solve(prob, Tsit5(), abstol = 1e-16)
352357
```
353358

354359
Notice that this is a "numerical zero", not an exact zero, and thus the solution will leave the
@@ -370,4 +375,4 @@ sol[α * x - β * x * y]
370375

371376
```@example init
372377
plot(sol)
373-
```
378+
```

0 commit comments

Comments
 (0)