Batch Compute Steady-State Operating Points Reusing Generated MATLAB Code

This example shows how to batch compute steady-state operating points for a model using generated MATLAB® code. You can batch linearize a model using the operating points and study the change in model behavior.

If you are new to writing scripts, interactively configure your operating points search using the Linear Analysis Tool. You can use Simulink® Control Design™ to automatically generate a script based on your Linear Analysis Tool settings.

  1. Open the Simulink model.

    sys = 'magball';
  2. In the Simulink Editor, select Analysis > Control Design > Linear Analysis.

  3. In the Linear Analysis Tool, in the Operating Point drop-down list, click Trim Model.

    By default, the software specifies all model states to be at equilibrium, as shown in the Steady State column.

  4. In the States tab, select the Known check box for the magball/Magnetic Ball Plant/height state.

  5. Click Start trimming to compute the operating point using numerical optimization.

    The Trim progress viewer shows that the optimization algorithm terminated successfully. The (Maximum Error) Block area shows the progress of reducing the error of a specific state or output during the optimization.

  6. In the Trim the model dialog box, click Generate MATLAB Script

    The MATLAB Editor window opens with an automatically generated script.

  7. Modify the script to trim the model at multiple operating points.

    1. Remove unneeded comments from the generated script.

    2. Define the height variable, height, with values at which to compute operating points.

    3. Add a for loop around the operating point search code to compute a steady-state operating point for each height value. Within the loop, before calling findop, update the reference ball height, specified by the Desired Height block.

    Your script should now look similar to this:

    %% Specify the model name
    model = 'magball';
    %% Create the operating point specification object.
    opspec = operspec(model);
    % State (5) - magball/Magnetic Ball Plant/height
    % - Default model initial conditions are used to initialize optimization.
    opspec.States(5).Known = true;
    %% Create the options
    opt = findopOptions('DisplayReport','iter');
    %% Specify ball heights at which to compute operating points
    height = [0.05;0.1;0.15];
    %% Loop over height values to find the corresponding operating points
    for i = 1:length(height)
        % Set the ball height in the specification
        opspec.States(5).x = height(i);
        % Update the model ball haight reference parameter
        set_param('magball/Desired Height','Value',num2str(height(i)))
        % Trim the model
        [op(i),opreport(i)] = findop(model,opspec,opt);

    After running this script, op contains operating points corresponding to each of the specified height values.

See Also

Related Topics

Was this topic helpful?