# optimization of a function with array of parameters using matlab solver ga

1 visualizzazione (ultimi 30 giorni)
fima v il 11 Nov 2022
Modificato: Torsten il 12 Nov 2022
Hello, i i am trying to optimize function S, so it will get values between 0 and 10 using its E_opt parameter
coefficients which shown bellow in the expression of S.
I defined optimvar for each sub cell of the E_opt, and used solve as shown bellow.
but then i get an error on optimvar saying E_opt(1) is not a valid matlab name.
how do we run such optimization so i will get optimized array of parameters E_opt?
Thanks.
clc
clear all
f=6;
lambda=300/6;
theta_in_rad=(0/180)*pi;
theta_out_rad=(70/180)*pi;
x=linspace(-15*lambda,15*lambda,1000);
y=linspace(-15*lambda,15*lambda,1000);
[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_opt(1)=optimvar('E_opt(1)','Lower',0,'Upper',+1);
E_opt(2)=optimvar('E_opt(2)','Lower',0,'Upper',+1);
E_opt(3)=optimvar('E_opt(3)','Lower',0,'Upper',+1);
E_opt(4)=optimvar('E_opt(4)','Lower',0,'Upper',+1);
E_opt(5)=optimvar('E_opt(5)','Lower',0,'Upper',+1);
E_opt(6)=optimvar('E_opt(6)','Lower',0,'Upper',+1);
E_opt(7)=optimvar('E_opt(7)','Lower',0,'Upper',+1);
S=@(E_opt)abs(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).*conj((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)));
S_max=@(E_opt)max(abs(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).*conj((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))));
S_min=@(E_opt)min(abs(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).*conj((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))));
cam=fcn2optimexpr( S,E_opt);
camMax=fcn2optimexpr( S_max,E_opt);
camMin=fcn2optimexpr( S_min,E_opt);
prob = optimproblem("Objective",camMax);
prob.Constraints.camUpper=camMax<=10;
prob.Constraints.camLower=camMin>=0;
[sol,fval] = solve(prob,"Solver","ga");
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
fima v il 12 Nov 2022
Hello ,my E_opt array of variables needed to be complex numbers
is there a way to change the command bellow so each cell will be complex so the amplitude will be between 1 to 7 and the phase is between 0 to 220 degrees?
Thanks.
E_opt = optimvar('E_opt',7,'Lower',0,'Upper',1);

Accedi per commentare.

### Risposte (1)

Torsten il 11 Nov 2022
E_opt = optimvar('E_opt',7,'Lower',0,'Upper',+1);
instead of
E_opt(1)=optimvar('E_opt(1)','Lower',0,'Upper',+1);
E_opt(2)=optimvar('E_opt(2)','Lower',0,'Upper',+1);
E_opt(3)=optimvar('E_opt(3)','Lower',0,'Upper',+1);
E_opt(4)=optimvar('E_opt(4)','Lower',0,'Upper',+1);
E_opt(5)=optimvar('E_opt(5)','Lower',0,'Upper',+1);
E_opt(6)=optimvar('E_opt(6)','Lower',0,'Upper',+1);
E_opt(7)=optimvar('E_opt(7)','Lower',0,'Upper',+1);
and
[sol,fval] = solve(prob);
instead of
[sol,fval] = solve(prob,"Solver","ga");
##### 10 CommentiMostra 8 commenti meno recentiNascondi 8 commenti meno recenti
fima v il 12 Nov 2022
Hello ,my E_opt array of variables needed to be complex numbers
is there a way to change the command bellow so each cell will be complex so the amplitude will be between 1 to 7 and the phase is between 0 to 220 degrees?
Thanks.
E_opt = optimvar('E_opt',7,'Lower',0,'Upper',1);
Torsten il 12 Nov 2022
Modificato: Torsten il 12 Nov 2022
Read the warning in the MATLAB documentation:
Warning
The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result might be incorrect.

Accedi per commentare.

### Categorie

Scopri di più su Systems of Nonlinear Equations in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by