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

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.

Answers (1)

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 Comments

Thanks a lot for your kind response. No that's not the solution because still it gives -ve values for which values in u are +ve.
Torsten
Torsten on 4 May 2024
Edited: Torsten on 4 May 2024
No that's not the solution because still it gives -ve values for which values in u are +ve.
Yes, but why do you think it shouldn't ? You allowed all values to vary between -90 and 90. And your function doesn't compare u with b, but xu with xb.
Thanks a lot for your kind response. It's not so. Each time I take vector u and run main and it gives me the values of u in the command window. For example see these new files in the attachment and run the main1 and see the result in the command window. Why does it give correct result?
Don't you understand that your function returns the same value er1 if the last entry in u is replaced by -u ?
And that thus both [-33 -40 33 40] and [-33 -40 33 -40] solve your optimization ?
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
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
Yes, you are right but I don't want this. If you run the main1 in 2nd set of new flles attached, you will see that it gives correct result. I want correct result like this.
What is my doubt: Inside you, I have taken four values for which the bounds are generated. But inside myfun 2 of them are taken and given to azu i.e., azu = [u(1:M)]' and likewise 2 of them are given to elu like elu = [u(M+1:end)]' because they are different.
But I am generating all the four in the same range, so this may be the reason. But how to tackle this issue, I don't know.
"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
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.

Sign in to comment.

Categories

Asked:

on 4 May 2024

Commented:

on 6 May 2024

Community Treasure Hunt

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

Start Hunting!