This tutorial explains how to use functions in MOSAICmodeling. Functions should be used whenever it is possible to calculate the value of a variable directly by an algebraic expression based on the values of other variables. The explanatory example will solve the following equation system:

Equation system ‘eq sys func simple’

Equations

$latex x+b\cdot y = A$ (1)

$latex B\cdot (y)^{2} = – a + x$ (2)

Function

$latex A=f(a,B)=0.5\cdot(a+B)$ (3)

Notation ‘nota x’

Base Names:

[table sort=”desc,asc”]

Name, Description

$latex a$,parameter a

$latex A$,parameter A

$latex b$,parameter b

$latex B$,parameter B

$latex x$,value x

$latex y$,value y

[/table]

Problem description

Design variables and the corresponding values

$latex a = 2.0$

$latex b = 1.0$

$latex B = -4.0$

Iteration variables initialization and expected results

[table]

,Case one, Case two

Initialization,$latex x=0.0$,$latex x=-0.5$

,$latex y=0.0$,$latex y=0.5$

Solution,$latex x=-0.25$,$latex x=-2$

,$latex y=-0.75$,$latex y=1$

[/table]

In the example given above there are two equations together with a function. Instead of introducing another equation to reflect (Eq. 1), we will create a general MOSAICmodeling function and *apply* it once to the equation system:

Function ‘mean value’

$latex m(o,p) = 0.5 \cdot (o+p)$

Notation ‘nota mean value’

Base Names:

[table sort=”desc,asc”]

Name, Description

$latex m$,mean value

$latex o$,value one

$latex p$,value two

[/table]

**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.

Enter the symbols specified below ‘nota x’ into this notation.

- Create and store the equations (1) and (2) using the new notation.

**Creating the function** First the general function (3) will be created. The following steps have to be done:

- Create and store the notation for the function. This notation must contain all symbols specified above ‘nota mean value’.
- Select the tab
in the Model bar.**Function** - Load the notation ‘nota mean value’ into the field named
.**Notation** - Write some explanatory text into the
field.**Description** - In the tab
check that**Interface & Body Settings**and`Free Interface`are selected in the comboboxes for the interface respectively body setting.`Specify formula` - Activate the tab
.**Interface Specification** - Click
or double click into the white area next to the button. A dialog window pops up where you can enter a variable naming for the output variable.**[Edit Output]** - Type
*m*into the field, press the**Tex Expression**button**render**, and then**(Shortcut: Shift+Enter)**.**[OK] (Shortcut: Ctrl+Enter)** - Press
and specify the name**[Add Input]***o*the same way as in the previous step. - Repeat the last step to add the output name
*p*. - Activate the tab
.**Body Specification** - Enter the right hand side of (3) into the
field and press the render button. The string to be entered should be similar to this:**Mosaic Tex**

0.5 \cdot (o+p)

- Save the function as ‘fun_meanValue’ to make it available in equation systems.

**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.
- Enter a description and save the Equation System (‘eqs_funcSimple’).

**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.

- Select the Equation System Editor in the Model Bar and open the Equation System ‘eqs_funcSimple’.
- Activate the tab
and press**Functions****[Add Function].** - Take a look at the new dialog window named
.**Add Function Usage** - In this dialog press
on the upper right hand side and choose the function ‘fun_meanValue’ you created and stored previously. The field**[Select]**should now display the function (3).**Preview** - On the left hand side of the dialog you see the
and the**Output Naming**as they are defined in the function. The latter will be linked to the desired variable names as they appear in the equation system, the so called**Input Namings***applied namings*. - On the right hand side of the dialog there is a section called
that contains the tables**Applications (Function Calls)**,**Output Variable**, and**Input Variables**.**Parameters** - Press
within the**[Add Application]**section to create a new function appliance.**Applications** - Take a look at the new dialog window called
**Edit Function Application** - In this dialog double click into the field
or click**Applied Naming**below that field. This opens another dialog where you can enter the naming of the output variable in the equation system.**[Edit Output]** - Enter
*A*into thearea, press the**Tex Expression**buttton**render**and then**(Shorcut: Shift+Enter)**.**[OK]****(Shortcut: Ctrl+Enter)** - Double click into the table row of the variable naming
*o*or select this row and click. Again a dialog is shown where a variable naming can be entered.**[Edit Input]** - Enter
*a*similar to the previous step - In the same way specify
*B*as the applied naming for*p*. - Press
in the**[OK]**dialog.**Edit Function Appliance** - In the
dialog the new applied Naming ‘A’ should be visible**Edit Function Usage** - Click on ‘A’ to show the input applications (o->a, p->B) in the tables in the
section.**Applications** - Press
in the**[OK]**dialog.**Edit Function Usage** - Save the equation system.

**Evaluating the equation system**

- Activate the
tab.**Simulation** - Select the tab
and load the equation system ‘eqs_funcSimple’ created in the previous steps.**Equation System** - Select the tab
. You will find that the variable named**Specifications***A*has been automatically classified as*calculated variable*. - Manually classify the variables
*a*,*b*, and*B*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. - Enter a description and save the simulation ‘eva_funcSimple’.
- Open the tab
, click**Evaluation**and then**[Generate Code]**.**[Evaluate]** - Have a look at the output of the solver given in the tab
.**Results**