Deploy Simulations with Tunable Parameters
With Simulink® Compiler™, you can deploy simulations that use tunable parameters.
As you construct a model, you can experiment with block parameters, such as the coefficients of a Transfer Fcn block, to help you decide which blocks to use. You can simulate the model with different parameter values, and capture and observe the simulation output.
You can change the values of most numeric block parameters during a simulation. This technique allows you to quickly test parameter values while you develop an algorithm. You can:
Tune and optimize control parameters.
Calibrate model parameters.
Test control robustness under different conditions.
The following example shows how to set a tunable parameter in a model, write a standalone application that can be used to tune the parameters, and analyze the simulations. For more information on tunable parameters, see Tune and Experiment with Block Parameter Values.
If the models contain non-deployable mask initialization code, there may be errors while
tuning the parameter. These models are deployable but are limited in their functionality due
to them containing certain parameters that cannot be tuned during runtime. You can recompile
the model with new values for such parameters. You can use the
simulink.compiler.getTunableVariables
function to figure out which
variables in the model are tunable.
When simulating the top model, to tune referenced workspace variables inside nested model references, you can mark the referenced variables as model arguments and pass their instance specific values from the top model via a model block. Alternatively, you can define all the variables in all the nested models in the base workspace.
Prepare a Script to Deploy Simulations with Parameter Tuning
In this example, create a MATLAB® function to simulate the model sldemo_suspn_3dof
with the values of Simulink.SimulationInput
. Save the script as
deployParameterTuning.m
on the MATLAB path.
Prepare a Function to Deploy
Create a function called deployParameterTuning
containing the code
shown below. This code creates a Simulink.SimulationInput
object for
the model sldemo_suspn_3dof
. mb
is the value that we
pass through the setVariable
method for the tunable parameter,
Mb
. To configure this script to be deployed, use the function
simulink.compiler.configureForDeployment
.
simulink.compiler.configureForDeployment
configures the
Simulink.SimulationInput
object for deployment by setting its
simulation mode to Rapid Accelerator and by restricting inputs that require rebuilding the
deployed app.
function deployParameterTuning(oName, mb) if ischar(mb) || isstring(mb) mb = str2double(mb); end if isnan(mb) || ~isa(mb, 'double') || ~isscalar(mb) disp('The value of mb given to deployParameterTuning must be a double scalar or a string or character that can be converted to a double scalar'); end in = Simulink.SimulationInput('sldemo_suspn_3dof'); in = in.setVariable('Mb', mb); in = simulink.compiler.configureForDeployment(in); out = sim(in); save(oName, 'out'); end
Deploy the Prepared Function
Build the standalone application using the
compiler.build.standaloneApplication
function.buildResults = compiler.build.standaloneApplication( ... "deployParameterTuning.m","Verbose",true);
You can specify additional options in the
compiler.build
command by using name-value arguments. For details, seecompiler.build.standaloneApplication
.The
compiler.build.Results
objectbuildResults
contains information on the build type, generated files, included support packages, and build options.The function generates the executable file named
deployParameterTuning
within a folder nameddeployParameterTuningstandaloneApplication
in your current working directory.Note
The generated files do not include an installer for the application or MATLAB Runtime. To create an installer using the
buildResults
object, seecompiler.package.installer
.Run your application from the system command prompt. To test your application in MATLAB before deployment, run the executable using the bang (
!
) operator. For instance:!deployParameterTuning.exe
To deploy your application outside of MATLAB, you must have MATLAB Runtime installed at the same version as the MATLAB version used to build the application. Ensure that your end users can run the application by including MATLAB Runtime in the installer or by supplying users with information on how to download it. For information on installing and using MATLAB Runtime, see About MATLAB Runtime.
See Also
Apps
- MATLAB Coder (MATLAB Coder)
Functions
configureForDeployment
|Simulink.SimulationInput
|compiler.build.standaloneApplication
|mcc
|simulink.compiler.genapp
|sim