|
1 | 1 | # Component-Based Modeling with Ordinary Differential Equations |
2 | 2 |
|
| 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 | + |
3 | 51 | First let's build an ODE model. To do this we start by defining some |
4 | 52 | variables. In a differential equation system, we need to differentiate |
5 | 53 | 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 |
71 | 119 | between these two models: |
72 | 120 |
|
73 | 121 | ```julia |
74 | | -@variables α(t) |
| 122 | +@variables a(t) |
75 | 123 | @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]) |
78 | 126 | ``` |
79 | 127 |
|
80 | 128 | This `ODESystem` thus connects the two Lorenz systems and defines the |
|
0 commit comments