Why do i receive the error:" unrecognized function or variable "Fun"?
Show older comments
% Particle Swarm Optimization
function [Y,X,cg_curve]=PSO(N,Max_iteration,lb,ub,dim,fhandle,fnonlin)
%PSO Infotmation
Vmax=6;
noP=N;
wMax=0.9;
wMin=0.2;
c1=2;
c2=2;
% Initializations
for k = 1 : N
Swarm.Particles(k).X = (ub-lb) .* rand(1,dim) + lb;
Swarm.Particles(k).V = zeros(1, dim);
Swarm.Particles(k).PBEST.X = zeros(1,dim);
Swarm.Particles(k).PBEST.O = inf;
Swarm.GBEST.X = zeros(1,dim);
Swarm.GBEST.O = inf;
end
cg_curve=zeros(1,Max_iteration);
%Main
for t = 1 : Max_iteration
% Calcualte the objective value
for k = 1 : N
currentX = Swarm.Particles(k).X;
Swarm.Particles(k).O = fun(fhandle,fnonlin,currentX);
% Update the PBEST
if Swarm.Particles(k).O < Swarm.Particles(k).PBEST.O
Swarm.Particles(k).PBEST.X = currentX;
Swarm.Particles(k).PBEST.O = Swarm.Particles(k).O;
end
% Update the GBEST
if Swarm.Particles(k).O < Swarm.GBEST.O
Swarm.GBEST.X = currentX;
Swarm.GBEST.O = Swarm.Particles(k).O;
end
end
% Update the X and V vectors
w = wMax - t .* ((wMax - wMin) / Max_iteration);
for k = 1 : N
Swarm.Particles(k).V = w .* Swarm.Particles(k).V + c1 .* rand(1,dim) .* (Swarm.Particles(k).PBEST.X - Swarm.Particles(k).X) ...
+ c2 .* rand(1,dim) .* (Swarm.GBEST.X - Swarm.Particles(k).X);
% Check velocities
index1 = find(Swarm.Particles(k).V > Vmax);
index2 = find(Swarm.Particles(k).V < -Vmax);
Swarm.Particles(k).V(index1) = Vmax;
Swarm.Particles(k).V(index2) = -Vmax;
Swarm.Particles(k).X = Swarm.Particles(k).X + Swarm.Particles(k).V;
% Check positions
index1 = find(Swarm.Particles(k).X > ub);
index2 = find(Swarm.Particles(k).X < lb);
Swarm.Particles(k).X(index1) = ub(index1);
Swarm.Particles(k).X(index2) = lb(index2);
end
cg_curve(t) = Swarm.GBEST.O;
end
X=Swarm.GBEST.X;
Y=Swarm.GBEST.O;
end
Reference: Grey Wold Optimizer (GWO) source codes version 1.0
The code was developed on R2011b(7.13), i tried to use it to learn more about PSO but came out as error:
unrecognized function or variable "Fun"
Error in PSO:
Swarm.Particles(k).O = fun(fhandle,fnonlin,currentX);
1 Comment
ScottB
on 12 Jan 2024
Kevin,
There isn't a native ML function called "fun". Is it possible you mean "funm"? It evaluates general matrix function.
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!