This example shows how to programmatically optimize controller parameters to meet step
response requirements using the
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.
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.
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.
The height of water in the tank,
H, must meet the following step
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,
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.
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 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.InitialValue correspond to a step change in the water tank
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 is an array of 2
To limit the parameters to positive values, set the minimum value of each parameter to
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.
edit sldo_model1_design to view this function.
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.
ans = 0.1739 0.0169 -0.0002 -0.0101 -0.0229 0.0073 -0.0031 0.0423
Cleq values are positive, beyond the specified tolerance, which
indicates the response using the current parameter values violates the design
Specify optimization options.
The software configures
opt to use the default optimization method,
fmincon, and the sequential quadratic programming algorithm for
Optimize the response.
At each optimization iteration, the software simulates the model and the default
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.
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 (Optimization Toolbox).
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
ans = -0.0001 -0.0028 -0.0050 -0.0101 -0.0135 -0.0050 -0.0050 -0.0732
All values satisfy
0 within the
optimization tolerances, which indicates that the step response requirements are
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
View the optimized parameter values.
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.
Simulate the model.