{"id":5645,"date":"2023-08-20T23:37:45","date_gmt":"2023-08-20T22:37:45","guid":{"rendered":"http:\/\/mosaic-modeling.de\/?page_id=5645"},"modified":"2025-10-03T09:03:07","modified_gmt":"2025-10-03T08:03:07","slug":"use-of-user-defined-language-specifications-i-nonlinear-equation-system-with-matlab","status":"publish","type":"page","link":"https:\/\/mosaic-modeling.de\/?page_id=5645","title":{"rendered":"Use of User-defined Language Specifications I \u2013 Nonlinear equation system with Matlab"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">On this page<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Goal\" >Goal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Workflow-for-setting-up-a-UDLS\" >Workflow for setting up a UDLS<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Global-language-settings\" >Global language settings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Operations\" >Operations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Variable-namings\" >Variable namings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Code-generation-properties\" >Code generation properties<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Code-elements\" >Code elements<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Help-header\" >Help header<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Initialize-and-solve\" >Initialize and solve<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Model-equations\" >Model equations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Direct-function-calls\" >Direct function calls<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/mosaic-modeling.de\/?page_id=5645\/#Code-generation\" >Code generation<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Goal\"><\/span>Goal<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In this example, we will set up a UDLS for Matlab. This UDLS shall generate code to solve a nonlinear equation system containing function calls. The UDLS is available in MOSAICmodeling with ID 183144.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Workflow-for-setting-up-a-UDLS\"><\/span>Workflow for setting up a UDLS<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A UDLS is set up in the &#8220;Miscellaneous&#8221; section of MOSAICmodeling in the tab Language Specification. Should this tab be invisible to you and you would like to use it, please <a href=\"http:\/\/mosaic-modeling.de\/?page_id=4090\">request this feature<\/a> for your account.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Global-language-settings\"><\/span>Global language settings<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>We begin in the tab Global Language Settings by adding an informative description. As programming languages can change their syntax over time, you should also mention for which version this UDLS is tested. Then, perform the following steps:<\/p>\n<ol>\n<li>Activate Direct Functions Supported and Case Sensitive in the Language Settings. The former will allow us to include direct function calls, the latter ensures a difference between capital and small letters<\/li>\n<li>Activate Force Floating Point in the Number settings<\/li>\n<li>Select &#8220;NLE&#8221; as Equation System Type<\/li>\n<li>Select &#8220;Simulation&#8221; in the box Opt or Nopt<\/li>\n<\/ol>\n<p>The final specifications are shown in Figure 1. The button in the bottom right corner next to Minimum Specifications Complete should be green now.<\/p>\n<figure id=\"attachment_5650\" aria-describedby=\"caption-attachment-5650\" style=\"width: 1920px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5650\" src=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings.png\" alt=\"\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings.png 1920w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings-300x169.png 300w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings-1024x576.png 1024w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings-768x432.png 768w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings-1536x864.png 1536w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings-1110x624.png 1110w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_GlobalLanguageSettings-528x297.png 528w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><figcaption id=\"caption-attachment-5650\" class=\"wp-caption-text\">Figure 1: Settings in the tab Global Language Settings.<\/figcaption><\/figure>\n<h4><span class=\"ez-toc-section\" id=\"Operations\"><\/span>Operations<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Move to the Operations tab. We need to specify in which way basic operations are expressed in the programming language, i.e., Matlab. Therefore,<\/p>\n<ol>\n<li>Click on Add and add a &#8220;+&#8221; as Infix, click on the green arrow to see the output<\/li>\n<li>Click on Subtract and add a &#8220;-&#8221; as Infix, click on the green arrow to see the output<\/li>\n<li>Click on Multiply and add &#8220;(&#8221; as Prefix, &#8220;)*(&#8221; as Infix, and &#8220;)&#8221; as Suffix, click on the green arrow to see the output. The additional brackets ensure that any addition or subtraction terms in LEFT or RIGHT are correctly enclosed<\/li>\n<li>Click on Divide and add &#8220;(&#8221; as Prefix, &#8220;)\/(&#8221; as Infix, and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<li>Click on Power and add &#8220;(&#8221; as Prefix, &#8220;)^(&#8221; as Infix, and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<li>Click on R-root and add &#8220;(&#8221; as Prefix, &#8220;)^(1.0\/(&#8221; as Infix, and &#8220;))&#8221; as Suffix, click on the green arrow to see the output. Alternatively, you could implement the Matlab function nthroot(X,N). Attention: Make sure that you selected Swap LR because MOSAICmodeling uses the function \\sqrt[N]{X} internally, i.e., the first argument (LEFT) is the root&#8217;s exponent. This is not shown in Figure 2<\/li>\n<li>Click on Exp and add &#8220;exp(&#8221; as Prefix and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<li>Click on Log and add &#8220;log(&#8221; as Prefix and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<li>Click on Sinus and add &#8220;sin(&#8221; as Prefix and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<li>Click on Cosinus and add &#8220;cos(&#8221; as Prefix and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<li>Click on Parentheses and add &#8220;(&#8221; as Prefix and &#8220;)&#8221; as Suffix, click on the green arrow to see the output<\/li>\n<\/ol>\n<p>The remaining operations are not of interest for simulation code and can therefore be neglected. The final specifications are shown in Figure 2.<\/p>\n<figure id=\"attachment_5651\" aria-describedby=\"caption-attachment-5651\" style=\"width: 1920px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5651\" src=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations.png\" alt=\"\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations.png 1920w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations-300x169.png 300w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations-1024x576.png 1024w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations-768x432.png 768w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations-1536x864.png 1536w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations-1110x624.png 1110w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_Operations-528x297.png 528w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><figcaption id=\"caption-attachment-5651\" class=\"wp-caption-text\">Figure 2: Settings in the tab Operations.<\/figcaption><\/figure>\n<h4><span class=\"ez-toc-section\" id=\"Variable-namings\"><\/span>Variable namings<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Go to the tab Variable Namings and do the following:<\/p>\n<ol>\n<li>Select Use Variable Naming as Strategy<\/li>\n<li>Click on Namespace to base name and add a &#8220;_&#8221; as Infix, click on the green arrow to see the output. You see that there is now a &#8220;_&#8221; between std and P<\/li>\n<li>Add the same &#8220;_&#8221; for Leading superscript, Leading subscript, and Leading indexname. After every change, click on the green arrow to see what has changed<\/li>\n<li>In this example, we leave the Infix for Index name to index value empty<\/li>\n<\/ol>\n<p>The final specifications are shown in Figure 3.<\/p>\n<figure id=\"attachment_5652\" aria-describedby=\"caption-attachment-5652\" style=\"width: 1920px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5652\" src=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings.png\" alt=\"\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings.png 1920w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings-300x169.png 300w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings-1024x576.png 1024w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings-768x432.png 768w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings-1536x864.png 1536w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings-1110x624.png 1110w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_VariableNamings-528x297.png 528w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><figcaption id=\"caption-attachment-5652\" class=\"wp-caption-text\">Figure 3: Settings in the tab Variable Namings.<\/figcaption><\/figure>\n<h4><span class=\"ez-toc-section\" id=\"Code-generation-properties\"><\/span>Code generation properties<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Next, we skip the tab External Functions as we do not supply additional interfaces, and go directly to the tab Code Generation Properties. Now, we want to add a couple of basic solver options for Matlab&#8217;s fsolve:<\/p>\n<ol>\n<li>Add the the Property MaxIter with Handle _MAXITER_, Type INTEGER, and Default 1000<\/li>\n<li>Add the the Property TolFun with Handle _TOLFUN_, Type FLOAT, and Default 1e-10<\/li>\n<li>Add the the Property DisplayIter with Handle _DISPLAY_, Type STRING, and Default &#8216;iter&#8217;<\/li>\n<li>Add the the Property MaxFunEvals with Handle _MAXFUNEVALS_, Type INTEGER, and Default 100000<\/li>\n<li>Add the the Property File Name with Handle _FILENAME_, Type STRING, and Default MATLAB_NLE_export<\/li>\n<li>Click on the row containing the name DisplayIter and click on &#8220;+ Option&#8221; on the right-hand side. Enter &#8216;iter&#8217; and confirm. Repeat this for &#8216;notify&#8217;, &#8216;off&#8217;, &#8216;final&#8217;, &#8216;iter-detailed&#8217;, and &#8216;final-detailed&#8217;. This allows the user to select a preferred choice within MOSAICmodeling when a simulation shall be exported<\/li>\n<\/ol>\n<p>The final specifications are shown in Figure 4.<\/p>\n<figure id=\"attachment_5649\" aria-describedby=\"caption-attachment-5649\" style=\"width: 1920px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5649\" src=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties.png\" alt=\"\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties.png 1920w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties-300x169.png 300w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties-1024x576.png 1024w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties-768x432.png 768w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties-1536x864.png 1536w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties-1110x624.png 1110w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeGenerationProperties-528x297.png 528w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><figcaption id=\"caption-attachment-5649\" class=\"wp-caption-text\">Figure 4: Settings in the tab Code Generation Properties.<\/figcaption><\/figure>\n<h4><span class=\"ez-toc-section\" id=\"Code-elements\"><\/span>Code elements<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>This tab is fairly complicated as it contains the structure for the generated code (see Figure 5). We will try to be as concise as possible in our description. However, you can of course also directly open the UDLS with the ID given above.<\/p>\n<figure id=\"attachment_5663\" aria-describedby=\"caption-attachment-5663\" style=\"width: 1920px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-5663 size-full\" src=\"http:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements.png\" alt=\"\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements.png 1920w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements-300x169.png 300w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements-1024x576.png 1024w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements-768x432.png 768w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements-1536x864.png 1536w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements-1110x624.png 1110w, https:\/\/mosaic-modeling.de\/wp-content\/uploads\/2023\/08\/UDLS_MATLAB_NLE_CodeElements-528x297.png 528w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><figcaption id=\"caption-attachment-5663\" class=\"wp-caption-text\">Figure 5: Settings in the tab Code Elements.<\/figcaption><\/figure>\n<p>We begin in the in the block _FULL_PROBLEM_SOLVING_CODE_. This block shall basically contain the overall structure of the generated code. Here, we now add the following lines:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>_Help_Header_\n\n_Initialize_And_Solve_\n\n_Model_Equations_\n\n_DIR_FUN_IMPL_LOOP_\n\n_Display_Results_<\/code><\/pre>\n\n\n<p>These blocks will execute the following tasks:<\/p>\n<ul>\n<li>_Help_Header_: generation of documentation code at the beginning<\/li>\n<li>_Initialize_And_Solve_: variable initialization and call of fsolve<\/li>\n<li>_Model_Equations_: model equations<\/li>\n<li>_DIR_FUN_IMPL_LOOP_: loop writing the code for the functions that are part of the model<\/li>\n<li>_Display_Results_: will show the results for the calculated variables<\/li>\n<\/ul>\n<p>We will now go through these blocks in sequential order. Note that when you really develop a UDLS, this process might be much more iterative than in this example here.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Help-header\"><\/span>Help header<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>The block _Help_Header_ does not exist so far.\u00a0 Hence, we create it by highlighting the _FULL_PROBLEM_SOLVING_CODE_ block and clicking on &#8220;+ Node&#8221; at the bottom left corner. Define the handle _Help_Header_ in the popup window and click on Submit&#8221;. You can now move the created block upwards or downwards using the two arrows.<\/p>\n<p>Highlight the new block and enter the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>% Matlab code to solve the nonlinear equation system below\n% Code generated with MOSAICmodeling<\/code><\/pre>\n\n\n<p>Hence, this block will only add a couple of comments at the beginning. You can make this much more complicated and add the whole notation of your system, but we will keep it simple here.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Initialize-and-solve\"><\/span>Initialize and solve<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>The next block is responsible for variable and parameter specification as well as the call of fsolve. First, we need to create the block again in the same way as above. Then, we highlight it and add the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>function InitializeAndSolveEquationSystem()\n\n\t% load variable start values\n_Set_Start_VARS_LOOP_\n\t% load parameters\n_Set_PARAMS_LOOP_\n\toptions = optimoptions('fsolve','MaxIter',_MAXITER_,'MaxFunEvals',_MAXFUNEVALS_,'TolFun',_TOLFUN_,'Display',_DISPLAY_);\n\t&#091;SOL,RES,EXITFLAG] = fsolve (@( X_ITER ) modelEquations(X_ITER,PARAMS),X_INIT,options);\n\n\tdisplayResults(SOL);\n\tfprintf('\\n')\n\tfprintf('Residuals: \\n')\n\tdisp(RES')\n\tfprintf('Terminated with exitflag %i \\n',EXITFLAG)\n\nend<\/code><\/pre>\n\n\n<p>This code creates a function in Matlab that begins with a looping setting the initial guesses for the variables. Then, the parameter values are set within a loop. Afterwards, the solver options are set based on the handles that were created in the tab Code Generation Properties. Then, we have the call to fsolve. Finally, we display the results, the residuals, and the exitflag of the solver. As you can see, this is basically Matlab code in combination with a couple of handles and blocks of the UDLS.<\/p>\n<p>As you can see, the block requires the loops _Set_Start_VARS_LOOP_ and _Set_PARAMS_LOOP_. These loops are now added as subnodes of the node _Initialize_And_Solve_. Add the first node as &#8220;Variables&#8221; block and the second node as &#8220;Parameters&#8221; block. After you have added both nodes, highlight the first one.<\/p>\n<p>One the right-hand side, you can see all available subhandles, e.g., _CTRVALUE_, which is the index of the variables. Add the code below to this block:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>X_INIT(_CTRVALUE_) = _VAR_VALUE_;   % _VAR_NAME_<\/code><\/pre>\n\n\n<p>This code will be executed as loop and will write the variable X_INIT with an increasing index. The respective enry of X_INIT will receive the value _VAR_VALUE_.<\/p>\n<p>Then, go to the PARAMS loop and the following line:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>PARAMS(_CTRVALUE_) = _PAR_VALUE_;\t% _PAR_NAME_\n<\/code><\/pre>\n\n\n<p>This will do the same for the parameter values. At every point of your code development, you can go to the tab Overall Results and load any simulation you have access to. Then, you can go back to the tab Code Elements and click on &#8220;Code&#8221; at the bottom right corner of the editor. This generates the code of the highlighted block according to your current specifications.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Model-equations\"><\/span>Model equations<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>We now move on to formulating the model equations. Therefore, create the block _Model_Equations_ and add the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>function&#091;Y] = modelEquations(X,PARAMS)\n\n\t% read out variables  \n_Read_VARS_LOOP_\n\t% read out parameters \n_Read_PARAMS_LOOP_\n\t% perform direct function calls\n_DIR_FUN_CALL_LOOP_\n\t% evaluate the function values  \n_Calc_EQUAS_AE_LOOP_\nend<\/code><\/pre>\n\n\n<p>This code creates a Matlab function that reads all variable and parameter values, calls the functions of the model, and then calculates the equations of the model. Three subnodes are required (the block _DIR_FUN_CALL_LOOP_ is automatically included for the UDLS). Create the first subnode as &#8220;Variables&#8221;, block, the second one as &#8220;Parameters&#8221; block, and the third one as &#8220;EquationsAE&#8221; block. In the first block, add the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\t_VAR_NAME_ = X(_CTRVALUE_);\n<\/code><\/pre>\n\n\n<p>This code is again executed in a loop and writes the current entry of X into the variable name _VAR_NAME_. In the second block, add<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\t_PAR_NAME_ = PARAMS(_CTRVALUE_);<\/code><\/pre>\n\n\n<p>This does the same for the parameters. Finally, add the following code to the third block:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\tY(_CTRVALUE_) = _CALC_EXPR_;<\/code><\/pre>\n\n\n<p>This loop writes each equation into one entry of the variable Y.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Direct-function-calls\"><\/span>Direct function calls<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Now, we need to make sure that the functions within the model are called and implemented correctly. We start with the former by highlighting the block _DIR_FUN_CALL_LOOP_ and adding the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\t_FCT_OUT_VAR_NAME_ = _FUN_NAMESHORT_(_FUN_CALL_VARS_LOOP__SEP_SYMBOL__FUN_CALL_PARAMS_LOOP_);\n<\/code><\/pre>\n\n\n<p>This code specifies the output varnames of the functions within a loop. The functions are called with their shortnames with the call arguments and call parameters, both separated by the SEP_SYMBOL (\u201c,\u201d). Note that DIR_FUN_CALL_LOOP should contain subnodes FUN_CALL_VARS_LOOP and FUN_CALL_PARAMS_LOOP with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>_VAR_NAME__SEP_SYMBOL_<\/code><\/pre>\n\n\n<p>and<\/p>\n\n\n<pre class=\"wp-block-code\"><code>_PAR_NAME__SEP_SYMBOL_<\/code><\/pre>\n\n\n\n<p>Afterwards, we specify how the functions are implemented. Therefore, we highlight the block _DIR_FUN_IMPL_LOOP and add the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function&#091;_FCT_OUT_VAR_NAME_] = _FUN_NAMESHORT_(_FUN_IMPL_VARS_LOOP__SEP_SYMBOL__FUN_IMPL_PARS_LOOP_)\n\t_FCT_OUT_VAR_NAME_ = _FUN_EXPR_;\nend<\/code><\/pre>\n\n\n<p>This code creates Matlab functions for all functions that are part of the model. The function returns the output variable name, is named according to its shortname (attention, here function call and function implementation must be consistent!), and receives the list of input variables and parameters, separated by the SEP_SYMBOL. For the list of input variables and parameters, the implementation block needs the subnodes FUN_IMPL_VARS_LOOP and FUN_IMPL_PARS_LOOP with the same code as in the call loop above. Afterwards, the output variable is calculated using the function expression FUN_EXPR.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>function displayResults(SOL)\n\n\t% print variable values to display\n_Disp_VARS_LOOP_\nend<\/code><\/pre>\n\n\n<p>Within this function, we place another variable loop block with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\tdisp(&#091;'_VAR_NAME_ \t ', num2str(SOL(_CTRVALUE_),'%.8f')]);\n<\/code><\/pre>\n\n\n<p>This code displays the variable name and the respective entry of the solution vector SOL with 8 decimal digits.<\/p>\n<p>Now save the UDLS at a location of your preference.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Code-generation\"><\/span>Code generation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>As already alluded to above, you can now go to the tab Overall Results, load a simulation, and click on Generate Full Code. This is good for debugging when you develop your UDLS.\u00a0<\/p>\n<p>Once you are happy with the results, you can use the UDLS in the &#8220;Simulation&#8221; or &#8220;Optimization&#8221; section of MOSAICmodeling (here, we stick to simulations). In this example, we will load the simulation from the example <strong>Use of Functions III \u2013 Parameter Lists and Indices<\/strong> with ID 182676. Go to the Evaluation tab and then to the Generation tab. Within the Language Specification, select &#8220;User-defined&#8221; and open your saved UDLS. Note that you can now set the options you added in the tab Code Generation Properties. Once you are happy with the specificed options, click on Generate Code. The result is shown below. This code can now be copied to Matlab to solve the equation system.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>% Matlab code to solve the nonlinear equation system below\n% Code generated with MOSAICmodeling\n\nfunction InitializeAndSolveEquationSystem()\n\n\t% load variable start values\n\tX_INIT(1) = 0.5;\t% e0_L\n\tX_INIT(2) = 0.5;\t% e0_V\n\tX_INIT(3) = 0.5;\t% e0_x_c1\n\tX_INIT(4) = 0.5;\t% e0_x_c2\n\tX_INIT(5) = 0.5;\t% e0_y_c1\n\tX_INIT(6) = 0.5;\t% e0_y_c2\n\n\t% load parameters\n\tPARAMS(1) = 1.0;\t% e0_F\n\tPARAMS(2) = 354.6;\t% e0_T\n\tPARAMS(3) = 0.5;\t% e0_z_c1\n\tPARAMS(4) = 0.5;\t% e0_z_c2\n\tPARAMS(5) = 101325.0;\t% e0_P\n\tPARAMS(6) = 82.718;\t% e0_A_c1\n\tPARAMS(7) = 73.649;\t% e0_A_c2\n\tPARAMS(8) = -6904.5;\t% e0_B_c1\n\tPARAMS(9) = -7258.2;\t% e0_B_c2\n\tPARAMS(10) = -8.8622;\t% e0_C_c1\n\tPARAMS(11) = -7.3037;\t% e0_C_c2\n\tPARAMS(12) = 7.4664E-6;\t% e0_D_c1\n\tPARAMS(13) = 4.1653E-6;\t% e0_D_c2\n\tPARAMS(14) = 2.0;\t% e0_E_c1\n\tPARAMS(15) = 2.0;\t% e0_E_c2\n\n\toptions = optimoptions('fsolve','MaxIter',1000,'MaxFunEvals',100000,'TolFun',1e-10,'Display','Iter');\n\t&#091;SOL,RES,EXITFLAG] = fsolve (@( X_ITER ) modelEquations(X_ITER,PARAMS),X_INIT,options);\n\n\tdisplayResults(SOL);\n\tfprintf('\\n')\n\tfprintf('Residuals: \\n')\n\tdisp(RES')\n\tfprintf('Terminated with exitflag %i \\n',EXITFLAG)\n\nend\n\nfunction&#091;Y] = modelEquations(X,PARAMS)\n\n\t% read out variables  \n\te0_L = X(1);\n\te0_V = X(2);\n\te0_x_c1 = X(3);\n\te0_x_c2 = X(4);\n\te0_y_c1 = X(5);\n\te0_y_c2 = X(6);\n\n\t% read out parameters \n\te0_F = PARAMS(1);\n\te0_T = PARAMS(2);\n\te0_z_c1 = PARAMS(3);\n\te0_z_c2 = PARAMS(4);\n\te0_P = PARAMS(5);\n\te0_A_c1 = PARAMS(6);\n\te0_A_c2 = PARAMS(7);\n\te0_B_c1 = PARAMS(8);\n\te0_B_c2 = PARAMS(9);\n\te0_C_c1 = PARAMS(10);\n\te0_C_c2 = PARAMS(11);\n\te0_D_c1 = PARAMS(12);\n\te0_D_c2 = PARAMS(13);\n\te0_E_c1 = PARAMS(14);\n\te0_E_c2 = PARAMS(15);\n\n\t% perform direct function calls\n\te0_P_LV_c2 = fun_182674(e0_T,e0_A_c2,e0_B_c2,e0_C_c2,e0_D_c2,e0_E_c2);\n\te0_P_LV_c1 = fun_182674(e0_T,e0_A_c1,e0_B_c1,e0_C_c1,e0_D_c1,e0_E_c1);\n\n\t% evaluate the function values  \n\tY(1) = (e0_F)*(e0_z_c1)-((e0_L)*(e0_x_c1)+(e0_V)*(e0_y_c1));\n\tY(2) = (e0_F)*(e0_z_c2)-((e0_L)*(e0_x_c2)+(e0_V)*(e0_y_c2));\n\tY(3) = (e0_x_c1)*(e0_P_LV_c1)-((e0_y_c1)*(e0_P));\n\tY(4) = (e0_x_c2)*(e0_P_LV_c2)-((e0_y_c2)*(e0_P));\n\tY(5) = 1.0-((e0_x_c1+e0_x_c2));\n\tY(6) = 1.0-((e0_y_c1+e0_y_c2));\n\nend\n\nfunction&#091;std_P_LV] = fun_182674(std_T,std_A,std_B,std_C,std_D,std_E)\n\tstd_P_LV = exp(std_A+(std_B)\/(std_T)+(std_C)*(log(std_T))+(std_D)*(((std_T))^(std_E)));\nend\n\nfunction displayResults(SOL)\n\n\t% print variable values to display\n\tdisp(&#091;'e0_L \t ', num2str(SOL(1),'%.8f')]);\n\tdisp(&#091;'e0_V \t ', num2str(SOL(2),'%.8f')]);\n\tdisp(&#091;'e0_x_c1 \t ', num2str(SOL(3),'%.8f')]);\n\tdisp(&#091;'e0_x_c2 \t ', num2str(SOL(4),'%.8f')]);\n\tdisp(&#091;'e0_y_c1 \t ', num2str(SOL(5),'%.8f')]);\n\tdisp(&#091;'e0_y_c2 \t ', num2str(SOL(6),'%.8f')]);\n\nend\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal In this example, we will set up a UDLS for Matlab. This UDLS shall generate code to solve a nonlinear equation system containing function calls. The UDLS is available in MOSAICmodeling with ID 183144. Workflow for setting up a UDLS A UDLS is set up in the &#8220;Miscellaneous&#8221; section of MOSAICmodeling in the tab [&hellip;]<\/p>\n","protected":false},"author":252,"featured_media":0,"parent":5484,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5645","page","type-page","status-publish","czr-hentry"],"_links":{"self":[{"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/pages\/5645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/users\/252"}],"replies":[{"embeddable":true,"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5645"}],"version-history":[{"count":12,"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/pages\/5645\/revisions"}],"predecessor-version":[{"id":6251,"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/pages\/5645\/revisions\/6251"}],"up":[{"embeddable":true,"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=\/wp\/v2\/pages\/5484"}],"wp:attachment":[{"href":"https:\/\/mosaic-modeling.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}