Nonlinear constraints depend on the output of optimization objective
49 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Torsten
el 29 de Nov. de 2024 a las 9:42
Editada: Torsten
el 30 de Nov. de 2024 a las 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 comentario
Matt J
el 29 de Nov. de 2024 a las 14:29
Editada: Matt J
el 29 de Nov. de 2024 a las 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
el 29 de Nov. de 2024 a las 9:39
Editada: Hitesh
el 29 de Nov. de 2024 a las 9:41
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 comentarios
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!