Use of Functions II – Applications Using Indices

 

This tutorial introduces the use of indices in function applications.

Model description

The following example will solve the following equation system:

   x_i = a_i + b_i \\ s_i = y_i

As in the previous example, a function shall be used to calculate y_i. This function has the same formula as before:

 y_i = f(a_i, b_i) = 0.5 \cdot (a_i+b_i).

Workflow

Notation of equation system

For the notation of the equation, we need to set up all base names of symbols that appear in the equations, i.e.

  • a, parameter
  • A, parameter
  • b, parameter
  • B, parameter
  • s, additional value s (new)
  • x, iteration variable
  • y, iteration variable

As indices are now involved, we also need to define the index i:

  • i, index of x  1..N (new)

The resulting notation has ID 182662.

Equations

Next, we can define the equations of the model. Therefore, we open the equation editor, load the notation we just created, and enter the equations as stated above in the model description. The formulated equations are available with the IDs 182663 and 182664.

Notation and definition of function

Because we use exactly the same function, we do not have to define and save a notation and the function itself again, but we will simply re-use this function and apply it on our new variables. The function is available with ID 182656.

Equation system

We are now able to construct our new equation system based on the new equations and the function saved in the previous example:

  1. Go to “Equation System” and load the notation for the equations
  2. Add the two equations to your system in the Connected Elements tab
  3. Go to the Functions tab. To use the general function created above, it must be added to the equation system and applied on the variables within this problem. In this context, applying means specifying which variable names in the equation system are supposed to be the corresponding input and output variables of the function.
  4. Click on “Add Function” at the bottom of the screen and open your function in the opening popup window. After loading the function, you should see the expression for m as a function of the inputs o and p. On the left hand side of the dialog, you see the Output Naming and the Input Naming as defined in the function. 
  5. On the right hand side of the dialog there is a section called Applications (Function Calls), which contains the tables Output Variable, Input Variables, and Parameters. Click on “Add Application”, which opens another popup window.
  6. In this dialog, double click into the field Applied Naming or click on “Edit Output” below that field. This opens another dialog where you can enter the naming of the output variable in the equation system.
  7. Enter y_i into the Tex Expression, render, and confirm
  8. Double click into the table row of the variable naming o or select this row and click “Edit Input”. Again a dialog is shown in which a variable naming can be entered. Enter a_i, render, and confirm; then repeat this step for p with b_i being the applied input
  9. Confirm these matches at the bottom of the window. When clicking on ‘A’ in the Applications, you should now find the match of the inputs (o \rightarrow a_i, p \rightarrow b_i)
  10. Confirm in this window at the bottom and save your equation system

This equation system is available with ID 182665. 

Evaluation / Simulation

Your model is now complete and you can move on to the “Simulation” section of MOSAICmodeling.

  1. Enter a suitable description
  2. Select the tab Equation System and load the system created in the previous steps. Go to Indexing and set N to 2
  3. If you now go to the tab Instantiated System and then to the Functions tab, you will notice that you still have one application but two usages, because the function is applied on all instances of i, i.e., for i = 1 and i = 2
  4. Select the tab Specifications. In the list ALL VARIABLES, you will find the variables as defined in your model notation. Therein, y_i will be of type “CALCULATED_VALUE” because it is determined by a function
  5. Classify the variables a_i and b_i as design variables by selecting this category from the dropdown menu in the Type column. You can also assign the variables x_i and s_i as “ITERATION_VALUE”

Initialization and results

This example is solved with the initial guesses for the variables x_i and s_i given in Table 1. After having assigned the initial guesses and the parameter values, you can save the variable specification and then the simulation. This simulation is available with ID 182666 with variable specification 182667. The solution for all iteration variables is also given in Table 1.

NameDescriptionValue / Initial guessSolution
a_{i=1}Parameter1.0
a_{i=2}Parameter1.5
b_{i=1}Parameter3.0
b_{i=2}Parameter3.5
y_{i=1}Explicitly calculable value2.0
y_{i=2}Explicitly calculable value2.5
s_{i=1}Iteration variable0.02.0
s_{i=2}Iteration variable0.02.5
x_{i=1}Iteration variable0.04.0
x_{i=2}Iteration variable0.05.0
Table 1: Overview of parameter values, initial guesses, and the solution.