Prev | Next |
MATLAB Solver
MATLAB is a numerical computing environment and programming language that includes a large library of mathematical functions, each of which can be called from a script written in JavaScript.
You can invoke arbitrary mathematical functions from MATLAB at run time using a simple construct, called a Solver Class, written in JavaScript. A Solver Class for MATLAB can call into the external MATLAB tool via its API and link the mathematical functions directly into your running simulation. For example:
var matlab = new Solver("matlab");
var resultFromMatlab = matlab.exec('complexMathsFunction', parameter1, parameter2);
See the Solvers in Simulations Help topic.
Features include:
- Retrieving vectors, matrices, numbers, and strings from MATLAB
- MATLAB vectors return as JavaScript one-dimensional arrays (and JavaScript one-dimensional arrays return as MATLAB vectors)
- MATLAB matrices return as JavaScript two-dimensional arrays (and JavaScript two-dimensional arrays return as MATLAB matrices)
- You can retrieve variable values from MATLAB using:
matlab.get(<name>) - You can call any MATLAB function with JavaScript values using:
matlab.exec(<name>, [<arguments>]) - All arguments are passed inside a JavaScript object
- You can also use the result in JavaScript
- You can execute any MATLAB statement using:
matlab.exec("script")
MATLAB is available as an alternative to the GNU Octave library and can be used in all the same contexts as GNU Octave.
Note: Integration with MATLAB requires MATLAB version R2018b or higher.
Setup and Configuration
After you install MATLAB, Enterprise Architect will read the location from the registry to provide automatic integration.
If MATLAB does not load automatically, set the path (as in the Configure SysML Simulation Window Help topic) to the value obtained by running 'matlabroot' in the MATLAB console.
Usage
Use |
Discussion |
---|---|
Constructing |
Create a new Solver connected to MATLAB by passing 'matlab' to the Solver constructor. That is: var matlab = new Solver('matlab'); |
Assigning Values |
Assign values using the matlab.set function. For example: matlab.set('simple_value', 3.14); or var myString = "this is an example string"; matlab.set('myString', myString); |
Retrieving Values |
Retrieve results from MATLAB using the matlab.get function. For example: var simple_value = matlab.get('simple_value'); var myString = matlab.get('myString'); |
Calling Functions |
Pass the name of the function as the first parameter to Solver.exec. Either:
or
A helper function can be used here to avoid errors: // Wrap a variable number of arguments into an object to be passed to solver.exec function args() { var obj = {}; for (var i = 0; i < arguments.length; i++) { obj[i] = arguments[i]; } return obj; } var result = matlab.exec('minus', args(8, 4.5)); |
Trace() |
The Trace(statement) method allows you to print out trace statements at any arbitrary point in your simulation. This is an excellent means of supplementing the Simulation log with additional output information during execution. The Trace() output is written to the Simulation window. For example: matlab.set('simple_value', 3.14); var pi = matlab.get('simple_value'); Trace( "Simple Value = " + pi ); |
Videos
Sparx Systems provide a YouTube video of using the MATLAB console to create a MATLAB Solver. See:
Two further videos illustrate the use of the MATLAB Solver in performing a StateMachine Simulation of a Vaccine Trial, and a Lottery Draw. See: