# MATLAB NLE – decomposed and nested

This code generator uses the bordered block diagonal form of the algebraic equation system to decompose the system into an outer and an inner iteration loop. MOSAIC is seeing to the resorting of all equations and variables and generates all required solver calls.

### Required Solver

• fsolve (preinstalled with MATLAB)

### User Options

• Include Jacobian Pattern: If this option is set to “yes”, MOSAIC provides the incidence matrix of the Jacobian and includes it in the solver call.
• TolFun: MATLAB’s option to set the convergence criterion for fsolve. For more information refer to MATLAB’s documentation
• Use Analytic Derivatives: If this option is set to “yes”, MOSAIC provides a symbolic form of the Jacobian matrix to fsolve.
• MaxIter: MATLAB’s option to set the maximum number of iterations for fsolve. For more information refer to MATLAB’s documentation.
• MaxFunEvals: MATLAB’s option to set the maximum number of function evaluations of the algebraic system by fsolve. For more information refer to MATLAB’s documentation.
• Use Sparse Jacobian: If this option is set to “yes”, MOSAIC provides the Jacobian in a sparse form to fsolve.
• Reuse old Derivatives: If this option is set to “yes”, MOSAIC reuses the derivatives from a previous run. The user has to decided if something has changed and if it is necessary to regenerate the derivatives. The generation of the derivatives of course takes some time.
• Relax criticial operations: If this option is set to “yes”, log(x) and 1/x are written with as log(x+epsilon) and 1/(x+epsilon). The system is first solved with eps set to 1E-9 and in a second run with epsilon = 0.
• Use initial values for scaling: If this option is set to “yes”, the initial values are used to scale the numerical derivatives of the outer iteration loop.

### Usage for Simulation

1. Open an NLE Evaluation in MOSAIC
2. Set the variable specs to obtain a degree of freedom of zero.
3. Select the Predefined Language Specification “MATLAB NLE”.
4. Choose the Code Generator and Solver Properties in accordance with the User Options above.
5. Press Generate Code.
6. Copy everything from the View Code tab and paste it to a new m-file inside MATLAB’s IDE.
7. Save the new m-file
8. Press run inside MATLAB’s IDE.
9. After completion the results appear in MATLAB’s command line window.
10. Copy the lines containing the variable names and their final values and paste them into the Import tab of MOSAIC’s evaluation environment.
11. Select the Predefined Language Specification “MATLAB NLE” and press Import.
12.  The results will automatically update the Initial values in the Variable Specification tab and can be saved for future use by pressing Save As.