Learn how to model, tune, and simulate a closedβloop controller with this practical MATLAB PID controller tuning example that uses a DC-DC buck converter voltage loop.
Quick answer
To run a MATLAB PID controller tuning example, define your plant transfer function using the 'tf' command, then apply the 'pidtune' function to automatically compute proportional, integral, and derivative gains based on a target crossover frequency. For example, define a second-order plant G = tf([1], [1e-6, 1e-3, 1]), then execute [C, info] = pidtune(G, 'pid', 2*pi*1000) to design a controller with a 1 kHz bandwidth. Finally, verify stability using step(feedback(C*G, 1)).
Introduction to Plant Modeling in MATLAB
Feedback control is needed to keep voltage steady in powerβelectronics circuits. To show a practical MATLAB PID controller tuning example, we will design a voltageβloop controller for a DC-DC buck converter. The buck converter takes an unregulated input voltage and produces a lower, stable output voltage even when the load resistance or the input supply changes. When a buck converter works in continuous conduction mode (CCM), its dynamic behavior looks like a secondβorder lowβpass filter. The transfer function G(s) connects the dutyβcycle command to the output voltage.
This function depends on the inductance (L), the capacitance (C), the equivalent series resistance (ESR), and the load resistance (R). In this example, we use a simplified secondβorder transfer function, assuming the output voltage is controlled by changing the duty cycle.
For a closely related example, see Applying Ant Lion Optimization Technique to Enhance Power Converters Performance via Effective Controller Tuning.
Closed-Loop Voltage Control System Architecture
The signal flow of the tuned feedback loop, showing how the PID controller processes the voltage error to regulate the converter.
- 1Voltage Reference
Target output voltage setpoint (V_ref)
Input signal
- 2Error Detector
Subtracts measured output voltage from reference
Error = V_ref – V_out
- 3PIDF Controller
Applies tuned Kp, Ki, Kd, and derivative filter N
Computes control action
- 4Duty Cycle Saturation
Limits control output to physical range
Typically 0% to 95%
- 5Buck Converter Plant
Second-order LC filter with load resistance
Physical power stage
- 6Output Voltage Feedback
Measured voltage returned to error detector
Closed loop path
Defining the Transfer Function in MATLAB
Before we tune the controller, we need to create the plant transfer function in the MATLAB workspace with the Control System Toolbox. First, we set the basic parameters: an inductance L of 100 microhenries, a capacitance C of 220 microfarads, and a load resistance R of 10 ohms.
The buck converterβs continuousβtime transfer function is G(s) = 1 / (L*C*s^2 + (L/R)*s + 1). In the MATLAB command window, we enter these values and build the model using the βtfβ command. This objectβoriented model lets MATLAB run frequency and timeβdomain analyses.
For a closely related example, see Enhancing ride comfort of semi-active suspension through collaboration control using dung beetle optimizer optimized Fuzzy PID controller.
The MATLAB PID Controller Tuning Example Script
To perform the tuning, we use the automated 'pidtune' command. This function optimizes the proportional (Kp), integral (Ki), and derivative (Kd) gains to satisfy specified crossover frequency and phase margin targets. It also includes a first-order filter on the derivative term to suppress high-frequency sensor noise.
Below is the complete, executable MATLAB script to define the plant, tune the PID controller, construct the closed-loop system, and plot the transient step response:
% MATLAB PID Controller Tuning Example
% Step 1: Define physical parameters
L = 100e-6; % Inductance (H)
C = 220e-6; % Capacitance (F)
R = 10; % Load Resistance (Ohms)
% Step 2: Create the plant transfer function G(s)
num = [1];
den = [L*C, L/R, 1];
G = tf(num, den);
% Step 3: Define target loop bandwidth (rad/s) and tune
target_bandwidth = 2 * pi * 1500; % 1.5 kHz crossover frequency
[C_pid, info] = pidtune(G, 'pidf', target_bandwidth);
% Step 4: Construct the closed-loop transfer function T(s)
T = feedback(C_pid * G, 1);
% Step 5: Display calculated controller parameters
disp('Tuned PID Parameters:');
disp(C_pid);
disp('Tuning Info:');
disp(info);
% Step 6: Plot step response
figure;
step(T);
title('Closed-Loop Step Response of Buck Converter');
grid on;
For a closely related example, see An Adaptive Frequency Multi-Resonant Current Controller for Grid-Tied Single-Phase Cascaded H-Bridge Photovoltaic Inverters.
Effect of Tuning Parameters on System Response
Understanding how adjusting individual controller gains impacts the closed-loop performance helps during manual fine-tuning.
| Factor | Engineering view | Why it matters |
|---|---|---|
| Proportional Gain (Kp) Increase | Decreases rise time, increases overshoot, minor change in settling time, reduces steady-state error. | Primary speed adjustment |
| Integral Gain (Ki) Increase | Decreases rise time, increases overshoot, increases settling time, eliminates steady-state error. | Necessary for accuracy |
| Derivative Gain (Kd) Increase | Minor change in rise time, decreases overshoot, decreases settling time, no effect on steady-state error. | Improves stability |
| Filter Coefficient (N) Decrease | Reduces high-frequency noise amplification, can introduce phase lag if set too low. | Protects gate driver |
Analyzing the Tuned Controller Performance
Running the script gives you the controller gains and the main loop characteristics. Theβ―'pidf'β―controller type in the script is a PID controller that includes a derivative filter coefficient (N). The filter stops sudden spikes in the control signal that can be caused by measurement noise.
The returnedβ―'info'β―structure holds the achieved crossover frequency and the phase margin. In power systems, a phase margin of 45 to 60 degrees is usually desired because it gives a quick response with little overshoot. If the step response has too much ringing, you can change the phaseβmargin target by supplying a custom options structure to theβ―'pidtune'β―function withβ―'pidtuneOptions'.
Closed-Loop Frequency Domain Validation
Although the step response shows how the system behaves over time, we also need to check frequencyβdomain margins to be sure the system stays stable when conditions change. In MATLAB, we use the βmarginβ command to find the gain and phase margins of the open-loop system (C_pid * G). When we run `margin(C_pid * G)`, MATLAB displays a Bode plot with the gain and phase curves. The gain margin tells us how much we can increase the loop gain before the system becomes unstable. The phase margin tells us how much extra phase delay the system can tolerate.
For a buck converter, a gain margin larger than 6 dB gives confidence that the converter will keep working even as components age or temperature changes.
Transitioning from Script to Simulink Simulation
For complex electrical systems, engineers often move from MATLAB scripts to Simulink. You can bring the tuned controller values straight into the Simulink PID Controller block. In the blockβs dialog box, choose βExternalβ as the source, or point to the workspace variables C_pid.Kp, C_pid.Ki, C_pid.Kd, and C_pid.N.
Simulink also lets you add physical limits, such as dutyβcycle saturation (usually kept between 0 and 0.95 for a buck converter) and antiβwindup features. These nonlinear effects are hard to capture with pure transfer functions, but they are essential to prevent integrator windup during large transients.
Useful research starting points
Key takeaways
- The 'pidtune' command automatically calculates balanced controller gains based on a specified crossover frequency.
- A derivative filter (N) is essential in practical power converters to block high-frequency switching noise.
- Evaluating both step response and Bode margins ensures the system is stable under transient loads.
- Translating script-based designs to Simulink allows for the testing of physical constraints like duty cycle saturation.
Questions engineers often ask
What is the difference between PID and PIDF in MATLAB?
A regular PID controller includes an ideal derivative part, which can boost highβfrequency noise. A PIDF controller adds a firstβorder lowβpass filter to the derivative part. This filter reduces the gain at high frequencies, making the controller work well in real electrical systems where sensor noise is present.
How do I choose the target crossover frequency for a buck converter?
The target crossover frequency (bandwidth) should be chosen based on the switching frequency of the converter. A standard rule of thumb is to set the crossover frequency between 1/10th and 1/5th of the switching frequency to avoid switching ripple interference and ensure stable average-model behavior.
Why does my tuned closed-loop system oscillate in Simulink?
Oscillations in Simulink usually happen when the system has nonlinear effects that are missing from the linear MATLAB model. Typical reasons are dutyβcycle saturation, propagation delays, or integrator windup. Turn on antiβwindup in the Simulink PID block and see whether the phase margin is too low.
Can I use pidtune with a non-linear simulation model directly?
Can I use pidtune with a nonlinear simulation model directly?
The pidtune function works only with a linear plant model. If you need to tune a nonlinear Simulink model, first create a linear version of the system at the desired operating point. You can do this with the Linear Analysis Tool or by running the linmod command. The linearization gives you a transferβfunction model, and you use that model as input to pidtune to set up the controller.
Continue learning: matlab pid controller tuning example
Explore our guide on simulating power electronic converters to learn how to build complete closed-loop systems in MATLAB and Simulink.