Why doesn't my simple PSO work?
Show older comments
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 Comments
Jorge Guerra Pires
on 31 Jan 2017
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.
John D'Errico
on 31 Jan 2017
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.
Jorge Guerra Pires
on 31 Jan 2017
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!
Accepted Answer
More Answers (0)
Categories
Find more on Particle Swarm 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!