Hi everyone, how do I fix this issue/>>>>>>Array indices must be positive integers or logical values.

1 回表示 (過去 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 also need Y values in an array of 4###

回答 (1 件)

Image Analyst
Image Analyst 2023 年 6 月 4 日
Since this is one of the most common FAQs, see the FAQ for a thorough discussion:

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by