help using matlab solve ga function for optimization of non linear expression
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Matt J
le 4 Nov 2022
Modifié(e) : Matt J
le 4 Nov 2022
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 commentaires
Matt J
le 5 Nov 2022
Modifié(e) : Matt J
le 5 Nov 2022
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");
Voir également
Catégories
En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!