Simulink Control Design 

This example shows how to use systune or looptune to automatically tune fixed structure control systems. Note that to define tuning goals and run systune and looptune commands, you need Robust Control Toolbox.
On this page… 

Controller Tuning with SYSTUNE 
For this example we use the following model of an engine speed control system:
open_system('scdspeedctrl')
The control system consists of a single PID loop and the PID controller gains must be tuned to adequately respond to step changes in the desired speed. Specifically, we want the response to settle in less than 5 seconds with little or no overshoot. While pidtune is a faster alternative for tuning a single PID controller, this simple example is well suited for an introduction to automated tuning using the systune and looptune commands. To start with a controller that is not tuned yet, set the PID Controller to be a pure integrator.
set_param('scdspeedctrl/PID Controller','P','0','I','0.01','D','0');
Controller Tuning with SYSTUNE
The slTuner interface provides a convenient gateway to systune for control systems modeled in Simulink. This interface lets you specify which blocks in the Simulink model are tunable and what signals are of interest for open or closedloop validation. Create an slTuner instance for the scdspeedctrl model and list the "PID Controller" block as tunable. Note that all Linear Analysis points in the model (signals "Ref" and "Output" here) are automatically available as points of interest for tuning.
ST0 = slTuner('scdspeedctrl','PID Controller');
The PID block is initialized with its value in the Simulink model, which you can access using getBlockValue. Note that the proportional and derivative gains are initialized to zero above.
getBlockValue(ST0,'PID Controller')
ans = 1 Ki *  s with Ki = 0.01 Name: PID_Controller Continuoustime Ionly controller.
Next create a reference tracking requirement to capture the target settling time. Use the signal names in the Simulink model to refer to the reference and output signals, and use a twosecond response time target to ensure settling in less than 5 seconds.
TrackReq = TuningGoal.Tracking('Ref','Output',2);
You can now tune the control system ST0 subject to the requirement TrackReq.
ST1 = systune(ST0,TrackReq);
Final: Soft = 1.07, Hard = Inf, Iterations = 115
The final value is close to 1 indicating that the tracking requirement is met. systune returns a "tuned" version ST1 of the control system described by ST0. Again use getBlockValue to access the tuned values of the PID gains:
getBlockValue(ST1,'PID Controller')
ans = 1 s Kp + Ki *  + Kd *  s Tf*s+1 with Kp = 0.00188, Ki = 0.0033, Kd = 0.000466, Tf = 0.000203 Name: PID_Controller Continuoustime PIDF controller in parallel form.
To simulate the closedloop response to a step command in speed, get the initial and tuned transfer functions from speed command "Ref" to "Output" speed and plot their step responses:
T0 = getIOTransfer(ST0,'Ref','Output'); T1 = getIOTransfer(ST1,'Ref','Output'); step(T0,T1) legend('Initial','Tuned')
Controller Tuning with LOOPTUNE
You can also use looptune to tune fixed structure control systems. The looptune workflow is very similar to the systune workflow. One difference is that looptune needs to know the boundary between the plant and controller, which is specified in terms of controls and measurements signals. For a single loop the performance is essentially captured by the response time, or equivalently by the openloop crossover frequency. Based on firstorder characteristics the crossover frequency should exceed 1 rad/s for the closedloop response to settle in less than 5 seconds. You can therefore tune the PID loop using 1 rad/s as target 0dB crossover frequency.
% Mark the signal "u" as a point of interest addPoint(ST0,'u') % Tune the controller parameters Control = 'u'; Measurement = 'Output'; wc = 1; ST1 = looptune(ST0,Control,Measurement,wc);
Final: Peak gain = 0.961, Iterations = 10 Achieved target gain value TargetGain=1.
Again the final value is close to 1, indicating that the target control bandwidth was achieved. As with systune, use getIOTransfer to compute and plot the closedloop response from speed command to actual speed. The result is very similar to that obtained with systune.
T0 = getIOTransfer(ST0,'Ref','Output'); T1 = getIOTransfer(ST1,'Ref','Output'); step(T0,T1) legend('Initial','Tuned')
You can also perform openloop analysis, for example, compute the gain and phase margins at the plant input u.
% Note: 1 because margin expects the negativefeedback loop transfer L = getLoopTransfer(ST1,'u',1); margin(L), grid
Once you are satisfied with the systune or looptune results, you can upload the tuned controller parameters to Simulink model for further validation with the nonlinear model.
writeBlockValue(ST1)
You can now simulate the engine response with the tuned PID controller.
The nonlinear simulation results closely match the linear responses obtained in MATLAB.
Comparison of PI and PID Controllers
Closer inspection of the tuned PID gains suggests that the derivative term contributes little because of the large value of the Tf coefficient.
showTunable(ST1)
Block 1: scdspeedctrl/PID Controller = 1 s Kp + Ki *  + Kd *  s Tf*s+1 with Kp = 0.000653, Ki = 0.00282, Kd = 0.0021, Tf = 46.7 Name: PID_Controller Continuoustime PIDF controller in parallel form.
This suggests using a simpler PI controller instead. To do this, you need to override the default parameterization for the "PID Controller" block:
setBlockParam(ST0,'PID Controller',ltiblock.pid('C','pi'))
This specifies that the "PID Controller" block should now be parameterized as a mere PI controller. Next retune the control system for this simpler controller:
ST2 = looptune(ST0,Control,Measurement,wc);
Final: Peak gain = 0.915, Iterations = 5 Achieved target gain value TargetGain=1.
Again the final value is less than one indicating success. Compare the closedloop response with the previous ones:
T2 = getIOTransfer(ST2,'Ref','Output'); step(T0,T1,T2,'r') legend('Initial','PID','PI')
Clearly a PI controller is sufficient for this application.