help using matlab solve ga function for optimization of non linear expression
8 ビュー (過去 30 日間)
古いコメントを表示
Hello , i have this non linear function called "h" shown in the code bellow.
I want to optimize k1, k2 ,a ,b parameters so this function will get values between 0.55 and 0.45 over the x axes.
I have tried to use the following manual and save the script file as optim.m
i tried to use use solve ga to get my function be get values between 0.55 and 0.45 over the x axes
i get an error shown bellow.
Where did i go wrong?
"Error using optim
Constraints must be an OptimizationConstraint or a struct containing OptimizationConstraints."
Thanks.
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
cam=h(a,b,k1,k2);
prob = optimproblem("Objective",cam);
prob.Constraints.cons1=a<=1;
prob.Constraints.cons2=a>=-1;
prob.Constraints.cons3=b<=1;
prob.Constraints.cons4=b>=-1;
prob.Constraints.cons5=h<=0.55;
prob.Constraints.cons5=h>=0.45;
[sol,fval] = solve(prob,"Solver","ga");
0 件のコメント
回答 (1 件)
Matt J
2022 年 11 月 4 日
編集済み: Matt J
2022 年 11 月 4 日
x=linspace(0,100,10000);
vars = ga(fun,4,A,b,Aeq,beq,lb,ub, @(vars)nonlcon(vars,x), options);
function [c,ceq]=nonlcon(vars,x)
a=vars(1); b=vars(2);
k1=vars(3); k2=vars(4);
h =abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
c(1)=0.45-min(h);
c(2)=max(h)-0.55;
ceq=[];
end
7 件のコメント
Matt J
2022 年 11 月 5 日
編集済み: Matt J
2022 年 11 月 5 日
Optimization variables cannot be combined with complex data.
This ran to completion for me:
a=optimvar('a','Lower',-1,'Upper',+1);
b=optimvar('b','Lower',-1,'Upper',+1);
k1=optimvar('k1','LowerBound',-1,'UpperBound',1);
k2=optimvar('k2','LowerBound',-1,'UpperBound',1);
x=linspace(0,10,1000);%<---- I made this up
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
hmax=@(a,b,k1,k2) max(h(a,b,k1,k2));
hmin=@(a,b,k1,k2) min(h(a,b,k1,k2));
cam=fcn2optimexpr( h, a,b,k1,k2);
camMax=fcn2optimexpr( hmax, a,b,k1,k2);
camMin=fcn2optimexpr( hmin, a,b,k1,k2);
prob = optimproblem("Objective",camMax);
prob.Constraints.camUpper=camMax<=0.55;
prob.Constraints.camLower=camMin>=0.45;
[sol,fval] = solve(prob,"Solver","ga");
you have changed the original code.could you please use one "h" function?
No, you need multiple functions. However, here is a slightly different formulation which avoids hmin.
a=optimvar('a','Lower',-1,'Upper',+1);
b=optimvar('b','Lower',-1,'Upper',+1);
k1=optimvar('k1','LowerBound',-1,'UpperBound',1);
k2=optimvar('k2','LowerBound',-1,'UpperBound',1);
x=linspace(0,10,1000);%<---- I made this up
h=@(a,b,k1,k2)abs(a*exp(-j*k1*x)+b*exp(-j*k2*x));
hmax=@(a,b,k1,k2) max(h(a,b,k1,k2));
cam=fcn2optimexpr( h, a,b,k1,k2);
camMax=fcn2optimexpr( hmax, a,b,k1,k2);
prob = optimproblem("Objective",camMax);
prob.Constraints.camUpper=camMax<=0.55;
prob.Constraints.camLower=cam>=0.45;
[sol,fval] = solve(prob,"Solver","ga");
参考
カテゴリ
Help Center および File Exchange で Get Started with Optimization Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!