Error: GA function - Too many output arguments

Hello, I am trying to use the ga function to obtain the PID controller gains to a step response in order to minimize the IAE (integral of the absolute error) and I am running the code below.The problem is that when I try to use the Optimization Tool, I always get the 'Too many output arguments' error message.
function myFitness(k)
syms s
SP = 1;
G = (2.156/(42.56*s + 1))*exp(-1.005*s)
C = (k(1) + (k(2)/s) + k(3)*s);
H = feedback(G*C,1);
[z,t] = step(SP*H);
error = abs(SP-z(end));
y = integral(error,0,1000)
end

2 Comments

@pbarros: how are you calling this fitness function? Please show the complete error message and the code where this error actually occurs.
Hi, I created the function so I could use it to obtain the controller gains using the Optimization Tool in which I set the Fitness function to '@myFitness' and the number of variables to '3'. The complete error message that I get from the Optimization Tool when I try to run the solver is the following: ' Optimization running. Error running optimization. Too many output arguments.'
In the command window there is the warning below but I did what it asked and nothing changed.
Warning: You are using 'mutationuniform' mutation function for constrained minimization.
Solution may be infeasible; use '@mutationadaptfeasible' function for constrained minimization.
> In constrValidate (line 76)
In gacommon (line 125)
In ga (line 336)
In callSolver (line 32)
In optimguirun (line 40)
In optimguiswitchyard (line 14)

Sign in to comment.

 Accepted Answer

There are several problems with your code, not the least of which is your invoking the Symbolic Math Toolbox that is inappropriate here.
See the documentation for the tf (link) function.
This should run. I leave it to you to determine that it produces the correct result:
function myFitness(k)
s = tf('s');
SP = 1;
G = (2.156/(42.56*s + 1))*exp(-1.005*s)
C = (k(1) + (k(2)/s) + k(3)*s);
H = feedback(G*C,1);
[z,t] = step(SP*H);
err_fcn = @(z) abs(SP-z(end));
y = integral(err_fcn,0,1000, 'ArrayValued',1)
end

8 Comments

pbarros
pbarros on 4 Mar 2018
Edited: pbarros on 4 Mar 2018
Thanks for the help but I am still getting a 'Too many output arguments' error.
My pleasure.
One thing you need to do is to return ‘y’ as the output of your ‘myFitness’ function. (I was concentrating on getting the code within the function to run correctly, and forgot about that.)
Change the function declaration to:
function y = myFitness(k)
Try that. Your ‘myFitness’ function then returns a scalar value, which is what ga wants, so that should work.
I think it worked now, the optimization is running without problems. Thank you very much! Have a nice day!
As always, my pleasure!
I have this unconstrained optimization running in the background:
function y = myFitness(k)
s = tf('s');
SP = 1;
G = (2.156/(42.56*s + 1))*exp(-1.005*s);
C = (k(1) + (k(2)/s) + k(3)*s);
H = feedback(G*C,1);
[z,t] = step(SP*H);
err_fcn = @(z) abs(SP-z(end));
y = integral(err_fcn,0,1000, 'ArrayValued',1);
end
opts = optimoptions('ga', 'PopulationSize',500, 'InitialPopulationMatrix',randi(100, 500, 3), 'PlotFcn',@gaplotbestindiv);
[B,fval,exitflag] = ga(@myFitness, 3, [],[],[],[],[],[],[],[], opts)
Currently, ‘k = [69; 9; 55]’. This may not be the final best individual. (I prefer large initial populations, since it essentially guarantees a successful solution.)
Did you get a result for the fitness function close to zero? I am using the Optimization Tool and I only get huge values. I think that I messed up the settings.
My ga run converged a few minutes ago. The results:
B =
69.0000e+000 9.0000e+000 56.0000e+000
fval =
499.0010e+003
exitflag =
1.0000e+000
In my code, ‘B = k’, so those are the final values. (I did not constrain it at all.)
I am not certain what you are doing, so I cannot suggest a specific approach. There are various techniques to ‘tune’ your feedback controller that are well known in control theory, pole placement being one. These are discussed in any book on modern control, such as C-T Chen’s excellent text, that uses the MATLAB Control System Toolbox extensively, and that I still use as a primary reference. I would pursue that option, unless you are constrained to use only ‘classical control’. I have not done anything in classical control in decades, so I cannot help you with that.
I am trying to get the best gains values ('k') that minimizes the error function (between the reference signal ('SP') and the step response 'z' function). I must have made a mistake somewhere because I think I should get a very small value for the 'fval'. Thank you for the help and I am sorry for all the confusion.
As always, my pleasure.
No worries!
A classical control approach may not be the best option here.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics 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!