Skip to content

Commit 02ac87f

Browse files
authored
Merge pull request #1573 from SciML/myb/spring
Fix spring_mass tutorial
2 parents 32f9aef + 332dd98 commit 02ac87f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

docs/src/tutorials/spring_mass.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ eqs = [
4747
scalarize(D.(mass.v) .~ spring_force(spring) / mass.m .+ g)
4848
]
4949

50-
@named _model = ODESystem(eqs, t)
50+
@named _model = ODESystem(eqs, t, [spring.x; spring.dir; mass.pos], [])
5151
@named model = compose(_model, mass, spring)
5252
sys = structural_simplify(model)
5353

@@ -135,7 +135,7 @@ eqs = [
135135
Finally, we can build the model using these equations and components.
136136

137137
```julia
138-
@named _model = ODESystem(eqs, t)
138+
@named _model = ODESystem(eqs, t, [spring.x; spring.dir; mass.pos], [])
139139
@named model = compose(_model, mass, spring)
140140
```
141141

@@ -192,13 +192,26 @@ sys = structural_simplify(model)
192192
equations(sys)
193193

194194
4-element Vector{Equation}:
195-
Differential(t)(mass₊v[1](t)) ~ -spring₊k*mass₊pos[1](t)*(mass₊m^-1)*(sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t))) - spring₊l)*(sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t)))^-1)
196-
Differential(t)(mass₊v[2](t)) ~ -9.81 - (spring₊k*mass₊pos[2](t)*(mass₊m^-1)*(sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t))) - spring₊l)*(sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t)))^-1))
195+
Differential(t)(mass₊v[1](t)) ~ (-spring₊k*(spring₊x(t) - spring₊l)*mass₊pos[1](t)) / (mass₊m*spring₊x(t))
196+
Differential(t)(mass₊v[2](t)) ~ (-spring₊k*(spring₊x(t) - spring₊l)*mass₊pos[2](t)) / (mass₊m*spring₊x(t)) - 9.81
197197
Differential(t)(mass₊pos[1](t)) ~ mass₊v[1](t)
198198
Differential(t)(mass₊pos[2](t)) ~ mass₊v[2](t)
199199
```
200200

201-
We are left with only 4 equations involving 4 state variables (`mass.pos[1]`, `mass.pos[2]`, `mass.v[1]`, `mass.v[2]`). We can solve the system by converting it to an `ODEProblem` in mass matrix form and solving with an [`ODEProblem` mass matrix solver](https://diffeq.sciml.ai/stable/solvers/dae_solve/#OrdinaryDiffEq.jl-(Mass-Matrix)). This is done as follows:
201+
We are left with only 4 equations involving 4 state variables (`mass.pos[1]`,
202+
`mass.pos[2]`, `mass.v[1]`, `mass.v[2]`). We can solve the system by converting
203+
it to an `ODEProblem`. Some observed variables are not expanded by default. To
204+
view the complete equations, one can do
205+
```julia
206+
julia> full_equations(sys)
207+
4-element Vector{Equation}:
208+
Differential(t)(mass₊v[1](t)) ~ (-spring₊k*(sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t))) - spring₊l)*mass₊pos[1](t)) / (mass₊m*sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t))))
209+
Differential(t)(mass₊v[2](t)) ~ (-spring₊k*(sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t))) - spring₊l)*mass₊pos[2](t)) / (mass₊m*sqrt(abs2(mass₊pos[1](t)) + abs2(mass₊pos[2](t)))) - 9.81
210+
Differential(t)(mass₊pos[1](t)) ~ mass₊v[1](t)
211+
Differential(t)(mass₊pos[2](t)) ~ mass₊v[2](t)
212+
```
213+
214+
This is done as follows:
202215

203216
```julia
204217
prob = ODEProblem(sys, [], (0., 3.))

0 commit comments

Comments
 (0)