Skip to content

Commit 22a08de

Browse files
better README examples
1 parent 78aa2c5 commit 22a08de

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

README.md

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,82 @@ For information on using the package,
2121
[in-development documentation](https://mtk.sciml.ai/dev/) for the version of
2222
the documentation which contains the un-released features.
2323

24-
## High Level Example
24+
## High Level Examples
2525

26-
Let's define the Lorenz equations for numerically solving with DifferentialEquations.jl,
27-
but tell the symbolic system to automatically generate code for efficiently
28-
handling the sparse Jacobian.
26+
First let's define a second order riff on the Lorenz equations, symbolically
27+
lower it to a first order system, symbolically generate the Jacobian function
28+
for the numerical integrator, and solve it.
2929

3030
```julia
31-
using ModelingToolkit
31+
using ModelingToolkit, OrdinaryDiffEq
3232

3333
@parameters t σ ρ β
3434
@variables x(t) y(t) z(t)
3535
@derivatives D'~t
3636

37-
eqs = [D(x) ~ σ*(y-x),
37+
eqs = [D(D(x)) ~ σ*(y-x),
3838
D(y) ~ x*-z)-y,
3939
D(z) ~ x*y - β*z]
4040

4141
sys = ODESystem(eqs)
42+
sys = ode_order_lowering(sys)
4243

43-
u0 = [x => 1.0,
44+
u0 = [D(x) => 2.0,
45+
x => 1.0,
4446
y => 0.0,
4547
z => 0.0]
4648

4749
p ==> 28.0,
4850
ρ => 10.0,
4951
β => 8/3]
5052

51-
tspan = (0.0,100.0)
52-
prob = ODEProblem(sys,u0,tspan,p,jac=true,sparse=true)
53+
tspan = (0.0,100.0)
54+
prob = ODEProblem(sys,u0,tspan,p,jac=true)
55+
sol = solve(prob,Tsit5())
5356
```
5457

55-
This automatically will have generated fast sparse Jacobian functions, making
58+
![Lorenz2](https://user-images.githubusercontent.com/1814174/79118645-744eb580-7d5c-11ea-9c37-13c4efd585ca.png)
59+
60+
This automatically will have generated fast Jacobian functions, making
5661
it more optimized than directly building a function. In addition, we can then
57-
use ModelingToolkit to compose multiple ODE subsystems. Let's define two
58-
interacting Lorenz equations:
62+
use ModelingToolkit to compose multiple ODE subsystems. Now let's define two
63+
interacting Lorenz equations and simulate the resulting Differential-Algebriac
64+
Equation (DAE):
5965

6066
```julia
67+
@parameters t σ ρ β
68+
@variables x(t) y(t) z(t)
69+
@derivatives D'~t
70+
71+
eqs = [D(x) ~ σ*(y-x),
72+
D(y) ~ x*-z)-y,
73+
D(z) ~ x*y - β*z]
74+
6175
lorenz1 = ODESystem(eqs,name=:lorenz1)
6276
lorenz2 = ODESystem(eqs,name=:lorenz2)
6377

6478
@variables α
6579
@parameters γ
6680
connections = [0 ~ lorenz1.x + lorenz2.y + sin*γ)]
67-
connected = ODESystem(connections,[α],[γ],systems=[lorenz1,lorenz2])
81+
connected = ODESystem(connections,t,[α],[γ],systems=[lorenz1,lorenz2])
82+
83+
u0 = [lorenz1.x => 1.0,
84+
lorenz1.y => 0.0,
85+
lorenz1.z => 0.0,
86+
lorenz2.x => 1.0,
87+
lorenz2.y => 0.0,
88+
lorenz2.z => 0.0,
89+
α => 2.0]
90+
91+
p = [lorenz1.σ => 28.0,
92+
lorenz1.ρ => 10.0,
93+
lorenz1.β => 8/3,
94+
lorenz2.σ => 28.0,
95+
lorenz2.ρ => 10.0,
96+
lorenz2.β => 8/3,
97+
γ => 2.0]
98+
99+
tspan = (0.0,100.0)
100+
prob = ODEProblem(connected,u0,tspan,p)
101+
sol = solve(prob,Rodas5())
68102
```
69-
70-
which is now a differential-algebraic equation (DAE) of 7 variables which has
71-
two independent Lorenz systems and an algebraic equation that determines `α`
72-
such that an implicit constraint holds. We can then define the resulting
73-
`ODEProblem` and send it over to DifferentialEquations.jl.

0 commit comments

Comments
 (0)