Nonlinear constraints depend on the output of optimization objective
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear all,
I need to optimize two variables to minimize the norm error under some constraints.
However, one variable limitation is decided by another variable (See the following example code). Do I need to call fmincon function twice times? How would I solve this problem efficiently?
Best regards,
Jiali
errfun=@(x) sum(real(a-fun(x(2),x(1),freq)).^2....
+imag(a-fun(x(2),x(1),freq)).^2)^0.5;
constraint:
x(1)>=0;
x(2)>=x(1)*ratio;
0 commentaires
Réponse acceptée
Torsten
le 29 Nov 2024
Modifié(e) : Torsten
le 30 Nov 2024
Define the constraints using a lower bound of 0 for x(1) (you can prescribe this in the array lb) and one linear constraint (you can define this in the matrix A and the vector b).
And don't take the squareroot of your sum of squared differences !
A = [ratio,-1];
b = 0;
lb = [0,-Inf];
ub = [Inf,Inf]
a = ...;
freq = ...;
x0 = ...;
fun = @(x) sum(real(a-fun(x(2),x(1),freq)).^2....
+imag(a-fun(x(2),x(1),freq)).^2);
x = fmincon(fun,x0,A,b,[],[],,lb,ub)
1 commentaire
Matt J
le 29 Nov 2024
Modifié(e) : Matt J
le 29 Nov 2024
In more recent Matlab, you would use lsqcurvefit rather than fmincon (but it won't work in R2018a):
A = [ratio,-1];
b = 0;
lb = [0,-Inf];
ub = [Inf,Inf]
a = ...;
freq = ...;
x0 = ...;
x = lsqcurvefit(fun,x0,freq,[a(:),a(:)], A,b,[],[],lb,ub);
function resid=fun(x,freq)
err=fun(x(2),x(1),freq);
resid=[real(err(:)),imag(err(:))];
end
Plus de réponses (1)
Hitesh
le 29 Nov 2024
Modifié(e) : Hitesh
le 29 Nov 2024
You need to use optimization functions like "fmincon" in MATLAB. You do not need to call fmincon twice as it can handle multiple constraints directly.
Refer to the below code as an example:
n = 100; % Number of data points
freq = linspace(1, 10, n); % Frequency vector
a = randn(1, n) + 1i * randn(1, n); % Complex data vector
% Define the function 'fun'
fun = @(x2, x1, freq) x2 * exp(-x1 * freq); % Example function
% Define the error function
errfun = @(x) sum(real(a - fun(x(2), x(1), freq)).^2 + imag(a - fun(x(2), x(1), freq)).^2)^0.5;
% Optimization constraints
ratio = 0.5; % Example ratio
lb = [0, 0]; % Lower bounds for x(1) and x(2)
nonlcon = @(x) deal([], x(1) * ratio - x(2)); % Non-linear constraint
% Initial guess
x0 = [1, 1];
% Call fmincon
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
[x_opt, fval] = fmincon(errfun, x0, [], [], [], [], lb, [], nonlcon, options);
% Display results
disp('Optimized variables:');
disp(x_opt);
disp('Minimum error:');
disp(fval);

For more information regarding "fmincon" function, refer to the following MATLAB documentation:
0 commentaires
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display 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!