As part of our Enterprise Architect 15.2 features for leveraging Maths we have new
features for working with MATLAB as a Solver this can be used in simulations by assigning values to
MATLAB variables and then calling functions that return results to be used in the simulation.
We can also use this to get MATLAB to plot the results.
In this example we use a MATLAB procedure for calculating
the amount of solar radiation on a surface area.
Then using that to derive the amount of power delivered from a solar cell of that size.
We will use this function in a State Machine simulation. With this we will simulate starting
and stopping a machine based on the calculated output of the panel.
We will do this with the state machine being directed from a Win32 window.
That is with the process being started from a button
and the input details and results being exchanged via fields on that window.
We'll also show how this function can be used in a simulation to return plots generated by MATLAB.
What we will cover is initializing the MATLAB solver setting values to MATLAB variables
using matlab.set executing. A MATLAB command using matlab.exec.
How to use the execute command to generate a plot.
How to get values from MATLAB using the matlab.get.
We will then have a brief look at setting up a Win32 interface
to get our MATLAB simulation going with the external user input
rather than fixed values.
Okay so let's start by having a look at setting up our simulation.
We have a MATLAB function that needs the following variables. A day in the year.
Location details including longitude, latitude and timezone. The vertical mount angle of a panel.
The direction of the panel. South in the northern hemisphere. North in the southern hemisphere.
So we want to generate some plots from MATLAB functions for our solar radiation across a day.
We also set up a simulation that on the hour checks if the wattage output can supply a 2kW appliance.
Let's say a motor. This switches the state machine flow based on the power output.
For the state machine diagram we will start with a simple scenario of just using matlab.set.
Set its values into variables and then use the matlab.exec to run a plot.
As we will be working with simulations.
Let's start by opening up our simulation and the Simulation Events Panel.
Then we set our perspective to UML Behavioural and select a Starter State Machine by using the
Model Wizard that is the Control Shift + M.
This can be either UML or a SysML State Machine
We start with an initial element with two states and a final state
and a single transition for a simple direct flow.
First thing we need to do is to create a new Solver for connecting the script to MATLAB
by passing the MATLAB to the Solver Constructor.
We do this on the effect of the incoming transition on the first state.
That is in the script we place: /var matlab = new Solver('matlab');
Now let's set up an entry operation where we'll place some script.
We name this Operation Set Values to open the editor for this we use the Alt+7 keys
Firstly we need to set the path to find the MATLAB function that we are going to use.
This is the panel radiation function. To use this MATLAB function we need to pass
it some parameters by setting up MATLAB variables with values.
In this simple case we will set text constants on the JavaScript side and
use the matlab.set routine to create a variable in MATLAB
and set these values from the text.
For example we set MATLAB a variable called longitude and assign it a number 143.86
With the time zone we set this as a string of +10.
The format of the matlab.set function consists of defining the MATLAB variable
as a string in the first parameter and its value as the second parameter.
We will later cover getting values into the MATLAB variables from the simulations user interface.
So let's move on to looking more deeply at using the matlab.exec to run a function in MATLAB.
The key MATLAB function to do our calculation is the panel radiation function.
It creates arrays of values covering the radiation value for each hour in the day.
Note that this is using parameters that we set earlier with the matlab.set command
with returns like this it's best to leave these in MATLAB until we need to operate on them.
This keeps the script clear and simpler with less need to do any conversions.
So let's now place this execution call in the transition effects field.
The format of this call is matlab.exec the first parameter is a string containing the
MATLAB statement to be executed. This includes the function name with the parameters to be passed to it.
In this case it is panel radiation.
The values for each parameter we have set earlier using the matlab.set function.
This grip line has results from the panel radiation function being stored in the MATLAB arrays on the left.
We will use these to do a plot.
We will look at a matlab.get function to retrieve the times array and use individual values to vary our state transition.
Now in the next state we will do a simple matlab.exec to output the generated results as a plot.
We do this by adding some more Matlab.exec calls to generate the plot.
For this we create an entry operation and name it "Plot".
We will use matlab.set commands to set the details to be plotted
as well as the labels, titles and the legend for the plot.
Finally just to make sure the Solver is not cleared at the end of the State Machine flow.
We will add a trigger on the last transition to ensure that the state holds, until we prompted.
Okay let's now try running the simulation.
We open the Simulation Tab and click on the Run button.
This returns our plot from MATLAB showing the radiation levels for this particular day
at the defined Geo location.
Let's look at using the matlab.get function for returning values back to variables in our simulation script.
and then use these values to direct the flow of the state machine.
What we will set up is a simulation that steps through the hours and checks
the panel array output to see if it can power a two kilowatt load.
If so this switches to the On state otherwise to the Off state.
In the first state we will initialize a counter index to count the hours.
We still have our main call to the panel radiation in the transition.
To this we add a matlab.exec to calculate the solar power from the panel radiation array.
This is passed to the MATLAB array
We will rename this state to MachineLoop.
Then in the next state we use the matlab.exec to retrieve a single MATLAB variable,
a value for the current hours power from the array.
Then we create two states On and Off
On each we set a transition to and from the MachineLoop state.
For the transitions out of the MachineLoop state we set the condition
as to whether the power output is greater than 2 kW.
For this we set a conditional statement using a Matlab.get in the transitions guard statement.
In both the on/off states we need to use the matlab.exec to get the time from its array and
output this as a string variable in MATLAB. We will use an entry operation for setting these,
so we set up a showPower entry operation in both states.
Then we use the matlab.get to pass this as a simulation variable.
We have a trace command to output the power and time values to our simulation panel.
To finish this we need to set a transition to the Final state,
with a condition in the guard set to exit, when the hour counter reaches 12 hours.
Let's now look at running the Simulation.
To see this working, we will run the simulation
and check the output of the trace statements in the simulation window.
As another method of checking these values.
We use the simulation dialog and view the values of the JavaScript variables using the Locals View.
To do this we set a Breakpoint on a state.
Now we can run over the simulation while viewing
the values of the variables in the Locals View over several passes of the simulation.
Let's now create a Win32 Diagram and add buttons - set up our user interface for the simulation.
For this we set the Perspective to UX Design and select the Win32 UI.
For a quick start we use the Model Wizard to create a simple screen with fields and buttons.
Using this we create a set of fields to match our input.
We use the OK button to start our MATLAB procedure with the values from the fields on the dialog.
For this we want to set a trigger to be acted on by a signal in the state machine.
To do this we define an "on click" tag value with a UI broadcast statement
to call a signal called Estimate.
These are then used to trigger the transitions out of the initial state.
Before the exit to the transition it runs over the exit operation, which we'll look at setting up shortly.
To edit this we open scripts for all the states by selecting the parent and using Alt+7.
Now to set some default values for the Win32 fields, we place some script with values.
We hide some buttons used later and then show the dialog.
Now to get these values from the Win32 fields after the OK button triggers the evaluate signal.
We create an exit operation and call it pass parameters and place the script there.
This script gets the dialog field values into the sim variables
then sets these values in the MATLAB variables using the matlab.set function.
Just to make the UI more meaningful,
we set two controls to show or not show depending on the state.
To automate these controls we add simple statements in the entry operation of both the
On and the Off states. In these we add two lines to pass the values of the time and the power to
be shown on the user interface dialog. We also set the .Show on the UI controls to true or false.
To show or not show depending upon whether the flow is in the On or the Off state.
Let's have a look at the final model running as a simulation.
With this we can see the state machine running the dialogue in simulation mode.
With the OK button we trigger the transition, this uses the matlab.exec to call the panel radiation function.
On the next state using the get statement in the transitions.
These conditional statements are directing the flow to the On and Off state
- based on the condition in the guard.
From this we see that it starts with the Off state and then when the power output reaches 2 watts,
it transitions to the On state - to pass this back to the user via the dialog. We can see the MachineOn
and MachineOff buttons are being set to hide or show depending upon what state the flow is in.
This conforms to the script we added earlier in the on/off entry operations.
Now in conclusion we will run over what we have looked at in this demonstration.
First of all we ran over how to initialize MATLAB Solver using the set command
as well as covering setting values to MATLAB variables using the matlab.set function.
Then we looked at executing a MATLAB command using the matlab.exec function.
We use this to execute commands to generate a plot.
Then we access values from MATLAB using the matlab.get function
and finally we had a brief look at setting up the Win32 interface
for setting user-defined values in our simulation.
This integration with MATLAB using the Solvers is one of a number of features for expressing your
models through Mathematical Graphs and Simulation in the 15.2 release of Enterprise Architect