Conversion to double from pid2 is not possible-optimization app
Mostra commenti meno recenti
I have written the following code for optimizing ki, kp and kd values for pid controller using genatic algorithm. but the error come" conversion to double from pid2 is not possible".
code is:
function[j]= pid2(x)
s=tf('s');
plant= 1/ (s^2 + 3*s + 11);
kp=x(1)
ki= x(2)
kd=x(3)
cont= kp+ ki/s + kd*s
step(feedback(plant*cont,1));
dt=0.01;
t=0:dt:1;
err=1-step(feedback(plant*cont,1),t);
j=sum(t'.*abs(err)*dt)
end
please guide me, i am new in matlab.
Risposte (1)
I ran your code using the ga() tool, and it produced a set of PID gains that enable the controller to track the reference signal. It seems that there might have been a syntax error in the input argument of the ga() command in your previous attempt.
%% Tune PID controller via Genatic Algorithm
lb = [75 300 25]; % lower bound
ub = 2*lb; % upper bound
[k, fval] = ga(@pid2, 3, [], [], [], [], lb, ub)
s = tf('s');
P = 1/(s^2 + 3*s + 11);
kp = k(1);
ki = k(2);
kd = k(3);
C = pid(kp, ki, kd)
G = feedback(C*P, 1);
step(P), hold on
step(G), grid on
legend('Original Plant', 'Compensated System', 'location', 'East')
%% Maybe your previous attempt
[k, fval] = ga(@(k) @pid2, 3, [], [], [], [], lb, ub)
%% Cost function: Integral Time-weighted Absolute Error (ITAE)
function J = pid2(k) % I usually choose J = costfun(k)
s = tf('s');
P = 1/(s^2 + 3*s + 11); % Plant
kp = k(1);
ki = k(2);
kd = k(3);
C = kp + ki/s + kd*s; % Classical PID Controller
dt = 0.01;
t = 0:dt:1;
err = 1 - step(feedback(C*P, 1), t); % Error
J = sum(t'.*abs(err)*dt); % ITAE
end
Categorie
Scopri di più su Agriculture in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
