# Use of Functions II – Applications Using Indices

Stefan Kuntsche, Sanam Sabeti Adibpour

This tutorial introduces the use of indices in function appliances. It is recommended that you work through the previous tutorial first.

Equation system
Equations
(1) $x_{i} = a_{i} + b_{i}$
(2) $s_{i} = y_{i}$

Function
(3) $y_{i} = 0.5 \cdot (a_{i}+b_{i})$

Notation ‘nota x’

Base Names:

Name Description
$a$ parameter a
$A$ parameter A
$b$ parameter b
$B$ parameter B
$s$ additional value s   (new)
$x$ value x
$y$ value y

Indices:

Name Description
$i$ index of $x$  1..N  (new)

Problem description

Maximum value:
$N = 2$

Design variables and the corresponding values
$a_{i=1} = 1.0$
$a_{i=2} = 1.5$
$b_{i=1} = 3.0$
$b_{i=2} = 3.5$

Iteration variables and initialization values
$s_{i=1}=0.0$
$s_{i=2}=0.0$
$x_{i=1}=0.0$
$x_{i=2}=0.0$

Expected results
$s_{i=1}=2.0$
$s_{i=2}=2.25$
$x_{i=1}=4.0$
$x_{i=2}=5.0$

The following general function will be used:

Function ‘mean value’
(4) $m(o,p) = 0.5 \cdot (o+p)$

Notation ‘nota mean value’

Base Names:

Name Description
$m$ mean value
$o$ value one
$p$ value two

Creating the equations The equations are created as usual (see section Basics). The basic work flow for this is:

• Create and store the notation as it is given above for the equations. If you have just done the tutorial part Use of Functions I you may extend the equation you created there and use it here. Otherwise, create a new notation and enter the symbols specified below ‘nota_x’ into it.
• Create and store the equations (1) and (2) using the above notation.

Creating the function First the general function (4) must be created. This general function will be used to reflect relation (3). If you have done the tutorial part Use of Functions I you may use the function created there. Otherwise follow the instructions given there to create the function.

Creating the equation system For detailed help see section Basics. The basic work flow is:

• Add the notation, here you should use the same notation as you did for the equations (‘nota x’).
• Add the equations created in the previous steps.

Adding and applying the function To use the general function created above, it must be added to the equation system and applied to the variables concerned. In this context, applying means specifying which variable names in the equation system are supposed to be the corresponding input and output variables. In this example the applied variables contain indices. MOSAICmodeling can deal with indexed expressions and during the evaluation of the equation system the function is applied for every instance of the index.

• Open the Equation System in the Model Bar.
• Activate the tab Functions and press [Add Function]
• Take a look at the new dialog window named Add Function Usage.
• In this dialog press [Select] on the upper right hand side and choose the function you created and stored during the previous steps. The field Preview should now display the function (4).
• On the left hand side of the dialog you see the Output Naming and the Input Namings as they are defined in the function. The latter are will be linked to the desired variable names as they appear in the equation system, the so called applied namings.
• On the right hand side of the dialog there is a section called Applications (Function Calls) that contains the tables Output Variable, Input Variables, and Parameters.
• Press [Add Application] to create a new function application.
• Take a look at the new dialog window called Edit Function Appliance
• In this dialog double click the field Applied Naming or click [Edit Output] below that field. This opens another dialog where you can enter the naming of the output variable in the equation system.
• Enter y_{i} into the Tex Expression area and press [Render] and then [OK].
• Double click into the table row of the variable naming o or select this row and click [Edit Input]. Again a dialog is shown where a variable naming can be entered.
• Enter a_{i} into the Tex Expression area and press [Render] and then [OK].
• In the same way specify b_{i} as the applied naming for p.
• Press [OK] in the Edit Function Appliance dialog.
• In the Edit Function Usage dialog the new function appliance should now be shown in the tables in the Applications section.
• Press [OK] in the Edit Function Usage dialog.
• Save the equation system.

Evaluating the equation system

• Go to the Simulation Bar.
• Select the tab Equation System and load the equation system created in the previous steps.
• In the tab Indexing specify the index maximum value for i as 2 (by entering it into the cell Max Val).
• Activate the tab Instantiated System. You will see four equations in the sub tab Equations, two of which contain instances of $y_{i}$. Although it is not explicitly visible here, the function has been applied according to the indices to each set of $y_{i}$, $a_{i}$ and $b_{i}$.
• Go to the sub tab Functions and select the function on the left hand side to display the function applications on the right hand side. As you can see, the function calculates the value for two different variables ($y_{i=1}$, $y_{i=2}$) based on the one application for $y_{i}$, that has been specified in the equation system editor.
• Select the tab Specification and then Variable Specification. You will find that the variables named $y_{i=1}$ and $y_{i=2}$ have been automatically classified as calculated variables.
• Manually classify the variables $a_{i=1}$, $a_{i=2}$, $b_{i=1}$ and $b_{i=2}$ as design variables by moving them into the corresponding table and give them the values specified in the problem description indicated in the beginning of this section.
• Save the variable specification list
• Enter a description
• Save the evaluation/simulation
• Open the tab Evaluation, click [Generate Code] and then [Evaluate].
• Have a look at the output of the solver given in the tab Results.