Skip to content

Commit 7c71796

Browse files
committed
update README code
1 parent 8a313af commit 7c71796

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

README.md

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,35 @@ lower it to a first order system, symbolically generate the Jacobian function
3636
for the numerical integrator, and solve it.
3737

3838
```julia
39-
using OrdinaryDiffEqDefault, ModelingToolkit
39+
using ModelingToolkit
4040
using ModelingToolkit: t_nounits as t, D_nounits as D
4141

42+
# Defines a ModelingToolkit `System` model.
4243
@parameters σ ρ β
4344
@variables x(t) y(t) z(t)
44-
45-
eqs = [D(D(x)) ~ σ * (y - x),
45+
eqs = [
46+
D(D(x)) ~ σ * (y - x),
4647
D(y) ~ x *- z) - y,
47-
D(z) ~ x * y - β * z]
48-
48+
D(z) ~ x * y - β * z
49+
]
4950
@mtkcompile sys = System(eqs, t)
5051

51-
u0 = [D(x) => 2.0,
52+
# Simulate the model for a specific condition (initial condition and parameter values).
53+
using OrdinaryDiffEqDefault
54+
sim_cond = [
55+
D(x) => 2.0,
5256
x => 1.0,
5357
y => 0.0,
54-
z => 0.0]
55-
56-
p ==> 28.0,
58+
z => 0.0,
59+
σ => 28.0,
5760
ρ => 10.0,
58-
β => 8 / 3]
59-
60-
tspan = (0.0, 100.0)
61-
prob = ODEProblem(sys, u0, tspan, p, jac = true)
61+
β => 8 / 3
62+
]
63+
tend = 100.0
64+
prob = ODEProblem(sys, sim_cond, tend; jac = true)
6265
sol = solve(prob)
66+
67+
# Plot the solution in phase-space.
6368
using Plots
6469
plot(sol, idxs = (x, y))
6570
```
@@ -73,44 +78,46 @@ interacting Lorenz equations and simulate the resulting Differential-Algebraic
7378
Equation (DAE):
7479

7580
```julia
76-
using DifferentialEquations, ModelingToolkit
81+
using ModelingToolkit
7782
using ModelingToolkit: t_nounits as t, D_nounits as D
7883

79-
@parameters σ ρ β
80-
@variables x(t) y(t) z(t)
81-
82-
eqs = [D(x) ~ σ * (y - x),
84+
# Defines two lorenz system model.s
85+
eqs = [
86+
D(x) ~ σ * (y - x),
8387
D(y) ~ x *- z) - y,
84-
D(z) ~ x * y - β * z]
85-
88+
D(z) ~ x * y - β * z
89+
]
8690
@named lorenz1 = System(eqs, t)
8791
@named lorenz2 = System(eqs, t)
8892

93+
# Connect the two models, creating a single model.
8994
@variables a(t)
9095
@parameters γ
9196
connections = [0 ~ lorenz1.x + lorenz2.y + a * γ]
9297
@mtkcompile connected = System(connections, t, systems = [lorenz1, lorenz2])
9398

94-
u0 = [lorenz1.x => 1.0,
99+
# Simulate the model for a specific condition (initial condition and parameter values).
100+
using OrdinaryDiffEqDefault
101+
sim_cond = [
102+
lorenz1.x => 1.0,
95103
lorenz1.y => 0.0,
96104
lorenz1.z => 0.0,
97105
lorenz2.x => 0.0,
98-
lorenz2.y => 1.0,
99106
lorenz2.z => 0.0,
100-
a => 2.0]
101-
102-
p = [lorenz1.σ => 10.0,
107+
a => 2.0,
108+
lorenz1.σ => 10.0,
103109
lorenz1.ρ => 28.0,
104110
lorenz1.β => 8 / 3,
105111
lorenz2.σ => 10.0,
106112
lorenz2.ρ => 28.0,
107113
lorenz2.β => 8 / 3,
108-
γ => 2.0]
109-
110-
tspan = (0.0, 100.0)
111-
prob = ODEProblem(connected, u0, tspan, p)
114+
γ => 2.0
115+
]
116+
tend = 100.0
117+
prob = ODEProblem(connected, sim_cond, tend)
112118
sol = solve(prob)
113119

120+
# Plot the solution in phase-space.
114121
using Plots
115122
plot(sol, idxs = (a, lorenz1.x, lorenz2.z))
116123
```

0 commit comments

Comments
 (0)