Main Content

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 by 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

  1. On the Apps tab, in the Apps section, click the arrow. In Application Deployment, click Application Compiler.

    Alternately, you can open the Application Compiler app by entering applicationCompiler at the MATLAB prompt.

  2. In the Compiler project window, specify the main file of the MATLAB application that you want to deploy.

    1. In the Main File section, click .

    2. In the Add Files window, browse to path where you have saved the prepared function, and select deployParameterTuning.m. Click Open.

      The function deployParameterTuning.m is added to the list of main files.

  3. Decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the two options in the Packaging Options section:

    • Runtime downloaded from web — Generates an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application.

    • Runtime included in package — Generates an installer that includes the MATLAB Runtime installer.

  4. Customize the packaged application and its appearance:

    • Application information — This section lists editable information about the deployed application. You can also customize the standalone applications appearance by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.

    • Command line input type options — This section lists selection of input data types for the standalone application. For more information, see Determine Data Type of Command-Line Input (For Packaging Standalone Applications Only).

    • Additional installer options — Edit the default installation path for the generated installer and selecting custom logo. See Change the Installation Path.

    • Files required for your application to run —Files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.

    • Files installed for your end user — This section lists the files that are installed with your application. These files include:

      • A generated readme.txt file

      • The generated executable for the target platform

      See Specify Files to Install with Application.

    • Additional runtime settings —This section lists platform-specific options for controlling the generated executable. See Additional Runtime Settings.

  5. To generate the packaged application, click Package. In the Save Project dialog box, specify the location to save the project.

  6. In the Package dialog box, verify that Open output folder when process completes is selected.

    When the packaging process is complete, examine the generated output.

See Also

Apps

Functions

Tools

Related Topics