필터 지우기
필터 지우기

Hi everyone. How can I fix this issue?>>>Array indices must be positive integers or logical values.

조회 수: 3 (최근 30일)
clear all
clc
% Initialization Phase
N = 4;%from paper
Xmin1 = -5;%define Xmin
Xmax1 = 3;%define Xmax
Xmin2 = -3;%define Xmin
Xmax2 = 7;%define Xmax
Xmin3 = -1;%define Xmin
Xmax3 = 8;%define Xmax
T = 100;%Define max iteration
%fitness1=zeros(1,T);
fitness=zeros(1,T);
bestfitness=zeros(1,T);
bestfitness(1,1)=0;
%Initializing Y1-Y4
Y1 = zeros(1, N);
Y1(1:4) = Xmin1 + rand(1, 4) * (Xmax1 - Xmin1);
disp(['Initial Y1 values: ' num2str(Y1(1:4))]);
Y2 = zeros(1, N);
Y2(1:4) = Xmin2 + rand(1, 4) * (Xmax2 - Xmin2);
disp(['Initial Y2 values: ' num2str(Y2(1:4))]);
Y3 = zeros(1, N);
Y3(1:4) = Xmin3 + rand(1, 4) * (Xmax3 - Xmin3);
disp(['Initial Y3 values: ' num2str(Y3(1:4))]);
Xbest1 = zeros(1, T);% Initialize Xbest as an array
Xbest2 = zeros(1, T);
Xbest3 = zeros(1, T);
%X1 = zeros(1, T);
%X2 = zeros(1, T);
%X3 = zeros(1, T);
Xbar1 = zeros(1, T);
Xbar2 = zeros(1, T);
Xbar3 = zeros(1, T);
X1 = Xmin1 + rand() * (Xmax1 - Xmin1);
X2 = Xmin2 + rand() * (Xmax2 - Xmin2);
X3 = Xmin3 + rand() * (Xmax3 - Xmin3);
%Xbest(1) = min(Y); % Assign the minimum fitness value of the 4 Intial values as XBest
fprintf('X1 = %f\n', X1);
fprintf('X2 = %f\n', X2);
fprintf('X3 = %f\n', X3);
fitness1= (X1(1,1) - 1).^2 + (X2(1) - 2).^2 + (X3(1) - 3).^2;
fprintf('fitness1 = %f\n', fitness1);
Xbest1(1)=X1;
Xbest2(1)=X2;
Xbest3(1)=X3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% While not max iteration
t = 2;
while t < T+1
%Measurement Phase
%for t = 1:N
d = rand(1, N);
%fprintf('d = %f\n', d);
for i = 1:N
if d(i) > 0.5
delta1 = (exp(-10 * t / T) * ((Xmax1 - Xmin1)) / 2);
delta2 = (exp(-10 * t / T) * ((Xmax2 - Xmin2)) / 2);
delta3 = (exp(-10 * t / T) * ((Xmax3 - Xmin3)) / 2);
Y1(t) = Xbest1(t-1) + ((-delta1) + rand() * (delta1- (-delta1)));
Y2(t) = Xbest2(t-1) + ((-delta2) + rand() * (delta2 - (-delta2)));
Y3(t) = Xbest3(t-1) + ((-delta3) + rand() * (delta3 - (-delta3)));
else
Y1(t) = Xbest1(t-1);
clear all
clc
% Initialization Phase
N = 4;%from paper
Xmin1 = -5;%define Xmin
Xmax1 = 3;%define Xmax
Xmin2 = -3;%define Xmin
Xmax2 = 7;%define Xmax
Xmin3 = -1;%define Xmin
Xmax3 = 8;%define Xmax
T = 100;%Define max iteration
%fitness1=zeros(1,T);
fitness=zeros(1,T);
bestfitness=zeros(1,T);
bestfitness(1,1)=0;
%Initializing Y1-Y4
Y1 = zeros(1, N);
Y1(1:4) = Xmin1 + rand(1, 4) * (Xmax1 - Xmin1);
disp(['Initial Y1 values: ' num2str(Y1(1:4))]);
Y2 = zeros(1, N);
Y2(1:4) = Xmin2 + rand(1, 4) * (Xmax2 - Xmin2);
disp(['Initial Y2 values: ' num2str(Y2(1:4))]);
Y3 = zeros(1, N);
Y3(1:4) = Xmin3 + rand(1, 4) * (Xmax3 - Xmin3);
disp(['Initial Y3 values: ' num2str(Y3(1:4))]);
Xbest1 = zeros(1, T);% Initialize Xbest as an array
Xbest2 = zeros(1, T);
Xbest3 = zeros(1, T);
%X1 = zeros(1, T);
%X2 = zeros(1, T);
%X3 = zeros(1, T);
Xbar1 = zeros(1, T);
Xbar2 = zeros(1, T);
Xbar3 = zeros(1, T);
X1 = Xmin1 + rand() * (Xmax1 - Xmin1);
X2 = Xmin2 + rand() * (Xmax2 - Xmin2);
X3 = Xmin3 + rand() * (Xmax3 - Xmin3);
%Xbest(1) = min(Y); % Assign the minimum fitness value of the 4 Intial values as XBest
fprintf('X1 = %f\n', X1);
fprintf('X2 = %f\n', X2);
fprintf('X3 = %f\n', X3);
fitness1= (X1(1,1) - 1).^2 + (X2(1) - 2).^2 + (X3(1) - 3).^2;
fprintf('fitness1 = %f\n', fitness1);
Xbest1(1)=X1;
Xbest2(1)=X2;
Xbest3(1)=X3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% While not max iteration
t = 2;
while t < T+1
%Measurement Phase
%for t = 1:N
d = rand(1, N);
%fprintf('d = %f\n', d);
for i = 1:N
if d(i) > 0.5
delta1 = (exp(-10 * t / T) * ((Xmax1 - Xmin1)) / 2);
delta2 = (exp(-10 * t / T) * ((Xmax2 - Xmin2)) / 2);
delta3 = (exp(-10 * t / T) * ((Xmax3 - Xmin3)) / 2);
Y1(t) = Xbest1(t-1) + ((-delta1) + rand() * (delta1- (-delta1)));
Y2(t) = Xbest2(t-1) + ((-delta2) + rand() * (delta2 - (-delta2)));
Y3(t) = Xbest3(t-1) + ((-delta3) + rand() * (delta3 - (-delta3)));
else
Y1(t) = Xbest1(t-1);
Y2(t) = Xbest2(t-1);
Y3(t) = Xbest3(t-1);
end
end
%end
% Estimation Phase
% Initial Estimation
for k=2:t
if Y1(t - N + 1) < Y1(t - N + 2)
Xbar1(k) = Y1(t - N + 1) + (t - N + 2) - Y1(t - N + 1) * rand();
else
Xbar1(k) = Y1(t - N + 2) + (t - N + 1) - Y1(t - N + 2) * rand();
end
if Y2(t - N + 1) < Y2(t - N + 2)
Xbar2(k) = Y2(t - N + 1) + (t - N + 2) - Y2(t - N + 1) * rand();
else
Xbar2(k) = Y2(t - N + 2) + (t - N + 1) - Y2(t - N + 2) * rand();
end
if Y3(t - N + 1) < Y3(t - N + 2)
Xbar3(k) = Y3(t - N + 1) + (t - N + 2) - Y3(t - N + 1) * rand();
else
Xbar3(k) = Y3(t - N + 2) + (t - N + 1) - Y3(t - N + 2) * rand();
end
end
for k = 3:N
Xbar1(k) = Xbar1(k - 1) + (1/k) * (Y1(t - N + k) - Xbar1(k - 1));
Xbar2(k) = Xbar2(k - 1) + (1/k) * (Y2(t - N + k) - Xbar2(k - 1));
Xbar3(k) = Xbar3(k - 1) + (1/k) * (Y3(t - N + k) - Xbar3(k - 1));
end
X1(t) = Xbar1(k); % Update X(t) as X(k)
X2(t) = Xbar2(k); % Update X(t) as X(k)
X3(t) = Xbar3(k); % Update X(t) as X(k)
%Evaluation Phase
% Fitness evaluation
fitness(1,t) = (X1(t) - 1).^2 + (X2(t) - 2).^2 + (X3(t) - 3).^2;
fprintf('fitness = %f\n', fitness(1,1));
% Update bestfitness and Xbest
if fitness(1,t) > bestfitness(1,t-1)
bestfitness(1,t) = fitness(1,t);
Xbest1(1,t) = X1(1,t);
Xbest2(1,t) = X2(1,t);
Xbest3(1,t) = X3(1,t);
else
bestfitness(1,t) = fitness(1,t-1);
Xbest1(1,t) = Xbest1(1,t-1);
Xbest2(1,t) = Xbest1(1,t-1);
Xbest3(1,t) = Xbest1(1,t-1);
end
t=t+1;
end
Xbest1;
Xbest2;
Xbest3; + (t - N + 1) - Y1(t - N + 2) * rand();
end
if Y2(t - N + 1) < Y2(t - N + 2)
Xbar2(k) = Y2(t - N + 1) + (t - N + 2) - Y2(t - N + 1) * rand();
else
Xbar2(k) = Y2(t - N + 2) + (t - N + 1) - Y2(t - N + 2) * rand();
end
if Y3(t - N + 1) < Y3(t - N + 2)
Xbar3(k) = Y3(t - N + 1) + (t - N + 2) - Y3(t - N + 1) * rand();
else
Xbar3(k) = Y3(t - N + 2) + (t - N + 1) - Y3(t - N + 2) * rand();
end
end
for k = 3:N
Xbar1(k) = Xbar1(k - 1) + (1/k) * (Y1(t - N + k) - Xbar1(k - 1));
Xbar2(k) = Xbar2(k - 1) + (1/k) * (Y2(t - N + k) - Xbar2(k - 1));
Xbar3(k) = Xbar3(k - 1) + (1/k) * (Y3(t - N + k) - Xbar3(k - 1));
end
X1(t) = Xbar1(k); % Update X(t) as X(k)
X2(t) = Xbar2(k); % Update X(t) as X(k)
X3(t) = Xbar3(k); % Update X(t) as X(k)
%Evaluation Phase
% Fitness evaluation
fitness(1,t) = (X1(t) - 1).^2 + (X2(t) - 2).^2 + (X3(t) - 3).^2;
fprintf('fitness = %f\n', fitness(1,1));
% Update bestfitness and Xbest
if fitness(1,t) > bestfitness(1,t-1)
bestfitness(1,t) = fitness(1,t);
Xbest1(1,t) = X1(1,t);
Xbest2(1,t) = X2(1,t);
Xbest3(1,t) = X3(1,t);
else
bestfitness(1,t) = fitness(1,t-1);
Xbest1(1,t) = Xbest1(1,t-1);
Xbest2(1,t) = Xbest1(1,t-1);
Xbest3(1,t) = Xbest1(1,t-1);
end
t=t+1;
end
Xbest1;
Xbest2;
Xbest3;
Error in Untitled (line 150)
if Y1(t - N + 1) < Y1(t - N + 2)
I need the values for Y as 4 in an array##

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2023년 6월 4일
Error Here
>> t - N + 1
ans =
-1
hence in MTALAB Y1(-1) is not valid statement, all indices must be real & positive number only
(1,2,3,4,5 or any based on the array length) not Y1(0) or Y1(-1) etc
>> t - N + 2
ans =
0
>> whos Y2
Name Size Bytes Class Attributes
Y2 1x4 32 double
>>
Please check the if condition indices data
  댓글 수: 1
JESHURUN AUGUSTINE
JESHURUN AUGUSTINE 2023년 6월 4일
>> t - N + 1
##Formula is used as in the article below
DOI: 10.1109/ACCESS.2017.2777894
Is there anything wrong with the codes in the paper that I should know of
Pseudocode 2 Generation of Initial Estimation, X¯(2) at k=2
if Y(tN+1)<Y(tN+2)
X¯(2)=rand(U[Y(tN+1),Y(tN+2)])
else
X¯(2)=rand(U[Y(tN+2),Y(tN+1)])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by