How to illustrate the dynamic system output for a PID controller system?
9 views (last 30 days)
Show older comments
I'm trying to make a PID Controller with cstr in it for my homework in matlab environment, I've managed to come up with something promising but whenI try to illustrate the x3 output, it just scrambles everything and after 26th iteration it doesn't even evaluate(bc numbers too big or small).It should control my output in a specified range with my reference, control and error signal (r(n), u(n), e(n) respectively) but i think it doesn' do that. I'm open to any help or idea. Thanks in advance.
clear; clc; close all;
% Initialization
Kp0 = 1.5;
Ki0 = 0.1;
Kd0 = 0.25;
u=0:1;
% CSTR system parameters
Ts = 0.1;
Da1 = 3;
Da2 = 0.5;
Da3 = 1;
x1(1:5) = 0.5;
x2(1:5) = 0.5;
x3(1:5) = 1.5;
d2 = 1;
t = 1:20;
r = 0.45 + 0.15*sin(2*pi*(1/200)*t);
for n = 3:length(r)
% Compute error
e(n) = r(n) - x3(n);
% Update PID gains
Kp(n+1) = Kp0 + e(n);
Ki(n+1) = Ki0 + e(n);
Kd(n+1) = Kd0 + e(n);
% PID Control Signal
u(n) = u(n-1) + Kp(n) * (e(n) - e(n-1)) + Ki(n) * e(n) + Kd(n) * (e(n) - 2*e(n-1) + e(n-2));
% Saturate Control Signal
if u(n) > max(u)
u(n) = max(u);
elseif u(n) < min(u)
u(n) = min(u);
end
% Apply u to CSTR system
k1x1 = Ts*f1_func(x1(n), x2(n));
k1x2 = Ts*f2_func(x1(n), x2(n), d2, u(n));
k1x3 = Ts*f3_func(x2(n), x3(n), d2);
k2x1 = Ts*f1_func(x1(n) + k1x1/2, x2(n) + k1x2/2);
k2x2 = Ts*f2_func(x1(n) + k1x1/2, x2(n) + k1x2/2, d2, u(n));
k2x3 = Ts*f3_func(x2(n) + k1x2/2, x3(n) + k1x3/2, d2);
k3x1 = Ts*f1_func(x1(n) + k2x1/2, x2(n) + k2x2/2);
k3x2 = Ts*f2_func(x1(n) + k2x1/2, x2(n) + k2x2/2, d2, u(n));
k3x3 = Ts*f3_func(x2(n) + k2x2/2, x3(n) + k2x3/2, d2);
k4x1 = Ts*f1_func(x1(n) + k3x1, x2(n) + k3x2);
k4x2 = Ts*f2_func(x1(n) + k3x1, x2(n) + k3x2, d2, u(n));
k4x3 = Ts*f3_func(x2(n) + k3x2, x3(n) + k3x3, d2);
x1(n + 1) = x1(n) + 1/6 * (k1x1 + 2*k2x1 + 2*k3x1 + k4x1);
x2(n + 1) = x2(n) + 1/6 * (k1x2 + 2*k2x2 + 2*k3x2 + k4x2);
x3(n + 1) = x3(n) + 1/6 * (k1x3 + 2*k2x3 + 2*k3x3 + k4x3);
% Plotting
figure(1);
subplot(3,1,1);
plot(x3, 'r');
title('x3 - r');
subplot(3,1,2);
plot(r);
end
0 Comments
Answers (2)
Jon
on 8 Jan 2024
Unless you are modeling some sort of adaptive control (and I don't think you are) then your PID gains Kp,Kd,Ki should be constant. So you should not update them inside of the loop. So I suggest removing the following code
Kp(n+1) = Kp0 + e(n);
Ki(n+1) = Ki0 + e(n);
Kd(n+1) = Kd0 + e(n);
3 Comments
Sam Chak
on 9 Jan 2024
It seems beneficial to consolidate the three function files into a unified cstr() file, encompassing the three differential equations. Additionally, note that the adaptive control signal 'u' is absent in the f3_func() file. Please modify your custom RK4 algorithm to accommodate the cstr() function file effectively.
%% Continuous Stirred-Tank Reactor
function dxdt = cstr(t, x)
dxdt = zeros(3, 1);
% Parameters
Da1 = 3;
Da2 = 0.5;
Da3 = 1;
d2 = 1;
% Adaptive PID Controller
u = adaptivePID(t, x);
% Differential equations
dxdt(1) = 1 - x(1) - Da1*x(1) + Da2*x(2)^2;
dxdt(2) = - x(2) + Da1*x(1) - Da2*x(2)^2 - Da3*d2*x(2)^2 + u;
dxdt(3) = - x(3) + Da3*d2*x(2)^2 + u;
end
Jon
on 10 Jan 2024
Even if it is an adaptive controller, it would be surprising to me if your adaption algorithm, which increases the gains based upon adding the current error would lead to a stable controller. Where did you get the idea that you should adapt the gains in this way. Is this your idea, or is it from some reference.
I also see that you have posted something above that makes this look like it is an exam question. If so, then you should be sure that it is consistent with your academic code of conduct to be getting external assistance completing the work. At the very least you should be sure to inform your professor, that you have used MATLAB answers to help you, and detail which part of the work is your own, and which part you got from MATLAB answers.
In my opinion MATLAB answers should not be used for help with Exam questions. It may be ok for homework, if you are making an honest effort to complete the assignment yourself, and are learning from the assistance given on MATLAB answers, and use of MATLAB answers for homework is acceptable to your professor.
Sam Chak
on 10 Jan 2024
It appears that a simple PI controller can regulate the output to the desired reference signal.
global cume
cume = 0; % initial cumulative error
tspan = linspace(0, 20, 2001);
x0 = [0.5; 0.5; 1.5]; % initial state values
[t, x] = ode45(@cstr, tspan, x0);
plot(t, x(:,3)), grid on, xlabel('Time'), title('y = x_{3}(t)')
%% Continuous Stirred-Tank Reactor
function [dxdt, u] = cstr(t, x)
global cume
dxdt = zeros(3, 1);
% Parameters
Da1 = 3;
Da2 = 0.5;
Da3 = 1;
d2 = 1;
% Reference
r = 1; % 0.45 + 0.15*sin(2*pi*(1/200)*t);
% Error signal
e = r - x(3);
% PI Controller
kp = 0.75;
ki = 0.1;
kd = 0.25; % unused
cume = cume + e; % cumulative error
u = kp*e + ki*cume;
% Differential equations
dxdt(1) = 1 - x(1) - Da1*x(1) + Da2*x(2)^2;
dxdt(2) = - x(2) + Da1*x(1) - Da2*x(2)^2 - Da3*d2*x(2)^2 + u;
dxdt(3) = - x(3) + Da3*d2*x(2)^2 + u;
end
0 Comments
See Also
Categories
Find more on PID Controller Tuning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!