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

2 ビュー (過去 30 日間)
fima v 2022 年 11 月 11 日

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;
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;
eta=377;
D_x=1.064*lambda;
k_y=sqrt(k^2-k_x.^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=[2,3,4,5,6,7,8];
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);
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 件のコメント表示非表示 なし
fima v 2022 年 11 月 12 日
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);

サインインしてコメントする。

### 回答 (1 件)

Torsten 2022 年 11 月 11 日
E_opt = optimvar('E_opt',7,'Lower',0,'Upper',+1);
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);
[sol,fval] = solve(prob,"Solver","ga");
##### 10 件のコメント表示非表示 9 件の古いコメント
Torsten 2022 年 11 月 12 日

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.

サインインしてコメントする。

### カテゴリ

Find more on Get Started with Problem-Based Optimization and Equations 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!

Translated by