Matlab_Simulink: PID gains optimization program_input consideration (free program release)

What we want to achieve

  • Construction of a PID control simulator using Simulink and m-files
  • Optimization of PID gains for the above program
  • Optimization using fminsearch
  • Optimization to prevent excessive input

Matlab version

Matlab2021a
*Past versions also available as downloadable formats

Toolbox

Download URL

PID_Control_fmin_input - Google drive

*Download the folder for the Matlab version you are using.
 Example: If you are using the 2021a version, select "2021a" from the link above.
*We are not responsible for any problems caused by the use of the above programs.

Folder Structure

Download and extract the above.

How to run the program

Run "Main_PID_fmin_input.m"

m-file code (Main_PID_fmin_input.m)

%% Initialization
clc % Initialize command window
clear % Initialize workspace
close all % Close all graphs

%% Variable Declaration
% ===== Controlled system =====
K = 1;
A = 1;
B = 1;
C = 1;
% ===== reference signal =====
r_val = 10; % step reference signal
% ===== Simulink =====
N_File = 'PID_fmin.slx'; % simulink filne name
FinalTime = 30; % Simulation end time [s]
SamplingTime = 0.1; % Sampling time [s]
% ===== Estimated system gain =====
K_hat = 0.1*K; % It is used in fminsearch

%% fminsearch
ini_x = [0.5 0.5, 0.5]; % Initial PID gains
[x, fval] = fminsearch(@(x) func_fmin_input(x,N_File,K_hat),ini_x);

%% Run the Simulink
open(N_File); % Open Simulink file
sim(N_File); % Run the Simulink

%% Output Figures
% ===== Store the Simulink data =====
t = ScopeData.time; % Time
y = ScopeData.signals(1).values(:, 1); % Output
r = ScopeData.signals(1).values(:, 2); % Reference signal
u = ScopeData.signals(2).values(:, 1); % Input
% ===== output figures =====
% ----- y -----
figure;
subplot(211);
plot(t,y);
hold on;
plot(t,r,'--');
grid on;
xlabel('$ t {\rm [s]} $', 'interpreter', 'latex');
ylabel('$ y(t) $', 'interpreter', 'latex');
legend('$ y(t) $', '$ r(t) $', 'interpreter', 'latex');
% ----- u -----
subplot(212);
plot(t,u);
hold on;
grid on;
xlabel('$ t {\rm [s]} $', 'interpreter', 'latex');
ylabel('$ u(t) $', 'interpreter', 'latex');

m-file code (func_fmin_input.m)

function f = func_fmin_input(x,N_File,K_hat)
%% Variable Declaration
Kp = x(1);
Ki = x(2);
Kd = x(3);
assignin('base','Kp',Kp);
assignin('base','Ki',Ki);
assignin('base','Kd',Kd);
% Run the Simulink
sim(N_File);

%% Store the Simulink data
t = ScopeData.time; % Time
y = ScopeData.signals(1).values(:, 1); % Output
r = ScopeData.signals(1).values(:, 2); % Reference signal
u = ScopeData.signals(2).values(:, 1); % Input

%% 評価規範
% To make the same order between error and input, intrduce a K_hat
f = sum*1.^2);

%% output optimization results in command window
fprintf('[Kp, Ki, Kd, fval] = %.3g, %.3g, %.3g, %.3g\n',Kp, Ki, Kd, f);

Simulation result

Discussions

【Objective function】
 f = \sum_{t=0}^{N} ( ( r(t)-y(t) )^2 + \hat{K}(u_{final}-u(t))^2 )

where  \hat{K} is the estimated system gain,  u_{final} is the input value in steady state.

*1:r-y).^2 + (K_hat.*(u(end)-u