Why doesn't my simple PSO work?

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?

3 个评论

Dear Daniel Caliari, you code seems correct, I cannot see the problem. What I would do in your position is eliminating the function handling notation, create a m-file function for each function you need; it might happen that overlapping the function handling as you did created a bug problem, it is just a possibility. I have worked with PSO with m-files, not function handling, and the examples I have seen used just one function handling. Try it, it might be a possibility.
Jorge - no, the problem is not in pso, or in the use of function handles that call function handles. The problem is improperly calling integral.
John D'Errico, you are right, integral is not a created function, rather an built in function; supposing it is the case. Hopefully that solves the problem!

请先登录,再进行评论。

 采纳的回答

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.

5 个评论

Dear John D'Errico, after a while studying your comment, I believe I finally got it. He needs to redefine the problem, since integral will not pass a vector as he needs. He needs to call integral as a function handler, as you have mentioned, not as a function, as he is doing. I have tried to adapt his code, but I believe he needs to reread what he was trying to model, I believe a PID control problem, and redefine the code. Notwithstanding, I found his code cumbersome to read.
Sorry about missing the error message, I realy forgot It. I had imagined that the integral was the problem, but I didn't have understand why It is. Now, I think I got It! Thank's for the atention and for the help Jorge Guerra Pires and John D'Errico!
Jorge Guerra Pires you said you found my code cumbersome to read. If It is not ask to much, could you give me some hints to improve It?
Dear Daniel Caliari, first of all, please, do not interpret me wrong, likely that is your first version of the code! You may do what I have suggested previously, divide the function handlers into actual function with m-File, that helps to find problem in the code. For finding the problem in your code, I have do run part by part, I have used an aux variable:
aux=@(V)abs(erro(V))*V(4);
ITAE = @(V)integral(aux(V),0,inf);%however here you need to can "aux" just, instead of "aux(V)", if you test aux alone, it works well and give the output for a vector 1x4, but the integral calls the function with one argument, that is "dx" within the domain 0 and inf. You may as well add comments, saying what you wanted to say, it can make easier for people to help you. Again, it was just a comment, no intention to offend you!
Best regards, Jorge Pires PS. please, let me know how you solved the problem, I believe you need to redefine the problem, however, you may find another way. Remember, you need to call the integral with just one argument; unless you can use triple integral, "integral3", which also is a problem, since you vector is of 4 arguments.
Dear Daniel Caliari,
A suggestion for future problems you might face is: run the code line by line, add comment on the code sections you do not want to run. You may run the code in the prompt comment. The important is isolating the problem. The matlab error message can be useful, however, it is a compiler message, not always logical and easy to read; I actually rarely use them. I hope that helps somehow. Best, Jorge Pires,

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by