Nonlinear constraints depend on the output of optimization objective

7 次查看(过去 30 天)
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;

采纳的回答

Torsten
Torsten 2024-11-29
编辑:Torsten 2024-11-30
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
编辑:Matt J 2024-11-29
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

请先登录,再进行评论。

更多回答(1 个)

Hitesh
Hitesh 2024-11-29
编辑:Hitesh 2024-11-29
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 CenterFile Exchange 中查找有关 Solver 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