I believe the issue is related to your initial point. If your dimensions are 1x2, then your initial point should be 1x2 as well. Since it is just 0, when you run the optimization, the first input tried is [0,0]. Your objective function returns an empty array is this scenario, which results in the error about the output not being scalar.
MaxSidelobe= FindBestPlacingGA([0 0])
MaxSidelobe =
0x1 empty double column vector
Use the dropdown menu to change the initial point from 0 to a variable containing your initial point. Just make sure that variable is defined before you run the optmiization task. I used a variable named input0.
One other difference I noticed. In my version of MATLAB, the patternsearch solver is not available, so I used fmincon. The solution is very different between these two solvers:
- patternsearch: [59.8330, 0.2899]
- fmincon: [0.6559, 0.5000]
function MaxSidelobe= FindBestPlacingGA(input)
input(1) = deg2rad(input(1));
mic_pos = [Array3N(input(1),input(2));mic_pos];
MaxSidelobe= FPSF_Function(mic_pos,500,0:1:80);
function mic_pos = Array3N(theta,rho)
theta3N = [theta+pi/2;theta+pi*7/6;theta+pi*11/6];
[mic_pos(:,1),mic_pos(:,2)] = pol2cart(theta3N,rho);
function MSL= FPSF_Function(mic_pos,f,El)
Num_mic = size(mic_pos,1);
K = zeros(3, numAz, numEl);
x = cos(az_rad) * sin(el_rad);
y = sin(az_rad) * sin(el_rad);
W(p,q) = exp(-1i*dot(K(:,p,q)'-k0,mic_pos(n,:))*2*pi*f/c) + W(p,q);
Y = 10*log10((abs(W)).^2);
local_max = imregionalmax(Y);
max_values = Y(local_max);
Mainlobe = max(max_values(:));
sidelobes = max_values(max_values~=Mainlobe);
MSL = Mainlobe - max(sidelobes(:));