Optimization Live Editor task Error "Your objective function must return a scalar value"
Show older comments
Hi,I'm trying to maximize a function with genetic algorithm or patternsearch using Optimization Live Editor task. But it confuses me that an Error "Your objective function must return a scalar value" always occurs, and I have alreay checked out the output of my objective function. Can somebody tell me how to fix this problem? Would appreciate any help!

I checked out the the output of my objective function as follows:
input = [0 0.5];
MaxSidelobe = FindBestPlacingGA(input);
TF = isscalar(MaxSidelobe);
disp(TF);

The objective function and other functions needed:
function MaxSidelobe= FindBestPlacingGA(input)
input(1) = deg2rad(input(1));
mic_pos = [0 0.24 0
-0.2078 -0.12 0
0.2078 -0.12 0];
mic_pos = [Array3N(input(1),input(2));mic_pos];
MaxSidelobe= FPSF_Function(mic_pos,500,0:1:80);
end
function mic_pos = Array3N(theta,rho)
theta3N = [theta+pi/2;theta+pi*7/6;theta+pi*11/6];
mic_pos = zeros(3,3);
mic_pos(:,3) = 0;
[mic_pos(:,1),mic_pos(:,2)] = pol2cart(theta3N,rho);
end
function MSL= FPSF_Function(mic_pos,f,El)
Num_mic = size(mic_pos,1);
Az = -180:1: 180;
c = 343;
k0 = [0 0 -1];
numAz = length(Az);
numEl = length(El);
K = zeros(3, numAz, numEl);
for i = 1:numAz
for j = 1:numEl
az_rad = deg2rad(Az(i));
el_rad = deg2rad(El(j));
x = cos(az_rad) * sin(el_rad);
y = sin(az_rad) * sin(el_rad);
z = cos(el_rad);
K(:, i, j) = [x; y; z];
end
end
W = zeros(numAz,numEl);
for p = 1:numAz
for q = 1:numEl
for n = 1:Num_mic
W(p,q) = exp(-1i*dot(K(:,p,q)'-k0,mic_pos(n,:))*2*pi*f/c) + W(p,q);
end
end
end
W = W/Num_mic;
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(:));
end
Accepted Answer
More Answers (0)
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!