fminimax instead decrease increase my costfunct

조회 수: 1 (최근 30일)
koorosh dastan
koorosh dastan 2023년 12월 8일
편집: koorosh dastan 2023년 12월 8일
To better understand my code, I'll explain it section by section.
  1. Boundary selection:I divided my variable into two halves. The first half ranges from 0 to 1, and the second half ranges from -90 to 90.I chose not to begin the first half with 0, and I'll explain the reason later.
  2. Variable separation:I represented the first half of the variable using lb(1:elementNumb) = 0.000001. (if i put 0 it will be inf because of teta3)
  3. Concatenation:I concatenated the two halves of the variable
random_numbersw = ones(1,elementNumb);
ubb=90;
lbb=-90;
% random_numberst = lbb + (ubb + ubb) * rand(1, elementNumb); % Random number from -90 to 90
random_numberst = 12*(ones(1 , elementNumb));
x0 = [ random_numbersw,random_numberst ] ;
ub = zeros(1, elementNumb*2); % Initialize a matrix of zeros with ... elements
ub(1:elementNumb) = 1; % Set values from .. to .. to 0
ub(elementNumb+1:elementNumb*2) = ubb; % Set values from .. to .. to ..
lb = zeros(1, elementNumb*2); % Initialize a matrix of zeros with .. elements
lb(1:elementNumb) = 0.000001; % Set values from .. to .. to ..
lb(elementNumb+1:elementNumb*2) = lbb; % Set values from .. to .. to ..
Then I wrote the option and fminimax. I wrote several kinds of options for testing.
%%Fminimax
% options = optimoptions(@fminimax,'disp','iter','Diagnostics',...
% 'on','FiniteDifferenceType','central','FiniteDifferenceStepSize',0.6,'AbsoluteMaxObjectiveCount', 6 );
% options = optimoptions(@fminimax,'disp','iter','Diagnostics',...
% 'on', 'FiniteDifferenceStepSize',[0.1,5],'DiffMinChange',0.1,'MaxFunctionEvaluations',2,...
% 'FiniteDifferenceType','central','FiniteDifferenceStepSize',0.6);
% options = optimoptions(@fminimax,'disp','iter','Diagnostics',...
% 'on', 'FiniteDifferenceStepSize',1e-3,...
% 'FunctionTolerance',1e-9,'MaxFunctionEvaluations',20000,'FunValCheck','on');
options=optimset('disp','iter','LargeScale','off','Diagnostics',...
'on','TolFun',.00000000000001,'MaxIter',10000,'MaxFunEvals',1000000);
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[],options);
Then, I write my cost function.
There are some constant variables that are not important.
function y = costy(x)
freq = 9*10^9; %Freq
j = sqrt(-1); %Define Imaginary
l =(3*10^8)/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.5*l;
Each element in the cost function consists of two variables.
elementNumb = 34;
then beacuse i want to sweep my function in teta and want to minimize each area with various goal
step = 0.1;
teta1 = ((-80) :step: (-25));
teta2 = ((-25) :step: (9.6));
teta3 = ((10) :step: (15));
teta4 = ((15.4) :step: (50));
teta5 = ((50) :step: (60));
tetat = [teta1,teta2,teta3,teta4,teta5];
lenteta1 = length(teta1);
sumLenteta12 = length(teta1) + length(teta2);
sumLenteta123 = length(teta3) + sumLenteta12;
sumLenteta1234 = sumLenteta123 + length(teta4);
I wrote the main body of the cost function, which has five areas. The value of each element is represented by h, as I mentioned earlier. For each element, I have two variables, and the area that my cost function should sweep is between -80 and 60. In the end, I sum all of them so that all of my teta values are combined.
y = abs(sum(g,1));
here is my question
In teta3 I like to have a maximum in this area, so I inverted my function, but in this area, my cost function increases in each iteration. Isn't it supposed to decrease? Fminmax is supposed to minimize the cost function, right?
g = zeros(elementNumb,length(tetat));
for h = 1:elementNumb
%%teta1
for aa = 1:length(teta1)
g(h,aa) = g(h,aa)+(x(h) * ( exp(-j*(h-1) * (k*d*sind(teta1(aa)+x(h+elementNumb)))))); %w W
end
%%teta2
for bb=(lenteta1+1):(sumLenteta12)
g(h,bb) =g(h,bb)+( x(h) * exp(-j*(h-1) * (k*d*sind(teta2(bb-lenteta1)+x(h+elementNumb))))); %with W
end
%%teta3
for cc = (sumLenteta12+1):(length(teta3)+sumLenteta12)
g(h,cc) = g(h,cc)+(1/(( x(h) * exp(-j*(h-1) * (k*d*sind(teta3(cc-sumLenteta12)...
+x(h+elementNumb))))))); %w W
% disp(g(10,sumLenteta12+1))
end
%%teta4
for dd = (sumLenteta123+1):(sumLenteta123+length(teta4))
g(h,dd) = g(h,dd)+( x(h) * exp(-j*(h-1) * (k*d*sind(teta4(dd-(sumLenteta123))...
+x(h+elementNumb))))); %w W
end
%%teta5
for ee = (sumLenteta1234+1):(sumLenteta1234+length(teta5))
g(h,ee) = g(h,ee)+( x(h) * exp(-j*(h-1) * (k*d*sind(teta5(ee-(sumLenteta1234))...
+x(h+elementNumb))))); %w W
end
end
y = abs(sum(g,1));
end
here is the code (complete)
% close all
clear all
elementNumb=34;
%%
%%with W
% random_numbersw = rand(1,elementNumb); % Random number from 0 to 1
random_numbersw = ones(1,elementNumb);
ubb=90;
lbb=-90;
% random_numberst = lbb + (ubb + ubb) * rand(1, elementNumb); % Random number from -90 to 90
random_numberst = 12*(ones(1 , elementNumb));
x0 = [ random_numbersw,random_numberst ] ;
ub = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
ub(1:elementNumb) = 1; % Set values from 1 to 21 to 0
ub(elementNumb+1:elementNumb*2) = ubb; % Set values from 22 to 40 to 90
lb = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
lb(1:elementNumb) = 0.000001; % Set values from 1 to 21 to 0
lb(elementNumb+1:elementNumb*2) = lbb; % Set values from 22 to 40 to 90
%%
%%Fminimax
% options = optimoptions(@fminimax,'disp','iter','Diagnostics',...
% 'on','FiniteDifferenceType','central','FiniteDifferenceStepSize',0.6,'AbsoluteMaxObjectiveCount', 6 );
% options = optimoptions(@fminimax,'disp','iter','Diagnostics',...
% 'on', 'FiniteDifferenceStepSize',[0.1,5],'DiffMinChange',0.1,'MaxFunctionEvaluations',2,...
% 'FiniteDifferenceType','central','FiniteDifferenceStepSize',0.6);
% options = optimoptions(@fminimax,'disp','iter','Diagnostics',...
% 'on', 'FiniteDifferenceStepSize',1e-3,...
% 'FunctionTolerance',1e-9,'MaxFunctionEvaluations',20000,'FunValCheck','on');
options=optimset('disp','iter','LargeScale','off','Diagnostics',...
'on','TolFun',.00000000000001,'MaxIter',10000,'MaxFunEvals',1000000);
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[],options);
%%
%%function
function y = costy(x)
freq = 9*10^9; %Freq
j = sqrt(-1); %Define Imaginary
l =(3*10^8)/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.5*l; %Distant of each element
elementNumb = 34;
step = 0.1;
teta1 = ((-80) :step: (-25));
teta2 = ((-25) :step: (9.9));
teta3 = ((10) :step: (15));
teta4 = ((15.4) :step: (50));
teta5 = ((50) :step: (60));
tetat = [teta1,teta2,teta3,teta4,teta5];
lenteta1 = length(teta1);
sumLenteta12 = length(teta1) + length(teta2);
sumLenteta123 = length(teta3) + sumLenteta12;
sumLenteta1234 = sumLenteta123 + length(teta4);
g = zeros(elementNumb,length(tetat));
for h = 1:elementNumb
%%teta1
for aa = 1:length(teta1)
g(h,aa) = g(h,aa)+(x(h) * ( exp(-j*(h-1) * (k*d*sind(teta1(aa)+x(h+elementNumb)))))); %w W
end
%%teta2
for bb=(lenteta1+1):(sumLenteta12)
g(h,bb) =g(h,bb)+( x(h) * exp(-j*(h-1) * (k*d*sind(teta2(bb-lenteta1)+x(h+elementNumb))))); %with W
end
%%teta3
for cc = (sumLenteta12+1):(length(teta3)+sumLenteta12)
g(h,cc) = g(h,cc)+(-(( x(h) * exp(-j*(h-1) * (k*d*sind(teta3(cc-sumLenteta12)...
+x(h+elementNumb))))))); %w W
% disp(g(10,sumLenteta12+1))
end
%%teta4
for dd = (sumLenteta123+1):(sumLenteta123+length(teta4))
g(h,dd) = g(h,dd)+( x(h) * exp(-j*(h-1) * (k*d*sind(teta4(dd-(sumLenteta123))...
+x(h+elementNumb))))); %w W
end
%%teta5
for ee = (sumLenteta1234+1):(sumLenteta1234+length(teta5))
g(h,ee) = g(h,ee)+( x(h) * exp(-j*(h-1) * (k*d*sind(teta5(ee-(sumLenteta1234))...
+x(h+elementNumb))))); %w W
end
end
b = abs(sum(g,1));
y = [b(1:lenteta1),b(lenteta1+1:sumLenteta12),b(sumLenteta12+1:sumLenteta123) ...
,b(sumLenteta123+1:sumLenteta1234),b(sumLenteta1234+1:length(tetat))];
% w1 = 45;
% w2 = 65;
% w3 = 1; %%%%???????
% w4 = 45;
% w5 = 45;
%
% b=b/max(b);
% b=20*log10(abs(b));
% y=[(b(1:lenteta1)+w1),(b(lenteta1+1:sumLenteta12)+w2),(b(sumLenteta12+1:sumLenteta123)+w3) ...
% ,(b(sumLenteta123+1:sumLenteta1234)+w4),(b(sumLenteta1234+1:length(tetat))+w5)];
end
and here is another question if you have time after first please answer or maybe i open another topic for this one
assume the last line instead i write y = abs(sum(g,1));
i continue
b = abs(sum(g,1));
then
i write some w for each are and what they ll do i show you in this image
w1 = 45;
w2 = 65;
w3 = 1; %%%%????????
w4 = 45;
w5 = 45;
b=b/max(b);
b=20*log10(abs(b));
y=[(b(1:lenteta1)+w1),(b(lenteta1+1:sumLenteta12)+w2),(b(sumLenteta12+1:sumLenteta123)+w3) ...
,(b(sumLenteta123+1:sumLenteta1234)+w4),(b(sumLenteta1234+1:length(tetat))+w5)];
end
if you want to excute second question you should commentcomplete code these lines
b = abs(sum(g,1));
y = [b(1:lenteta1),b(lenteta1+1:sumLenteta12),b(sumLenteta12+1:sumLenteta123) ...
,b(sumLenteta123+1:sumLenteta1234),b(sumLenteta1234+1:length(tetat))];
and uncomment this one
% w1 = 45;
% w2 = 65;
% w3 = 1; %%%%???????
% w4 = 45;
% w5 = 45;
%
% b=b/max(b);
% b=20*log10(abs(b));
% y=[(b(1:lenteta1)+w1),(b(lenteta1+1:sumLenteta12)+w2),(b(sumLenteta12+1:sumLenteta123)+w3) ...
% ,(b(sumLenteta123+1:sumLenteta1234)+w4),(b(sumLenteta1234+1:length(tetat))+w5)];

답변 (0개)

카테고리

Help CenterFile Exchange에서 Test Model Components에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by