@@ -109,6 +109,36 @@ f = @eval eval(nlsys_func)
109109f2 = (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
114144The 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
146176For 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
151183t = Variable (:t ; known = true )() # independent variables are treated as known
@@ -155,19 +187,21 @@ w = Variable(:w; known = false) # unknown, left uncalled
155187x = Variable (:x ; known = false )(t) # unknown, depends on `t`
156188y = Variable (:y ; known = false )() # unknown, no dependents
157189z = 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
162196We can rewrite this more concisely using macros. Note the difference between
163197including and excluding empty parentheses. When in call format, variables are
164198aliased 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
173207Note 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
276310syntactic 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:
287321t = 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 ]
291325x = Variable (:x )(t)
292326y = Variable (:y )(t)
293327z = Variable (:z )(t)
0 commit comments