MATLAB: Doesn’t the simple PSO work

optimizationpso

Hi!
I tried to run the following code but I am getting constantly errors:
clear all
clc
g = @(V)-( 6.3223*V(4)^2 + 18*V(4) + 12.812 )/V(4);
PID = @(V)V(1) + V(2)/V(4) + V(3)*V(4);
entrada = @(V)heaviside(V(4));
H = @(V)PID(V)*g(V);
erro = @(V)entrada(V)/(H(V) + 1);
ITAE = @(V)integral(abs(erro(V))*V(4),0,inf);
nvar = 4;
rng default
options = optimoptions('particleswarm','SwarmSize',100,'MaxIterations',100);
lb = [0 0 0 -inf];
ub = [100 100 100 inf];
[x,fval] = particleswarm(ITAE, nvar,lb ,ub, options);
The error says that the first argument should be a function handle. I verified the datatype using the command 'whos' and it says it's a fucntion handle!! What I am doing wrong?

Best Answer

Sigh. Is this not the 5'th time or so you have asked this question?
When you try to run an optimization, or ANYTHING like this, first verify that your function runs when you pass it an input.
ITAE([1 2 3 4])
Error using integral (line 82)
First input argument must be a function handle.
Error in @(V)integral(abs(erro(V))*V(4),0,inf)
READ THE ERROR MESSAGE. What does it tell you?
Is this an error from pso? NO!
What is getting upset? The integral function! Look to see what generated the error.
So look at your definiotn of ITAE.
ITAE = @(V)integral(abs(erro(V))*V(4),0,inf);
Is the first argument to integral a function handle? No.
Yes, MATLAB knows that ITAE is a function handle. But that is not your problem, since it is not where the error came from. Is the first argument of integral a function handle?
Note: this is also the reason why it is imperative that when you get an error message, that you post the ENTIRE text of the error message. Don't just tell us your contraction of that message, telling us all that you think is significant. Provide the ENTIRE error message. In this case, if someone had actually seen the complete error message, they would have told you immediately where to look. Instead, you forced me to execute your code. This is why it took so long to get the simple answer to your problem. Of course, had you simply thought clearly about what the error message was telling you, you could have saved a great deal of time. So learn to read the error messages that you see. They do try to tell you where to look.
Related Question