Use of Functions IV – Definition by Interface and Empty Body Functions

Functions defined by interfaces can also be called “empty body functions”, because only inputs and outputs and no function bodies are specified within MOSAICmodeling. They can be used as placeholders in equation systems. The output and input variables must be defined via an interface, but the mathematical expression will not be specified at that point. In this way, the equation system is independent of the functional correlation used to calculate the output variable. Similar to functions with defined bodies, several variable inputs are possible while only one output variable is calculated. Both input and output variables can be scalars or vectors. Hence, more than one functional dependence can be expressed by the use of one empty body function.

Model description

In this exercise the use of functions defined by interfaces is practiced. The following equation must hold:

   Z = \sum_{i=1}^4 z_i.

The components z_i of the vector z are calculated via the following mathematical operations:

   z_1 = x + y \\[2ex] z_2 = x - y \\[2ex] z_3 = \frac{x}{y} \\[2ex] z_4 = x \cdot y. \\[2ex]

This shall be achieved with an empty body function. The mathemical expressions shall thus be added later, in this case in Matlab. The function’s output variable z is thus a vector. In the simulation, y shall be determined iteratively for a given x and Z.


Notation of equation system

Set up a notation with the following base names and indices:

Base names
  • x, constant
  • y, iteration variable
  • Z, sum of all components of z_i
  • z, vector
  • i, index for components of z 1…NI

The resulting notation has ID 182679.


Now formulate the model equation defined above. The resulting equation has ID 182680.

Definition of interface

To create the interface, do the following in the Interface tab:

  1. Load the notation
  2. Add an informative description of the interface
  3. Add fields for x, y, and z_i by clicking on “+ Field Name”
  4. Select the dimensions of these three variables – Scalar for x and y, and Vector for z_i
  5. Select “In” for x and y, and “Out” for z_i in the In/Out column
  6. Go to the Pattern Assistance tab and select Function in General within the Type of Interface
  7. Save the interface

This interface is available in MOSAICmodeling with ID 182681.

Definition of function

To create the empty body function, we take the following steps in the Function tab:

  1. Load the notation of this example
  2. Add an informative description of the function
  3. Make sure that Load interface is selected in the Interface & Body Settings
  4. Load the interface you created
  5. Save the function

The function has ID 182682.

Equation system

Go to the tab Equation System and add your equation in the same way as in the previous examples. Then go to the Functions tab within the equation system and take the following steps:

  1. Add the function by clicking on the “Add” button at the bottom
  2. Add an application by clicking on “Add Application”
  3. Add z_i as Applied Naming for the output and x as well as y as Applied Naming for the input
  4. Confirm that and confirm again to close the popup windows. You should now see one application of the function within the equation system

The resulting equation system has ID 182683.

Evaluation / Simulation

Next, we can move to the “Simulation” section and load the equation system we just created. Then, we take the following steps:

  1. Add a description for your simulation
  2. In the tab on the Generic System, go to Functions and check whether MOSAICmodeling confirms one usage of the function in one application
  3. Go to Indexing and enter 4 as Max Value for NI; then click on Confirm Index Data
  4. Go to the Specifications tab and assign Z and x as design values
  5. The variables z_1 through z_4 should be classified as calculated values

Initialization, filling out the the empty body, and results

To initialize and specify the model, take the following steps:

  1. Initialize this example with the design values and initial guesses given in Table 1
  2. Save the variable specification 
  3. Save the simulation
  4. Go to the Evaluation tab and generate the code for Matlab
  5. Copy the generated code into Matlab and search for the following code:
function[std_z_i] = fun_182682__03_function_emptybody(std_x,std_y)
	std_z_i =  --- NO BODY DEFINED: ADD YOUR CODE HERE.--- ;

           Replace the body of the function with the following code:

function[std_z_i] = fun_182682__03_function_emptybody(std_x,std_y)
    std_z_i(1) = std_x + std_y;
    std_z_i(2) = std_x - std_y;
    std_z_i(3) = std_x / std_y;
    std_z_i(4) = std_x * std_y;

This simulation is available with ID 182684 with variable specification 182685. The solution for the iteration variable is also given in Table 1.

NameDescriptionValue / Initial guessSolution
ZParameter determining the sum of z_i18.0
z_1First component of z6.0
z_2Second component of z2.0
z_3Third component of z2.0
z_4Fouth component of z8.0
yIteration variable10.02.0