Why does the code always return "-ve" values?

3 visualizaciones (últimos 30 días)
Sadiq Akbar
Sadiq Akbar el 4 de Mayo de 2024
Comentada: Sadiq Akbar el 6 de Mayo de 2024
When I run the main, it returns me all negative values though I have given only two negative values.
Explanaton: Inside main, I have provided only two negative values in vector u. But when I run it, it gives me either all negative values or three of them as negative values though the magnitudes are nearly the same but the signs are negative. Where does this negative sign come from?
All the three codes are attached. Just run main and see the result in the command window. The values of vector u are returned back in a variable best.

Respuestas (1)

Torsten
Torsten el 4 de Mayo de 2024
Editada: Torsten el 5 de Mayo de 2024
When I run the main, it returns me all negative values though I have given only two negative values.
Why not ? The objective is still 0 for the "new" solution. If you want to restrict the elements of the solution vector u, you must change the lb and/or ub vectors.
rng ("default")
clear;clc
u=[-33 -40 33 40];
psize=10;
psw=0.8;
iter=1000;
dim=length(u);
lb= -90*ones(1,dim);
ub= 90*ones(1,dim);
myfun(u,u)
ans = 0
[best,fmin]=fpa(psize,psw,iter,dim,lb,ub,@(b) myfun(b,u))
best = 1x4
-33.0000 -40.0000 33.0000 -40.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fmin = 2.0378e-13
myfun(best,u)
ans = 2.0378e-13
function er1 = myfun(b,u)
P=length(u);
M = P/2;
azu = [u(1:M)]';
elu = [u(M+1:end)]';
azb = [b(1:M)]';
elb = [b(M+1:end)]';
m = ones(M,1);
% Wavenumber vectors
ku = pi*[cosd(azu).*cosd(elu), sind(azu).*cosd(elu), sind(elu)].';
kb = pi*[cosd(azb).*cosd(elb), sind(azb).*cosd(elb), sind(elb)].';
N = 10; % Number of antennas
% uniform circular array
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
r = [rx, ry, zeros(N,1)];
Au = exp(-1j*r*ku);
Ab = exp(-1j*r*kb);
xu = Au*m;
xb = Ab*m;
% MSE
er = abs(xu-xb).^2;
er1 = mean(er,'all');
end
  7 comentarios
Torsten
Torsten el 5 de Mayo de 2024
"myfun" and "fun1" give different results when called.
The "u" that was a solution for "myfun" is not a solution for "fun1" (see below).
And you shouldn't say that you don't get correct results: as long as 0 is returned from the objective function and the solution satisfies the constraints, it is correct (in the mathematical sense). If it is not the solution that you want, you must adjust your objective function or your constraints.
clear;clc
u=[-33 -40 33 40];
psize=10;
psw=0.8;
iter=1000;
dim=length(u);
lb= -90*ones(1,dim);
ub= 90*ones(1,dim);
myfun(u,u)
ans = 0
myfun([u(1:3),-u(4)],u)
ans = 0
fun1(u,u)
ans = 0
fun1([u(1:3),-u(4)],u)
ans = 0.3242
function er1 = myfun(b,u)
P=length(u);
M = P/2;
azu = [u(1:M)]';
elu = [u(M+1:end)]';
azb = [b(1:M)]';
elb = [b(M+1:end)]';
m = ones(M,1);
% Wavenumber vectors
ku = pi*[cosd(azu).*cosd(elu), sind(azu).*cosd(elu), sind(elu)].';
kb = pi*[cosd(azb).*cosd(elb), sind(azb).*cosd(elb), sind(elb)].';
N = 10; % Number of antennas
% uniform circular array
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
r = [rx, ry, zeros(N,1)];
Au = exp(-1j*r*ku);
Ab = exp(-1j*r*kb);
xu = Au*m;
xb = Ab*m;
% MSE
er = abs(xu-xb).^2;
er1 = mean(er,'all');
end
function er1=fun1(b,u)
f=1e9;
c=3e8;
l=c/f;
k=(2*pi)/l;
N=8;
n=0:N-1;
phi_n=2*pi*n/N;
phi_n = rad2deg(phi_n);
M=length(u);
d_circular=l/2;
circumference = N*d_circular;
a = circumference/2*pi;
AFo = exp(-1i*k*a*cosd(u-phi_n.'));
AFe = exp(-1i*k*a*cosd(b-phi_n.'));
% MSE
er = abs(AFo-AFe).^2;
er1 = mean(er,'all');
end
Sadiq Akbar
Sadiq Akbar el 6 de Mayo de 2024
Thanks a lot for your kind response. Tha'ts what I am saying. If a function returns zero, it doesn't mean that it gives correct answer. If you look at both these functions, both are correct but the problem is with the bounds I think. In myfun, the set of four values in u are not estimated simultaneously but in case of fun1, all the values are estimated simultaneously. It means if we can change the code of main associated with the myfun in such a way that the bounds are applied to two values of u in one time, then it may become correct but how? that is very difficult for me. I can't handle that. So if you can help me in this regard, it will be good.

Iniciar sesión para comentar.

Categorías

Más información sobre Surrogate Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by