Skip to content

Commit fa3914e

Browse files
update component-based modeling tutorial
1 parent c26cde7 commit fa3914e

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

docs/src/tutorials/ode_modeling.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
# Component-Based Modeling with Ordinary Differential Equations
22

3+
## Copy-Paste Example
4+
5+
Here is the complete example, with explanation to follow:
6+
7+
```julia
8+
using ModelingToolkit, OrdinaryDiffEq
9+
10+
@parameters t σ ρ β
11+
@variables x(t) y(t) z(t)
12+
@derivatives D'~t
13+
14+
eqs = [D(x) ~ σ*(y-x),
15+
D(y) ~ x*-z)-y,
16+
D(z) ~ x*y - β*z]
17+
18+
lorenz1 = ODESystem(eqs,name=:lorenz1)
19+
lorenz2 = ODESystem(eqs,name=:lorenz2)
20+
21+
@variables a
22+
@parameters γ
23+
connections = [0 ~ lorenz1.x + lorenz2.y + a*γ]
24+
connected = ODESystem(connections,t,[a],[γ],systems=[lorenz1,lorenz2])
25+
26+
u0 = [lorenz1.x => 1.0,
27+
lorenz1.y => 0.0,
28+
lorenz1.z => 0.0,
29+
lorenz2.x => 0.0,
30+
lorenz2.y => 1.0,
31+
lorenz2.z => 0.0,
32+
a => 2.0]
33+
34+
p = [lorenz1.σ => 10.0,
35+
lorenz1.ρ => 28.0,
36+
lorenz1.β => 8/3,
37+
lorenz2.σ => 10.0,
38+
lorenz2.ρ => 28.0,
39+
lorenz2.β => 8/3,
40+
γ => 2.0]
41+
42+
tspan = (0.0,100.0)
43+
prob = ODEProblem(connected,u0,tspan,p)
44+
sol = solve(prob,Rodas5())
45+
46+
using Plots; plot(sol,vars=(a,lorenz1.x,lorenz2.z))
47+
```
48+
49+
## Generating ODESystems
50+
351
First let's build an ODE model. To do this we start by defining some
452
variables. In a differential equation system, we need to differentiate
553
between our (dependent) variables and parameters. Therefore, we label
@@ -71,10 +119,10 @@ we will define a new variable `α` which is defined by the interplay
71119
between these two models:
72120

73121
```julia
74-
@variables α(t)
122+
@variables a(t)
75123
@parameters γ
76-
connections = [0 ~ lorenz1.x + lorenz2.y + sin*γ)]
77-
connected = ODESystem(connections,t,[α],[γ],systems=[lorenz1,lorenz2])
124+
connections = [0 ~ lorenz1.x + lorenz2.y + a*γ]
125+
connected = ODESystem(connections,t,[a],[γ],systems=[lorenz1,lorenz2])
78126
```
79127

80128
This `ODESystem` thus connects the two Lorenz systems and defines the

0 commit comments

Comments
 (0)