Skip to content

Commit 769e724

Browse files
committed
Add documentation for array of variables
1 parent 9f843c8 commit 769e724

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@ f = @eval eval(nlsys_func)
109109
f2 = (du,u) -> f(du,u,(10.0,26.0,2.33))
110110
```
111111
112+
### Example: Arrays of variables
113+
114+
Sometimes it is convenient to define arrays of variables to model things like `x₁,…,x₃`.
115+
The `@variables` and `@parameters` macros support this with the following syntax:
116+
117+
```julia
118+
julia> @variables x[1:3];
119+
julia> x
120+
3-element Array{Operation,1}:
121+
x[1]()
122+
x[2]()
123+
x[3]()
124+
125+
# support for arbitrary ranges and tensors
126+
julia> @variables y[2:3,1:5:6];
127+
julia> y
128+
2×2 Array{Operation,2}:
129+
y[2,1]() y[2,6]()
130+
y[3,1]() y[3,6]()
131+
132+
133+
# also works for dependent variables
134+
julia> @parameters t; @variables z[1:3](t);
135+
julia> z
136+
3-element Array{Operation,1}:
137+
z[1](t())
138+
z[2](t())
139+
z[3](t())
140+
```
141+
112142
## Core Principles
113143
114144
The core idea behind ModelingToolkit.jl is that mathematical equations require
@@ -145,7 +175,9 @@ context-aware single variable of the IR. Its fields are described as follows:
145175
146176
For example, the following code defines an independent variable `t`, a parameter
147177
`α`, a function parameter `σ`, a variable `x` which depends on `t`, a variable
148-
`y` with no dependents, and a variable `z` which depends on `t`, `α`, and `x(t)`.
178+
`y` with no dependents, a variable `z` which depends on `t`, `α`, and `x(t)`
179+
and a parameters `β₁` and `β₂`.
180+
149181
150182
```julia
151183
t = Variable(:t; known = true)() # independent variables are treated as known
@@ -155,19 +187,21 @@ w = Variable(:w; known = false) # unknown, left uncalled
155187
x = Variable(:x; known = false)(t) # unknown, depends on `t`
156188
y = Variable(:y; known = false)() # unknown, no dependents
157189
z = Variable(:z; known = false)(t, α, x) # unknown, multiple arguments
190+
β₁ = Variable(, 1; known = true)() # with index 1
191+
β₂ = Variable(, 2; known = true)() # with index 2
158192

159-
expr = x + y^α + σ(3) * (z - t) - w(t - 1)
193+
expr = β₁ * x + y^α + σ(3) * (z - t) - β₂ * w(t - 1)
160194
```
161195
162196
We can rewrite this more concisely using macros. Note the difference between
163197
including and excluding empty parentheses. When in call format, variables are
164198
aliased to the given call, allowing implicit use of dependents for convenience.
165199
166200
```julia
167-
@parameters t α σ(..)
201+
@parameters t α σ(..) β[1:2]
168202
@variables w(..) x(t) y() z(t, α, x)
169203

170-
expr = x + y^α + σ(3) * (z - t) - w(t - 1)
204+
expr = β[1] * x + y^α + σ(3) * (z - t) - β[2] * w(t - 1)
171205
```
172206
173207
Note that `@parameters` and `@variables` implicitly add `()` to values that
@@ -276,7 +310,7 @@ is accessible via a function-based interface. This means that all macros are
276310
syntactic sugar in some form. For example, the variable construction:
277311
278312
```julia
279-
@parameters t σ ρ β
313+
@parameters t σ ρ β[1:3]
280314
@variables x(t) y(t) z(t)
281315
@derivatives D'~t
282316
```
@@ -287,7 +321,7 @@ is syntactic sugar for:
287321
t = Variable(:t; known = true)()
288322
σ = Variable(; known = true)
289323
ρ = Variable(; known = true)()
290-
β = Variable(; known = true)()
324+
β = [Variable(, i; known = true)() for i in 1:3]
291325
x = Variable(:x)(t)
292326
y = Variable(:y)(t)
293327
z = Variable(:z)(t)

0 commit comments

Comments
 (0)