First, fill in the transfer function of the device to be controlled, then determine the PID function. Next, use the unit feedback closed-loop transfer function formula to calculate the closed-loop function. Based on the ITAE optimal function, accurately determine the PID coefficients, and then use a pre-filter to cancel the zeros of the closed-loop transfer function. Check that the poles of the system’s transfer function are all in the left half of the complex plane, then call lsim() to generate the output and observe the overall system’s output signal.
%% Example of PID controller design %%
G1(s)=1/(s^2+5*s+6); %% Transfer function %%
PID(s)=(K3*s*s+K1*s+K2)/s; %%%% PID function %%
G3(s)=PID(s)*G(s)/(1+PID(s)*G(s)); %% Closed-loop function %%
G3(s)=(K3*s*s+K1*s+K2)/(s^3+(2+K3)*s^2+(6+K1)*s+K2); %% Closed-loop function %%
clear all; close all; clc; %%%%%%%%%%%%%%%
%% System transfer function %%
j_1=sqrt(-1);
G1=tf(1,conv([1, 2], [1, 3])); %% Best polynomial %%
Best1=[1, 1.75*w1, 2.15*w1*w1, w1*w1*w1]; %% (s^3+(2+K3)*s^2+(6+K1)*s+K2); %% Closed-loop function numerator %%
Ts=0.5; %% Rise time %%
Ts=4/(zeta*w1); %% Desired damping ratio zeta=0.7; %% Characteristic frequency w1=4/Ts/zeta;
K2=w1*w1*w1; %% K2=w1*w1*w1;
K3=1.75*w1-2; %% (2+K3)*s^2=1.75*w1*s^2;
K1=2.15*w1*w1-6; %% (6+K1)*s=2.15*w1*w1*s
PID=tf([K3, K1, K2],[1, 0]); %% PID adjustment after closed-loop function %%
G3Num=[K3, K1, K2]; %% Closed-loop function numerator: [K3*s*s+K1*s+K2]
G3Den=[1, 2+K3, 6+K1, K2]; %% s^3+(2+K3)*s^2+(6+K1)*s+K2 %%
%% Pre-filter %%
Gq=tf(K2, G3Num); %% sysGood=Gq*feedback(PID*G1,-1); %% Overall system transfer function: pre-filter - PID adjustment - object
sysGood=tf(K2, G3Den);
den4=sysGood.Denominator();
den5=[den4{1}];
roots(den5);
timeStep=0.01;
w1=2; %% Continuous frequency %%
timeEnd=20;
Len=timeEnd/timeStep+1;
time1=[0:1:Len-1]*timeStep;
inX=ones(Len,1); %% 5.0*sin(2*pi*w1*time1); %% inX=inX';
[outY,t2]=lsim(sysGood,inX,time1); %%%%%%%%%%%%%%%%%%%%
figure(1); plot(t2,inX,'b',t2,outY,'r'); title('Input and Output Curve.'); xlabel('t(s)'); ylabel('y'); legend('Input x', 'Output y');
figure(2); plot(t2, outY-inX, 'b'); title('Output and Output Error.'); xlabel('t(s)'); ylabel('y-x'); legend('y-x');
disp('End.');