Nonlinear constraints depend on the output of optimization objective
조회 수: 1 (최근 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;
댓글 수: 0
채택된 답변
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
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
2024년 11월 29일
편집: Hitesh
2024년 11월 29일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!