## Documentation |

This example shows how to programmatically optimize controller
parameters to meet step response requirements using `sdo.optimize`.

The model `sldo_model1` includes the following
blocks:

**Controller block**, which is a PID controller. This block controls the output of the`Plant`subsystem.**Unit Step block**applies a step input and produces the model output that should meet step response requirements.You can also use other types of inputs, such as ramp, to optimize the response to meet step response requirements generated by such inputs.

**Plant subsystem**is a second-order system with delay. It contains Transfer Function and Transport Delay blocks.

The plant output must meet the following step response requirements:

Rise time less than 2.5 seconds

Settling time less than 30 seconds

Overshoot less than 5%

Open the Simulink

^{®}model.`sys = 'sldo_model1'; open_system(sys);`

Log the model output signal.

Design requirements require logged model signals. During optimization, the model is simulated using the current value of the model parameters and the logged signal is used to evaluate the design requirements.

PlantOutput = Simulink.SimulationData.SignalLoggingInfo; PlantOutput.BlockPath = [sys '/Plant']; PlantOutput.OutputPortIndex = 1; PlantOutput.LoggingInfo.NameMode = 1; PlantOutput.LoggingInfo.LoggingName = 'PlantOutput';

Store the logging information.

simulator =

`sdo.SimulationTest`(sys); simulator.LoggingInfo.Signals = PlantOutput;`simulator`is a`sdo.SimulationTest`object that you also use later to simulate the model.Specify step response requirements.

StepResp = sdo.requirements.StepResponseEnvelope; StepResp.RiseTime = 2.5; StepResp.SettlingTime = 30; StepResp.PercentOvershoot = 5;

`StepResp`is a`sdo.requirements.StepResponseEnvelope`object.

When you optimize the model response, the software modifies parameter (design variable) values to meet the design requirements.

Select model parameters to optimize.

p = sdo.getParameterFromModel(sys,{'Kp','Ki','Kd'});

`p`is an array of 3`param.Continuous`objects.To limit the parameters to positive values, set the minimum value of each parameter to

`0`.p(1).Minimum = 0; p(2).Minimum = 0; p(3).Minimum = 0;

Create a design function.

evalDesign = @(p) sldo_model1_design(p,simulator,StepResp);

`evalDesign`is an anonymous function that calls the cost function`sldo_model1_design`. The cost function simulates the model and evaluates the design requirements.(Optional) Evaluate the current response.

Compute the model response using the current values of the design variables.

initDesign = evalDesign(p);

During simulation, the

`Step Response`block throws assertion warnings at the MATLAB^{®}prompt which indicate that the requirements specified in the block are not satisfied.Examine the nonlinear inequality constraints.

initDesign.Cleq

ans = -0.2571 -0.4968 -0.5029 -1.0000 0.7060 0.5092 0.4964 201.4682

Some

`Cleq`values are positive, beyond the specified tolerance, indicating that the response using the current parameter values violates the design requirements.

Specify optimization options.

opt =

`sdo.OptimizeOptions`; opt.MethodOptions.Algorithm = 'active-set';The software configures

`opt`to use the default optimization method,`fmincon`. However, because the optimization problem specifies only constraints, you specify the Active Set algorithm for`fmincon`. The Active Set algorithm is good for solving feasibility (constraints only) optimization problems.Optimize the response.

[pOpt,optInfo] = sdo.optimize(evalDesign,p,opt);

At each optimization iteration, the software simulates the model, and the default optimization solver

`fmincon`modifies the design variables to meet the design requirements. For more information, see Selecting Optimization Methods and How the Optimization Algorithm Formulates Minimization Problems.After the optimization completes, the command window displays the following results:

max Step-size First-order Iter F-count f(x) constraint optimality 0 5 0 201.5 1 12 0 167.5 0.687 0 2 19 0 0.8462 1.22 5.86 3 29 0 0.8169 0.133 0 4 37 0 0.7819 0.115 0 5 44 0 3.121 0.265 3.29 6 52 0 3.328 2.92 0 7 59 0 0.1413 1.26 2.81 8 66 0 0.02418 0.443 0.0029 9 73 0 8.125e-05 0.0563 1.03e-05 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the selected value of the function tolerance, and constraints are satisfied to within the selected value of the constraint tolerance.

The message

`Local minimum found that satisfies the constraints`indicates that the optimization solver found a solution that meets the design requirements within specified tolerances. For more information about the outputs displayed during the optimization, see Iterative Display in the Optimization Toolbox™ documentation.Examine the optimization termination information, contained in the

`optInfo`output argument. This information helps you verify that the response meets the step response requirements.For example, check the following fields:

`Cleq`, which shows the optimized nonlinear inequality constraints.optInfo.Cleq

ans = 0.0001 -0.0099 -0.0099 -1.0000 0.0004 -0.0100 -0.0101 -0.1997

All values satisfy

`Cleq`≤`0`, within the optimization tolerances, which indicates that the step response requirements are satisfied.`exitflag`, which identifies why the optimization terminated.The value is

`1`which indicates that the solver found a solution which was less than the specified tolerances on the function value and constraint violations.

View the optimized parameter values.

pOpt

pOpt(1,1) = Name: 'Kp' Value: 1.2364 Minimum: 0 Maximum: Inf Free: 1 Scale: 1 Info: [1x1 struct] pOpt(2,1) = Name: 'Ki' Value: 0.3901 Minimum: 0 Maximum: Inf Free: 1 Scale: 1 Info: [1x1 struct] pOpt(3,1) = Name: 'Kd' Value: 2.5937 Minimum: 0 Maximum: Inf Free: 1 Scale: 1 Info: [1x1 struct]

Simulate the model with the optimized values.

Update optimized parameter values in the model.

sdo.setValueInModel(sys,pOpt);

Simulate the model.

sim(sys);

`param.Continuous` | `sdo.getParameterFromModel` | `sdo.optimize` | `sdo.OptimizeOptions` | `sdo.requirements.StepResponseEnvelope` | `sdo.SimulationTest` | `Simulink.SimulationData.SignalLoggingInfo`

Was this topic helpful?