fmincon nonlinear inequality constraint including variable with range?

1 visualización (últimos 30 días)
Frank
Frank el 29 de Abr. de 2023
Editada: Torsten el 30 de Abr. de 2023
function [c,ceq] = nonlcon(x)
c(1)
The above is the name and content of a nonlinear inequality constraint.
If I want to write a nonlinear inequality constraint c(1) = (complicated function of x)*(n-3) for n between 0 and 9, how can I write it?
I have searched on Google and read many pages but I didn't find the answer.
Thank you very much!

Respuestas (1)

albara
albara el 29 de Abr. de 2023
In MATLAB, you can define a nonlinear inequality constraint function by writing a separate function file. Based on your given information, you can write a constraint function as follows:
  1. Create a new file in MATLAB and name it, for example, nonlcon.m.
  2. In the nonlcon.m file, define the nonlinear inequality constraint function:
function [c, ceq] = nonlcon(x)
% Set the value of n, assuming n is an integer between 0 and 9
n = 5; % (You can replace this with the desired value)
% Define your complicated function of x (replace with your actual function)
complicated_function = x(1)^2 + x(2)^2; % (This is an example, replace it)
% Define the nonlinear inequality constraint c(1)
c(1) = complicated_function * (n - 3);
% No nonlinear equality constraints
ceq = [];
end
3- Replace the example complicated_function with your actual function and set the value of n as needed.
4- Save the file.
Now you can use this nonlcon.m file as the nonlinear constraint function in your optimization problem. For example, if you're using fmincon, you can pass @nonlcon as the nonlinear constraint function:
% Define your objective function
fun = @(x) x(1)^2 + x(2)^2;
% Define initial guess, lower bounds, upper bounds, etc.
x0 = [1; 1];
lb = [];
ub = [];
A = [];
b = [];
Aeq = [];
beq = [];
% Call fmincon with nonlcon as the nonlinear constraint function
[x, fval] = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, @nonlcon);
This should solve your optimization problem with the given nonlinear inequality constraint. Remember to replace the example complicated_function and n with the appropriate expressions and values for your specific problem.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
  9 comentarios
Walter Roberson
Walter Roberson el 30 de Abr. de 2023
n=0:9;
c(n+1) = complicated_function * (n - 3);
You are not restricted to returning c as a scalar. The test is if all(c<=0) then succeed.
Torsten
Torsten el 30 de Abr. de 2023
Editada: Torsten el 30 de Abr. de 2023
c(1) = (complicated function of x)*(n-3) <= 0
Inserting n = 0, you get (complicated function of x)*(-3) <= 0, thus (complicated function of x) >=0
inserting n = 9, you get (complicated function of x)*6 <= 0, thus (complicated function of x) <=0
Thus your condition can only be satisfied if (complicated function of x) =0, and this an equality condition you should set as
ceq(1) = (complicated function of x).

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Optimization Toolbox 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!

Translated by