should cost function be only one point?
1 view (last 30 days)
Show older comments
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 Comments
Sam Chak
on 17 Dec 2023
@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
J = myCost(x) % returns one scalar value as expected
%% Test 2: Dastan 64-variable Cost function
x = ones(1, 64) % supply 64 values to the Cost function
J = costy(x) % cannot return one scalar value
%% 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
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!