This example shows how to programmatically optimize controller
parameters to meet step response requirements using the sdo.optimize
function.
The Simulink^{®} model watertank_stepinput
includes
the nonlinear Water-Tank System plant and a PI controller in a single-loop
feedback system.
The Step block applies a step input. You can also use other types of input, such as a ramp, to optimize the response generated by such inputs.
This figure shows the Water-Tank System.
Water enters the tank at the top at a rate proportional to the valve opening. The valve opening is proportional to the voltage, V, applied to the pump. The water leaves through an opening in the tank base at a rate that is proportional to the square root of the water height, H. The presence of the square root in the water flow rate results in a nonlinear plant.
The following table describes the variables, parameters, differential equations, states, inputs, and outputs of the Water-Tank System.
Variables | H is the height of water in the tank. Vol is the volume of water in the tank. V is the voltage applied to the pump. |
Parameters | A is the cross-sectional area of the tank. b is a constant related to the flow rate into the tank. a is a constant related to the flow rate out of the tank. |
Differential equation | $$\frac{d}{dt}Vol=A\frac{dH}{dt}=bV-a\sqrt{H}$$ |
States | H |
Inputs | V |
Outputs | H |
The height of water in the tank, H
, must
meet the following step response requirements:
Rise time less than 2.5 seconds
Settling time less than 20 seconds
Overshoot less than 5%
Open the Simulink model.
sys = 'watertank_stepinput';
open_system(sys);
Log the water level, H
.
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 '/Water-Tank System']; 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 = 20; StepResp.PercentOvershoot = 5; StepResp.FinalValue = 2; StepResp.InitialValue = 1;
StepResp
is a sdo.requirements.StepResponseEnvelope
object.
The values assigned to StepResp.FinalValue
and StepResp.InitialValue
correspond
to a step change in the water tank height from 1
to 2
.
When you optimize the model response, the software modifies parameter (design variable) values to meet the design requirements.
Select model parameters to optimize. Here, optimize the parameters of the PID controller.
p = sdo.getParameterFromModel(sys,{'Kp','Ki'});
p
is an array of 2 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;
Create a design function to evaluate the system performance for a set of parameter values.
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.
Tip
Type |
Evaluate the current response. (Optional)
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.1739 0.0169 -0.0002 -0.0101 -0.0229 0.0073 -0.0031 0.0423
Some Cleq
values are positive, beyond the
specified tolerance, which indicates the response using the current
parameter values violates the design requirements.
Specify optimization options.
opt = sdo.OptimizeOptions
;
opt.MethodOptions.Algorithm = 'sqp';
The software configures opt
to use the default
optimization method, fmincon
, and the sequential
quadratic programming algorithm for fmincon
.
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 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 0.1739 1 10 0 0.03411 1 0.81 2 15 0 0 0.235 0.0429 3 15 0 0 2.26e-18 0 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.0028 -0.0050 -0.0101 -0.0135 -0.0050 -0.0050 -0.0732
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 that 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: 2.0545 Minimum: 0 Maximum: Inf Free: 1 Scale: 1 Info: [1x1 struct] pOpt(2,1) = Name: 'Ki' Value: 0.3801 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