How to fix : Unable to perform assignment because the left and right sides have a different number of elements.

2 次查看(过去 30 天)
I am trying to use particle swarm optimization to solve a problem of mine.
And i get following error:
Unable to perform assignment because the left and right sides have a
different number of elements.
This is the initial code:
function output= fun(X)
g(:,1)=[2;2]; R1=X(:,1); R3=X(:,2); R2= R3-g; n=X(:,3);
Br1=1.2;Br2=1.2;mu=4.*pi.*10.^-7;R4=35;
Length=70; L=2.*pi.*sqrt((R4.^2+R1.^2)./2);
x=(Br1.*Br2.*L.*(10^-6))/(4.*pi.*mu);
a=Length./n;
b=R2-R1;
d=R4-R3;
h=R3-R2;
c=-10:0.5:0;
F=0;
I feel there is an issue with how i have defined the variables.
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-1-18
What is the full error and in which line does it occur?
Mention the code that is relevant to the line where the error occurs.
Supreeth D K
Supreeth D K 2023-1-18
This is the PSO code i have used and error occurs in line 152
clc;
clear;
close all;
%% Problem Definition
run=5;
for i=1:run
CostFunction = @(X)-fun(X); % maximizing Function
nVar = 3; % Number of parameter
VarSize = [1 nVar]; % Size of Decision Variables Matrix
VarMin = [10 0.5 1]; % Lower Bound of parameters
VarMax = [40 5.5 2.5]; % Upper Bound of parameters
%% PSO Parameters
MaxIt = 1000; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
% PSO Parameters
w = 1; % Inertia Weight
wdamp = 0.99; % Inertia Weight Damping Ratio
c1 = 1; % Personal Learning Coefficient
c2 = 1.5; % Global Learning Coefficient
% Velocity Limits
VelMax = 0.1*(VarMax-VarMin);
VelMin = -VelMax;
%% Initialization
empty_particle.Position = [];
empty_particle.Cost = [];
empty_particle.Velocity = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
particle = repmat(empty_particle, nPop, 1);
GlobalBest.Cost = inf;
for i = 1:nPop
% Initialize Position
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
particle(i).Velocity = zeros(VarSize);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update Personal Best
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
BestCost = zeros(MaxIt, 1);
%% PSO Main Loop
for it = 1:MaxIt
for i = 1:nPop
% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+c1*rand(VarSize).*(particle(i).Best.Position-particle(i).Position) ...
+c2*rand(VarSize).*(GlobalBest.Position-particle(i).Position);
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity, VelMin);
particle(i).Velocity = min(particle(i).Velocity, VelMax);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
% Velocity Mirror Effect
IsOutside = (particle(i).Position<VarMin | particle(i).Position>VarMax);
particle(i).Velocity(IsOutside) = -particle(i).Velocity(IsOutside);
% Apply Position Limits
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update Personal Best
if particle(i).Cost<particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost<GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
BestCost(it) = GlobalBest.Cost;
w = w*wdamp;
end
BestSol = GlobalBest;
disp(BestSol);
end
%% Results
figure;
%plot(BestCost, 'LineWidth', 2);
semilogy(BestCost, 'LineWidth', 2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2023-1-18
This is a simple error, it occurs when you try to save more number of elements into an array than it is initialized for.
Ex:
A = zeros(1,3) ;
A(1) = rand ; % filling one element in one position
A(2) = rand(1,2) ; % filling two elements in one position; not correct so error
Unable to perform assignment because the left and right sides have a different number of elements.

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by