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

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:

  • For functions that take a single parameter, simply pass the value as the second parameter; for example:
         var result = matlab.exec('ceil', 7.4);

or

  • If 2 or more parameters are required, pass all the parameters as a JavaScript Object as the second parameter; this can be done inline such as:
         var result = matlab.exec('minus', {0: 8, 1: 4.5});
    Note: the order of the parameters is determined by the alphabetical order of the object names

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));

Videos

Sparx Systems provide a You Tube video of using the MATLAB console to create a MATLAB Solver. See:

      The MATLAB Console

Two further videos illustrate the use of the MATLAB Solver in performing a StateMachine Simulation of a Vaccine Trial, and a Lottery Draw. See:

      StateMachine Simulation - Vaccine Trial

      StateMachine Simulation - Lottery Draw

Learn more