Why does the code always return "-ve" values?
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Torsten
2024-5-4
编辑:Torsten
2024-5-5
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)
[best,fmin]=fpa(psize,psw,iter,dim,lb,ub,@(b) myfun(b,u))
myfun(best,u)
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 个评论
Torsten
2024-5-5
"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)
myfun([u(1:3),-u(4)],u)
fun1(u,u)
fun1([u(1:3),-u(4)],u)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!