should cost function be only one point?

조회 수: 1 (최근 30일)
koorosh dastan
koorosh dastan 2023년 12월 15일
댓글: Sam Chak 2023년 12월 17일
should cost function be only one point?
assume I have numerous points to minimize, where I can change the values of these points. For example, I want to sweep from 90 to -90 with a step of 0.2 or 0.1, etc. Is it better to try an algorithm that attempts to minimize and maximize all points, or should I write a cost function and then try to work on it? Below, you can see a part of the code related to this.
or i must only have one point ????
be
first approach (alot of point)
and pls attention in teta3 here (where i like my func be max try to inverse it) in the end
(1./b(sumLenteta12+1:sumLenteta123))
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.67*l; %Distant of each element
elementNumb = 34;
step = 0.5;
teta1 = ((-70) :step: (-40));
teta2 = ((-40) :step: (6));
teta3 = ((11) :step: (15));
teta4 = ((18) :step: (45));
teta5 = ((45) :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));
ww=90;
for h = 1:elementNumb
%%teta1
fo
g(h,aa) = g(h,aa)+(x(h) * ( exp(j*(h-1) * (k*d*sind(teta1(aa)+(ww*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)+(ww*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)...
+(ww*x(h+elementNumb)))))); %w W
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))...
+(ww*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))...
+(ww*x(h+elementNumb)))))); %w W
end
% disp([x(1),x(2),x(3),x(35),x(36),x(50)]);
end
%for weighting
w1 = 0;
w2 = 0;
w3 = 0;
w4 = 0;
w5 = 0;
b = abs(sum(g));
% b=b/max(b);
% b=20*log10(abs(b));
% for each point evaluate
b = [b(1:lenteta1)+w1,b(lenteta1+1:sumLenteta12)+w2,(1./b(sumLenteta12+1:sumLenteta123))+w3 ...
,b(sumLenteta123+1:sumLenteta1234)+w4,b(sumLenteta1234+1:length(tetat))+w5];
y = b;
end
and one point here i get from each one max then try to minimize it
and attention here i add minus for max (teta3)
b3 = -max(b(sumLenteta12+1:sumLenteta123))+w3 ;
and this one wants more time
and formula which i use
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.67*l; %Distant of each element
elementNumb = 34;
step = 0.5;
teta1 = ((-70) :step: (-40));
teta2 = ((-40) :step: (6));
teta3 = ((11) :step: (15));
teta4 = ((18) :step: (45));
teta5 = ((45) :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));
ww=90;
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)+(ww*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)+(ww*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)...
+(ww*x(h+elementNumb)))))); %w W
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))...
+(ww*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))...
+(ww*x(h+elementNumb)))))); %w W
end
% disp([x(1),x(2),x(3),x(35),x(36),x(50)]);
end
w1 = 0;
w2 = 0;
w3 = 0;
w4 = 0;
w5 = 0;
b = abs(sum(g));
% b=b/max(b);
% b=20*log10(abs(b));
%% for each point evaluate
% b = [b(1:lenteta1)+w1,b(lenteta1+1:sumLenteta12)+w2,(1./b(sumLenteta12+1:sumLenteta123))+w3 ...
% ,b(sumLenteta123+1:sumLenteta1234)+w4,b(sumLenteta1234+1:length(tetat))+w5];
% y = b;
%% for one point evaluate
b1 = max(b(1:lenteta1))+w1;
b2 = max(b(lenteta1+1:sumLenteta12))+w2;
b3 = -max(b(sumLenteta12+1:sumLenteta123))+w3 ;
b4 = max(b(sumLenteta123+1:sumLenteta1234))+w4;
b5 = max(b(sumLenteta1234+1:length(tetat)))+w5;
totb=b1+b2+b3+b4+b5;
y = totb;
end
  댓글 수: 3
koorosh dastan
koorosh dastan 2023년 12월 16일
편집: koorosh dastan 2023년 12월 17일
thank you for your attention
i explain in below topic more about code
but there my code has some mistake like i should scale 90 to -90 and ...
i have 64 variavble in my function (x is my variable)
here is my complete code and if you like you can change my option i think itsnt good option
% close all
clear all
t0 = tic();
elementNumb=34;
%%
%%with W
% random_numbersw = rand(1,elementNumb); % Random number from 0 to 1
random_numbersw = ones(1,elementNumb);
ubb=1;
lbb=-1;
% random_numberst = lbb + (ubb + ubb) * rand(1, elementNumb); % Random number from -90 to 90
% random_numberst = 0.134*(ones(1 , elementNumb));
random_numberst =-0.145*(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.03125; % 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,'MaxFunctionEvaluations', 1e6,'FunctionTolerance',1e-15, ...
% 'MaxIterations',1e3,'OptimalityTolerance',1e-10, 'MaxSQPIter',inf,'ConstraintTolerance',1e-9,'FunValCheck', 'on');
options = optimoptions(@fminimax,'MaxFunctionEvaluations', 1e6,'FunctionTolerance',1e-15, ...
'MaxIterations',1e4,'OptimalityTolerance',1e-10,'ConstraintTolerance',1e-9,'FunValCheck', 'on');
[x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[],options);
dt = toc(t0)
beep;
beep;
%% with non linear constraint
% [x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,@nonlcon,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.67*l; %Distant of each element
elementNumb = 34;
step = 0.5;
teta1 = ((-55) :step: (-25));
teta2 = ((-40) :step: (9));
teta3 = ((11) :step: (15));
teta4 = ((18) :step: (22));
teta5 = ((22) :step: (55));
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));
ww=90;
for h = 1:elementNumb
%%teta1
for aa = 1:length(teta1)
% g(h,aa) = g(h,aa)+(1 * ( exp(j*(h-1) * (k*d*sind(teta1(aa)+deltaTeta(h)))))); %w/o W
g(h,aa) = g(h,aa)+(x(h) * ( exp(j*(h-1) * (k*d*sind(teta1(aa)+(ww*x(h+elementNumb))))))); %w W
end
%%teta2
for bb=(lenteta1+1):(sumLenteta12)
% g(h,bb) =(g(h,bb)+( 1 * exp(-j*(h-1) * (k*d*sind(teta2(bb-lenteta1)+deltaTeta(h))))))-0.001; %w/o W
g(h,bb) =g(h,bb)+( x(h) * exp(j*(h-1) * (k*d*sind(teta2(bb-lenteta1)+(ww*x(h+elementNumb)))))); %with W
end
%%teta3
for cc = (sumLenteta12+1):(length(teta3)+sumLenteta12)
% g(h,cc) = (g(h,cc)+( 1 * exp(j*(h-1) * (k*d*sind(teta3(cc-sumLenteta12)+deltaTeta(h))))))+0.01; %w/o W
g(h,cc) = g(h,cc)+ (x(h) * exp(j*(h-1) * (k*d*sind(teta3(cc-sumLenteta12)...
+(ww*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)+( 1 * exp(j*(h-1) * (k*d*sind(teta4(dd-(sumLenteta123))...
% +deltaTeta(h)))))+0.01; %w/o W
g(h,dd) = g(h,dd)+( x(h) * exp(j*(h-1) * (k*d*sind(teta4(dd-(sumLenteta123))...
+(ww*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))...
+(ww*x(h+elementNumb)))))); %w W
end
% disp([x(1),x(2),x(3),x(35),x(36),x(50)]);
end
w1 = 0;
w2 = 0;
w3 = 0;
w4 = 0;
w5 = 0;
b = abs(sum(g));
% b=b/max(b);
% b=20*log10(abs(b));
%% for each point evaluate
% b = [b(1:lenteta1)+w1,b(lenteta1+1:sumLenteta12)+w2,(1./b(sumLenteta12+1:sumLenteta123))+w3 ...
% ,b(sumLenteta123+1:sumLenteta1234)+w4,b(sumLenteta1234+1:length(tetat))+w5];
% y = b;
%% for one point evaluate
b1 = max(b(1:lenteta1))+w1;
b2 = max(b(lenteta1+1:sumLenteta12))+w2;
b3 = max(b(sumLenteta12+1:sumLenteta123))+w3;
b4 = max(b(sumLenteta123+1:sumLenteta1234))+w4;
b5 = max(b(sumLenteta1234+1:length(tetat)))+w5;
totb=b1+b2-b3+b4+b5;
y = totb;
end
and if you want to see result you can excute below code
you should open below code in another page and after excute previous code you get x
% function y = costy(teta)
elementNumb=34;
Function definitions in a script must appear at the end of the file.
Move all statements after the "costy" function definition to before the first local function definition.
deltaTeta=x(1:elementNumb*2);
f=9*10^9;
j=sqrt(-1);
l=(3*10^8)/f;
k=(2*pi)/l;
d=0.67*l;
% xas=90;
% teta=((-1*xas):0.1:(1*xas));
teta=((-1*90):0.1:(1*90));
% deltaTeta=zeros(elementNumb)-11;
y=0;
% w=ones(elementNumb);
for h=1:elementNumb
% y=y+exp(j*(h-1)*(k*d*sind(teta)));
y=y+deltaTeta(h)*exp(j*(h-1)*(k*d*sind(teta+90*deltaTeta(h+elementNumb))));
end
y=y/max(y);
% end
figure;
%
plot(teta,20*log10(abs(y)),'o')
%
axis([-100 100 -70 2]);
Sam Chak
Sam Chak 2023년 12월 17일
@koorosh dastan. yes, the cost function should return only one scalar value. Before solving any optimization problem, I typically analyze and test my designed cost function to ensure it operates as expected. A simple example using a Bivariate Quadratic Function is provided below.
%% Test 1: Bivariate Quadratic Function
x = ones(1, 2) % supply 2 values to the Cost function
x = 1×2
1 1
J = myCost(x) % returns one scalar value as expected
J = 0
%% Test 2: Dastan 64-variable Cost function
x = ones(1, 64) % supply 64 values to the Cost function
x = 1×64
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
J = costy(x) % cannot return one scalar value
Index exceeds the number of array elements. Index must not exceed 64.

Error in solution>costy (line 42)
g(h,aa) = g(h,aa)+(x(h) * ( exp(j*(h-1) * (k*d*sind(teta1(aa)+(ww*x(h+elementNumb))))))); %w W
%% My 2-variable Cost function
function J = myCost(x)
J = (x(1) - 1)^2 + (x(2) - 1)^2; % Bivariate Quadratic Function
end
%% Dastan 64-variable Cost 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.67*l; %Distant of each element
elementNumb = 34;
step = 0.5;
teta1 = -70:step:-40; % θ1 range
teta2 = -40:step:6; % θ2 range
teta3 = 11:step:15; % θ3 range
teta4 = 18:step:45; % θ4 range
teta5 = 45:step:60; % θ5 range
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));
ww = 90;
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)+(ww*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)+(ww*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) +(ww*x(h+elementNumb)))))); %w W
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)) + (ww*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)) + (ww*x(h+elementNumb)))))); %w W
end
% disp([x(1),x(2),x(3),x(35),x(36),x(50)]);
end
w1 = 0;
w2 = 0;
w3 = 0;
w4 = 0;
w5 = 0;
b = abs(sum(g));
% b=b/max(b);
% b=20*log10(abs(b));
%% for each point evaluate
% b = [b(1:lenteta1)+w1,b(lenteta1+1:sumLenteta12)+w2,(1./b(sumLenteta12+1:sumLenteta123))+w3 ...
% ,b(sumLenteta123+1:sumLenteta1234)+w4,b(sumLenteta1234+1:length(tetat))+w5];
% y = b;
%% for one point evaluate
b1 = max(b(1:lenteta1))+w1;
b2 = max(b(lenteta1+1:sumLenteta12))+w2;
b3 = -max(b(sumLenteta12+1:sumLenteta123))+w3 ;
b4 = max(b(sumLenteta123+1:sumLenteta1234))+w4;
b5 = max(b(sumLenteta1234+1:length(tetat)))+w5;
totb = b1 + b2 + b3 + b4 + b5;
y = totb;
end

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

답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by