Helo everyone, I want to ask about my error in matlab. Error: File: Untitled5.m Line: 57 Column: 56 -> (rand(1,nv​ar).*c1.*(​pbest(pop,​:)x(pop,:)​)+...) Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or

1 visualización (últimos 30 días)
maxpop=100; % maximum population
maxiter=1000; % maximum iteration
numtry=10; % number of trial runs
bestAll=zeros(numtry,2);
bestxAll=zeros(numtry,4); %two variable x y
function [record,gbestfit,gbest]=PSO(maxpop,maxiter)
% initial parameter
ub=[2 3]; % upper-bound variables
lb=[0 0]; % lower-bound variables
w=0.5;c1=2;c2=2; % pso parameters
vmax=(ub-lb)*0.8; % damping limit 80% of search space
pc=0.1; % crazy particle probability
nvar=length(ub); % number of variables
%initialize the population
x=zeros(maxpop,nvar);
fit=zeros(maxpop,1);
for i=1:maxpop
x(i,:)=rand(1,nvar).*(ub-lb)+lb; %randomize between ub and lb
fit(i,:)=objfunc(x(i,:),1); % evaluate fitness function
end
v=rand(maxpop,nvar).*ones(maxpop,nvar)*2-ones(maxpop,nvar); % randomize first velocity between -1 and 1
pbest=x; % has no history so the best is the first one
pbestfit=fit; % has no history so the best is the first one
[gbestfit,index]=max(pbestfit); % find gbest by comparing pbest
gbest=pbest(index,:);
record=zeros(maxiter,1);
for iter=1:maxiter
for pop=1:maxpop
% update velocity
v(pop,:)=w*v(pop,:)+...
rand(1,nvar).*c1.*(pbest(pop,:)x(pop,:))+...
rand(1,nvar).*c2.*(gbest-x(pop,:));
% velocity damping
indexover=find(abs(v(pop,:))>vmax); % find the velocity that exceed vmax
v(pop,indexover)=v(pop,indexover)./abs(v(pop,indexover)).*vmax(1,indexover); %damp that velocity
% update location
if rand() > pc % normal
x(pop,:)=x(pop,:)+v(pop,:);
else % crazy particle
x(pop,:)=x(pop,:)+rand()*v(pop,:)./abs(v(pop,:)).*vmax; % crazy particle velocity can be + or -
end
% fix out of boundary particles
indexlb=find(x(pop,:)<lb); % find index that is less than lower bound
indexub=find(x(pop,:)>ub); % find index that is more than upper bound
x(pop,indexlb)=lb(1,indexlb)+abs(x(pop,indexlb)-lb(1,indexlb)); % the particle bounces off the lower boundary
x(pop,indexub)=ub(1,indexub)-abs(x(pop,indexub)-ub(1,indexub)); % the particle bounces off the upper boundary
% calculate fitness
fit(pop,1)=objfunc(x(pop,:),iter); % evaluate fitness function
% find pbest
if fit(pop,1)>pbestfit(pop,1) % update pbest if fitness is better
pbestfit(pop,1)=fit(pop,1);
pbest(pop,:)=x(pop,:);
end
% find gbest
if fit(pop,1)>gbestfit % update gbest if fitness is better
gbestfit=fit(pop,1);
gbest=x(pop,:);
end
end
record(iter,1)=gbestfit; % record for plotting convergence history
end
end
Helo everyone, I want to ask about my error in matlab.
Error: File: Untitled5.m Line: 57 Column: 56 -> (rand(1,nvar).*c1.*(pbest(pop,:)x(pop,:))+...)
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error.
To construct matrices, use brackets instead of parentheses.
How I can solve thats problem? thank you

Respuestas (1)

Stephan
Stephan el 4 de Mayo de 2021
Editada: Stephan el 4 de Mayo de 2021
Line 36:
for iter=1:maxiter
for pop=1:maxpop
% update velocity
v(pop,:)=w*v(pop,:)+...
rand(1,nvar).*c1.*(pbest(pop,:)x(pop,:))+...
% ^
% |
% ---> Missing '*' or '+' or...
%
rand(1,nvar).*c2.*(gbest-x(pop,:));

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by