Nonlinear constraints depend on the output of optimization objective

49 ビュー (過去 30 日間)
Jiali
Jiali 2024 年 11 月 29 日 9:12
編集済み: Torsten 2024 年 11 月 30 日 20:50
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;

回答 (2 件)

Torsten
Torsten 2024 年 11 月 29 日 9:42
編集済み: Torsten 2024 年 11 月 30 日 20:50
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 件のコメント
Matt J
Matt J 2024 年 11 月 29 日 14:29
編集済み: Matt J 2024 年 11 月 29 日 14:31
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

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


Hitesh
Hitesh 2024 年 11 月 29 日 9:39
編集済み: Hitesh 2024 年 11 月 29 日 9:41
Hi @Jiali,
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:

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by