Conversion to double from pid2 is not possible-optimization app

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.

回答(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)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
k = 1×3
144.3332 529.2219 48.1111
fval = 4.2379e-04
s = tf('s');
P = 1/(s^2 + 3*s + 11);
kp = k(1);
ki = k(2);
kd = k(3);
C = pid(kp, ki, kd)
C = 1 Kp + Ki * --- + Kd * s s with Kp = 144, Ki = 529, Kd = 48.1 Continuous-time PID controller in parallel form.
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)
Conversion to double from function_handle is not possible.

Error in fcnvectorizer (line 19)
y(i,:) = feval(fun,(pop(i,:)));

Error in makeState (line 69)
Score = fcnvectorizer(state.Population(initScoreProvided+2:end,:),FitnessFcn,1,...

Error in galincon (line 24)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);

Error in ga (line 420)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...

Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
%% 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

类别

帮助中心File Exchange 中查找有关 Agriculture 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by