showing "too many output arguments" error in this code

1 次查看(过去 30 天)
function [J] = pid_optim(x)
s = tf('s');
plant = 1 / (s^2 + 10*s + 20);
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;
e = 1 - step(feedback(plant*cont,1),t);
J = sum(t'.*abs(e)*dt);

回答(1 个)

ag
ag 2023-11-3
Hi Keerthana,
I understand that you are getting “too many output arguments” error while implementing genetic algorithm.
I tried to run the function provided by you, and it ran without any errors. Try to modify the caller function to rectify the error.
Below is the full working code:
s = tf('s');
Gp = 1 / (s^2 + 10*s + 20);
step(Gp, 2), grid on
nvars = 3;
K = ga(@pid_optim, nvars)
Gc = K(1) + K(2)/s + K(3)*s; % ideal type
Gcl = feedback(Gc*Gp, 1);
step(Gcl, 2), grid on
S = stepinfo(Gcl)
function [J] = pid_optim(x)
s = tf('s');
plant = 1 / (s^2 + 10*s + 20);
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;
e = 1 - step(feedback(plant*cont,1),t);
J = sum(t'.*abs(e)*dt);
end
Please refer to the below resources for more details:
Hope this helps!
Best Regards,
Aryan Gupta

类别

Help CenterFile Exchange 中查找有关 PID Controller Tuning 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by