problem running optimization SOLVE function in matlab
5 views (last 30 days)
Show older comments
Hello, I am trying to run an optimization so i get my S function at some biunds as shown bellow.
I got the error from the optimization.
Where did i go wrong formulating the SOLVE function?
Thanks.
Solving problem using gamultiobj.
Infeasible individuals are present in the final population.
Optimization terminated: average change in the spread of Pareto solutions less than options.FunctionTolerance.
Constraints are not satisfied within constraint tolerance.
Error using optim.problemdef.OptimizationProblem/solve
Unable to perform assignment because the size of the left side is 1-by-100 and the size of the right side
is 10-by-10.
Error in optim2 (line 46)
[sol,fval] = solve(prob);
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
[code]
clc
clear all
f=6;
lambda=300/6;
theta_in_rad=(0/180)*pi;
theta_out_rad=(70/180)*pi;
x=linspace(-0.5*lambda,0.5*lambda,10);
y=0*linspace(-0.5*lambda,0.5*lambda,10);
[xx,yy]=meshgrid(x,y);
k=2*pi/lambda;
E_i=1;
E_r=E_i*sqrt(cos(theta_in_rad)/cos(theta_out_rad));
eta=377;
D_x=1.064*lambda;
k_x=k*sin(theta_in_rad)+2*pi/D_x;
k_y=sqrt(k^2-k_x.^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=[2,3,4,5,6,7,8];
k_x_n=k*sin(theta_in_rad)+(2*pi.*n)/D_x;
k_y_n=sqrt(k^2-k_x_n.^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%E_t=E_i.*exp(-j*k*xx*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+E_r.*exp(-j*k_x*xx-j*k_y*yy);%E_z
%E_t=E_i.*exp(-j*k*xx*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad));%E_z
%E_t=E_i.*exp(-j*k*xx*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+E_r.*exp(-j*k_x*xx-j*k_y*yy)
%E_t_evanesent=E_opt(1).*exp(-j*k_x_n(1)*xx-j*k_y_n(1)*yy)+E_opt(2).*exp(-j*k_x_n(2)*xx-j*k_y_n(2)*yy)+...
%E_t=E_i.*exp(-j*k*xx*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+E_r.*exp(-j*k_x*xx-j*k_y*yy)+E_t_evanesent;
%E_t_ev=E_i.*exp(-j*k*xx*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+E_r.*exp(-j*k_x*xx-j*k_y*yy)+E_opt(1).*exp(-j*k_x_n(1)*xx-j*k_y_n(1)*yy)+E_opt(2).*exp(-j*k_x_n(2)*xx-j*k_y_n(2)*yy)+E_opt(3).*exp(-j*k_x_n(3)*xx-j*k_y_n(3)*yy)+E_opt(4).*exp(-j*k_x_n(4)*xx-j*k_y_n(4)*yy)+E_opt(5).*exp(-j*k_x_n(5)*xx-j*k_y_n(5)*yy)+E_opt(6).*exp(-j*k_x_n(6)*xx-j*k_y_n(6)*yy)+E_opt(7).*exp(-j*k_x_n(7)*xx-j*k_y_n(7)*yy);
%H_t_ev=(j*k*cos(theta_in_rad))*E_i.*exp(-j*k*xx*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+(-j*k_y)*E_r.*exp(-j*k_x*xx-j*k_y*yy)+(-j*k_y_n(1))*E_opt(1).*exp(-j*k_x_n(1)*xx-j*k_y_n(1)*yy)+(-j*k_y_n(2))*E_opt(2).*exp(-j*k_x_n(1)*xx-j*k_y_n(2)*yy)+(-j*k_y_n(3))*E_opt(3).*exp(-j*k_x_n(3)*xx-j*k_y_n(3)*yy)+(-j*k_y_n(4))*E_opt(4).*exp(-j*k_x_n(4)*xx-j*k_y_n(4)*yy)+(-j*k_y_n(5))*E_opt(5).*exp(-j*k_x_n(5)*xx-j*k_y_n(5)*yy)+(-j*k_y_n(6))*E_opt(6).*exp(-j*k_x_n(6)*xx-j*k_y_n(6)*yy)+(-j*k_y_n(7))*E_opt(7).*exp(-j*k_x_n(7)*xx-j*k_y_n(7)*yy);
E_opt_amp = optimvar('E_opt_amp',7,'Lower',0,'Upper',1);
E_opt_ph = optimvar('E_opt_ph',7,'Lower',0,'Upper',360);
S_max=@(E_opt_amp,E_opt_ph)max(abs(E_i.*exp(-j*k*x*sin(theta_in_rad)+j*k*y*cos(theta_in_rad))+E_r.*exp(-j*k_x*x-j*k_y*y)+E_opt_amp(1)*exp(j*(E_opt_ph(1)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(1)*yy)+E_opt_amp(2)*exp(j*(E_opt_ph(2)/180)*pi).*exp(-j*k_x_n(2)*x-j*k_y_n(2)*y)+E_opt_amp(3)*exp(j*(E_opt_ph(3)/180)*pi).*exp(-j*k_x_n(3)*xx-j*k_y_n(3)*y)+E_opt_amp(4)*exp(j*(E_opt_ph(4)/180)*pi).*exp(-j*k_x_n(4)*x-j*k_y_n(4)*y)+E_opt_amp(5)*exp(j*(E_opt_ph(5)/180)*pi).*exp(-j*k_x_n(5)*x-j*k_y_n(5)*y)+E_opt_amp(6)*exp(j*(E_opt_ph(6)/180)*pi).*exp(-j*k_x_n(6)*xx-j*k_y_n(6)*y)+E_opt_amp(7)*exp(j*(E_opt_ph(7)/180)*pi).*exp(-j*k_x_n(7)*x-j*k_y_n(7)*y).*conj((1/(-j*k*eta))*(j*k*cos(theta_in_rad))*E_i.*exp(-j*k*x*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+(-j*k_y)*E_r.*exp(-j*k_x*xx-j*k_y*y)+(-j*k_y_n(1))*E_opt_amp(1)*exp(j*(E_opt_ph(1)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(1)*y)+(-j*k_y_n(2))*E_opt_amp(2)*exp(j*(E_opt_ph(2)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(2)*y)+(-j*k_y_n(3))*E_opt_amp(3)*exp(j*(E_opt_ph(3)/180)*pi).*exp(-j*k_x_n(3)*x-j*k_y_n(3)*y)+(-j*k_y_n(4))*E_opt_amp(4)*exp(j*(E_opt_ph(4)/180)*pi).*exp(-j*k_x_n(4)*x-j*k_y_n(4)*y)+(-j*k_y_n(5))*E_opt_amp(5)*exp(j*(E_opt_ph(5)/180)*pi).*exp(-j*k_x_n(5)*x-j*k_y_n(5)*y)+(-j*k_y_n(6))*E_opt_amp(6)*exp(j*(E_opt_ph(6)/180)*pi).*exp(-j*k_x_n(6)*x-j*k_y_n(6)*y)+(-j*k_y_n(7))*E_opt_amp(7)*exp(j*(E_opt_ph(7)/180)*pi).*exp(-j*k_x_n(7)*xx-j*k_y_n(7)*y))));
S_min=@(E_opt_amp,E_opt_ph)min(abs(E_i.*exp(-j*k*x*sin(theta_in_rad)+j*k*y*cos(theta_in_rad))+E_r.*exp(-j*k_x*x-j*k_y*y)+E_opt_amp(1)*exp(j*(E_opt_ph(1)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(1)*yy)+E_opt_amp(2)*exp(j*(E_opt_ph(2)/180)*pi).*exp(-j*k_x_n(2)*x-j*k_y_n(2)*y)+E_opt_amp(3)*exp(j*(E_opt_ph(3)/180)*pi).*exp(-j*k_x_n(3)*xx-j*k_y_n(3)*y)+E_opt_amp(4)*exp(j*(E_opt_ph(4)/180)*pi).*exp(-j*k_x_n(4)*x-j*k_y_n(4)*y)+E_opt_amp(5)*exp(j*(E_opt_ph(5)/180)*pi).*exp(-j*k_x_n(5)*x-j*k_y_n(5)*y)+E_opt_amp(6)*exp(j*(E_opt_ph(6)/180)*pi).*exp(-j*k_x_n(6)*xx-j*k_y_n(6)*y)+E_opt_amp(7)*exp(j*(E_opt_ph(7)/180)*pi).*exp(-j*k_x_n(7)*x-j*k_y_n(7)*y).*conj((1/(-j*k*eta))*(j*k*cos(theta_in_rad))*E_i.*exp(-j*k*x*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+(-j*k_y)*E_r.*exp(-j*k_x*xx-j*k_y*y)+(-j*k_y_n(1))*E_opt_amp(1)*exp(j*(E_opt_ph(1)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(1)*y)+(-j*k_y_n(2))*E_opt_amp(2)*exp(j*(E_opt_ph(2)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(2)*y)+(-j*k_y_n(3))*E_opt_amp(3)*exp(j*(E_opt_ph(3)/180)*pi).*exp(-j*k_x_n(3)*x-j*k_y_n(3)*y)+(-j*k_y_n(4))*E_opt_amp(4)*exp(j*(E_opt_ph(4)/180)*pi).*exp(-j*k_x_n(4)*x-j*k_y_n(4)*y)+(-j*k_y_n(5))*E_opt_amp(5)*exp(j*(E_opt_ph(5)/180)*pi).*exp(-j*k_x_n(5)*x-j*k_y_n(5)*y)+(-j*k_y_n(6))*E_opt_amp(6)*exp(j*(E_opt_ph(6)/180)*pi).*exp(-j*k_x_n(6)*x-j*k_y_n(6)*y)+(-j*k_y_n(7))*E_opt_amp(7)*exp(j*(E_opt_ph(7)/180)*pi).*exp(-j*k_x_n(7)*xx-j*k_y_n(7)*y))));
S=@(E_opt_amp,E_opt_ph)abs(E_i.*exp(-j*k*x*sin(theta_in_rad)+j*k*y*cos(theta_in_rad))+E_r.*exp(-j*k_x*x-j*k_y*y)+E_opt_amp(1)*exp(j*(E_opt_ph(1)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(1)*yy)+E_opt_amp(2)*exp(j*(E_opt_ph(2)/180)*pi).*exp(-j*k_x_n(2)*x-j*k_y_n(2)*y)+E_opt_amp(3)*exp(j*(E_opt_ph(3)/180)*pi).*exp(-j*k_x_n(3)*xx-j*k_y_n(3)*y)+E_opt_amp(4)*exp(j*(E_opt_ph(4)/180)*pi).*exp(-j*k_x_n(4)*x-j*k_y_n(4)*y)+E_opt_amp(5)*exp(j*(E_opt_ph(5)/180)*pi).*exp(-j*k_x_n(5)*x-j*k_y_n(5)*y)+E_opt_amp(6)*exp(j*(E_opt_ph(6)/180)*pi).*exp(-j*k_x_n(6)*xx-j*k_y_n(6)*y)+E_opt_amp(7)*exp(j*(E_opt_ph(7)/180)*pi).*exp(-j*k_x_n(7)*x-j*k_y_n(7)*y).*conj((1/(-j*k*eta))*(j*k*cos(theta_in_rad))*E_i.*exp(-j*k*x*sin(theta_in_rad)+j*k*yy*cos(theta_in_rad))+(-j*k_y)*E_r.*exp(-j*k_x*xx-j*k_y*y)+(-j*k_y_n(1))*E_opt_amp(1)*exp(j*(E_opt_ph(1)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(1)*y)+(-j*k_y_n(2))*E_opt_amp(2)*exp(j*(E_opt_ph(2)/180)*pi).*exp(-j*k_x_n(1)*x-j*k_y_n(2)*y)+(-j*k_y_n(3))*E_opt_amp(3)*exp(j*(E_opt_ph(3)/180)*pi).*exp(-j*k_x_n(3)*x-j*k_y_n(3)*y)+(-j*k_y_n(4))*E_opt_amp(4)*exp(j*(E_opt_ph(4)/180)*pi).*exp(-j*k_x_n(4)*x-j*k_y_n(4)*y)+(-j*k_y_n(5))*E_opt_amp(5)*exp(j*(E_opt_ph(5)/180)*pi).*exp(-j*k_x_n(5)*x-j*k_y_n(5)*y)+(-j*k_y_n(6))*E_opt_amp(6)*exp(j*(E_opt_ph(6)/180)*pi).*exp(-j*k_x_n(6)*x-j*k_y_n(6)*y)+(-j*k_y_n(7))*E_opt_amp(7)*exp(j*(E_opt_ph(7)/180)*pi).*exp(-j*k_x_n(7)*xx-j*k_y_n(7)*y)));
cam=fcn2optimexpr( S,E_opt_amp,E_opt_ph);
camMax=fcn2optimexpr( S_max,E_opt_amp,E_opt_ph);
camMin=fcn2optimexpr( S_min,E_opt_amp,E_opt_ph);
prob = optimproblem("Objective",cam);
prob.Constraints.camUpper=camMax<=0.5;
prob.Constraints.camLower=camMin>=0;
[sol,fval] = solve(prob);
sol.E_opt_amp
fval
[/code]
0 Comments
Answers (1)
Paul Kerr-Delworth
on 11 Jul 2023
Hi,
Thanks for posting this and apologies for stumbling across an internal bug which we'll fix as soon as possible.
As a workaround
Add this function to the bottom of your code
function out = convertToRow(S, E_opt_amp,E_opt_ph)
out = S(E_opt_amp,E_opt_ph);
out = out(:)';
end
Change this line
cam=fcn2optimexpr( S,E_opt_amp,E_opt_ph);
to
cam=fcn2optimexpr( @(E_opt_amp,E_opt_ph)convertToRow(S, E_opt_amp,E_opt_ph) ,E_opt_amp,E_opt_ph);
Your code should now run.
Hope this helps!
Paul
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!