Repetitive equation structures can be expressed rather compactly with the special syntactic form of equation called a for-equation. Such a structured equation can be expanded to a sometimes large number of simple equations. The iteration-variable ranges over the set of values in the iteration-set-expression which must be a vector expression with parameter or constant variability. This expression is evaluated once in each for-equation, and is evaluated in the scope immediately enclosing the for-equation. Such a scope can typically be the scope of an enclosing class definition or the scope of an enclosing for-equation.
Here the simulation of FiveEquations is shown.
FiveEquations has the same behavior as the class FiveEquationsUnrolled where the for-equation has been unrolled into five simple equations.
Here the simulation of FiveEquationsUnrolled is shown.
Since the scope of the iteration variable is the enclosing for-equation, it may hide other variables with the same name, e.g. the constant k declared in the class HideVariable. Note that the k used in the iteration expression 1:k+1 is the constant k declared in the class, whereas the k used in the equations inside the for-clause is the iteration variable k. You should avoid this style of modeling, even if it is legal, since it is confusing and hard to read such code.
Here we simulate HideVariable.