@@ -109,6 +109,36 @@ f = @eval eval(nlsys_func)
109
109
f2 = (du,u) -> f (du,u,(10.0 ,26.0 ,2.33 ))
110
110
```
111
111
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
+
112
142
## Core Principles
113
143
114
144
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:
145
175
146
176
For example, the following code defines an independent variable `t`, a parameter
147
177
`α`, 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
+
149
181
150
182
``` julia
151
183
t = Variable (:t ; known = true )() # independent variables are treated as known
@@ -155,19 +187,21 @@ w = Variable(:w; known = false) # unknown, left uncalled
155
187
x = Variable (:x ; known = false )(t) # unknown, depends on `t`
156
188
y = Variable (:y ; known = false )() # unknown, no dependents
157
189
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
158
192
159
- expr = x + y^ α + σ (3 ) * (z - t) - w (t - 1 )
193
+ expr = β₁ * x + y^ α + σ (3 ) * (z - t) - β₂ * w (t - 1 )
160
194
```
161
195
162
196
We can rewrite this more concisely using macros. Note the difference between
163
197
including and excluding empty parentheses. When in call format, variables are
164
198
aliased to the given call, allowing implicit use of dependents for convenience.
165
199
166
200
``` julia
167
- @parameters t α σ (.. )
201
+ @parameters t α σ (.. ) β[ 1 : 2 ]
168
202
@variables w (.. ) x (t) y () z (t, α, x)
169
203
170
- expr = x + y^ α + σ (3 ) * (z - t) - w (t - 1 )
204
+ expr = β[ 1 ] * x + y^ α + σ (3 ) * (z - t) - β[ 2 ] * w (t - 1 )
171
205
```
172
206
173
207
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
276
310
syntactic sugar in some form. For example, the variable construction:
277
311
278
312
``` julia
279
- @parameters t σ ρ β
313
+ @parameters t σ ρ β[ 1 : 3 ]
280
314
@variables x (t) y (t) z (t)
281
315
@derivatives D' ~ t
282
316
```
@@ -287,7 +321,7 @@ is syntactic sugar for:
287
321
t = Variable (:t ; known = true )()
288
322
σ = Variable (:σ ; known = true )
289
323
ρ = Variable (:ρ ; known = true )()
290
- β = Variable (:β ; known = true )()
324
+ β = [ Variable (:β , i ; known = true )() for i in 1 : 3 ]
291
325
x = Variable (:x )(t)
292
326
y = Variable (:y )(t)
293
327
z = Variable (:z )(t)
0 commit comments