PID controller in matlab code

I'm having trouble using a PID controller in a MATLAB script. I don't have the transfer function typically used with PID in MATLAB, but I do have the output error signal. I would like to connect this error signal to the PID code to improve the error correction.
For example, if I have this data (from the black system). I want to reduce the (maximum overshoot , rise time , settling time , ...) by using a PID controller .
clear all
clc
load("Vo_vn.mat")
load("x.mat")
Volt = Vo_vn;
Time = x;
OUTS = stepinfo(Volt,Time);
Desire = 23.5;
Error = Vo_vn-Desire;
RiseT = OUTS.RiseTime
RiseT = 1.3248e-04
SettT = OUTS.SettlingTime
SettT = 0.0037
OVERSH = (OUTS.Peak)-Desire
OVERSH = 14.3081
plot(Time,Volt)

4 Commenti

Sam Chak
Sam Chak circa un'ora fa
If you don't have the system transfer function in the traditional sense, where does the output error signal come from in the MATLAB script? The output signal must be generated from some system, either a transparent mathematical model or a data-driven black box. The PID controller, pid() function, itself does not require a transfer function to operate, unless you are referring to tuning the PID controller, which I understand you implied is intended to “improve the error correction.”
mohammed hussein
mohammed hussein circa 17 ore fa
Modificato: mohammed hussein circa 2 ore fa
Dear Sam Chak, thank you very much for your response. My output is derived from a data-driven black box. I have improved my equestion based on your feedback.
You first need to estimate a transfer function that fits the step response data. This is a topic in system identification, and you can use the tfest() function to address this type of problem. Otherwise, you can also intelligently guess the coefficients if you are familiar with the underlying system.
clear all
clc
load("Vo_vn.mat")
load("x.mat")
Volt = Vo_vn;
Time = x;
OUTS = stepinfo(Volt,Time);
Desire = 23.5;
Error = Vo_vn-Desire;
RiseT = OUTS.RiseTime
RiseT = 1.3248e-04
SettT = OUTS.SettlingTime
SettT = 0.0037
OVERSH = (OUTS.Peak)-Desire
OVERSH = 14.3081
% Estimated model
s = tf('s');
w = 8e3; % natural frequency (guess)
K = w^2; % stiffness coeff
zet = 0.16; % damping ratio (guess)
D = 2*zet*w; % damping coeff
Gp = K/(s^2 + D*s + K); % 2nd-order transfer function
figure
step(Gp, Time(end)) % step response of model
hold on
plot(Time, Volt/Desire, 'r-') % normalized data
legend('estimated model', 'black box data')
hold off
ylim([0 1.6])
Thank you for your suggestion

Accedi per commentare.

Risposte (1)

I revisited the problem, and you can perhaps use this as a mini guide. The manually estimated model is not 100% accurate, but it is relatively close. This model is then used by pidtune() to automatically tune the PID gains using a user-defined phase margin, an approach based on a concept you should already be familiar with in electrical engineering.
load("Vo_vn.mat")
load("x.mat")
Volt = Vo_vn;
Time = x;
OUTS = stepinfo(Volt,Time);
Desire = 23.5;
Error = Vo_vn-Desire;
RiseT = OUTS.RiseTime
RiseT = 1.3248e-04
SettT = OUTS.SettlingTime
SettT = 0.0037
OVERSH = (OUTS.Peak)-Desire
OVERSH = 14.3081
% Estimated model
s = tf('s');
w = 8725; % natural frequency (guess)
K = w^2; % stiffness coeff
zet = 0.158; % damping ratio (guess)
D = 2*zet*w; % damping coeff
Gp = K/(s^2 + D*s + K); % 2nd-order transfer function
figure
step(Desire*Gp, Time(end)) % step response of model
hold on
plot(Time, Volt, 'r-') % black box data
title('System Identification stage: Step Response')
legend('estimated model', 'black box data')
hold off
ylim([0 40])
% Auto-tuned PID Controller
opt = pidtuneOptions('PhaseMargin', 75, 'DesignFocus', 'reference-tracking');
Gc = pidtune(Gp, 'PIDF', opt)
Gc = 1 s Kp + Ki * --- + Kd * -------- s Tf*s+1 with Kp = 7.02, Ki = 2.35e+04, Kd = 0.000514, Tf = 2.08e-07 Continuous-time PIDF controller in parallel form.
% Closed-loop system
Gcl = minreal(feedback(Gc*Gp, 1))
Gcl = 1.884e11 s^2 + 2.568e15 s + 8.604e18 --------------------------------------------------------- s^4 + 4.804e06 s^3 + 2.017e11 s^2 + 2.934e15 s + 8.604e18 Continuous-time transfer function.
% Plot results
figure
step(Desire*Gcl, 5e-3), grid on % simulated compensated system (not actual black box system)
hold on
plot(Time, Volt, 'r-') % black box data
title('Controller Design stage: Step Response')
legend('simulated compensated system', 'uncompensated black box output')
hold off
ylim([0 40])

Richiesto:

il 22 Mag 2026 alle 6:03

Risposto:

circa 3 ore fa

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by