fmincon nonlinear inequality constraint including variable with range?

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

You answer is NOT relevant, I have said in the title "including variable with range”. Let me clarify: c(1) = (complicated function of x)*(n-3) for n between 0 and 9. That means ALL the real numbers between 0 and 9, NOT a specific number.
Is n a solution variable ?
Frank
Frank el 29 de Abr. de 2023
Editada: Frank el 29 de Abr. de 2023
Thank you very much for your answer.
I guess you meant "solution variable" is x.
n is not a solution variable.
n has specific physical meaning, n coupled with solution variable x has to be set < 0 for ALL real 9 > n > 0.
@Frank - you were not even remotely clear in your question. But you can compute the value of n, correct? As a function of x, that is?
Then all you need to do is compute n inside the constraint function, nonlcon. Now you have TWO nonlinear inequality constraints. Since an inequality constraint for fmincon will be of the form C(X) <= 0, then you will have:
C(1) = -n;
C(2) = n - 9;
Frank
Frank el 29 de Abr. de 2023
Editada: Frank el 29 de Abr. de 2023
I apologize if I wasn't clear. Thanks for your answer. Unfortunately, your answer is NOT what I wanted. The value of n is NOT computed, it is given a range from 0 to 9. It is NOT a function of x.
Let me reformulate my question:
If I want the nonlinear constraint
for ALL real 9>n>0, how can I write it?
This is different from the convention nonlinear constraint, which for n has a fixed number. Here I want the nonlinear constraint to satisfy ALL real numbers from 0 to 9.
Having wrote the above done, I just now understand it is an impossible task for Matlab because computer can only understand discreet numbers, computer can't understand continuous numbers in a range. Therefore, I have got the answer to my question: it is an impossible task.
Thank you all for the discussions!
Have a nice day!
Torsten
Torsten el 29 de Abr. de 2023
Editada: Torsten el 29 de Abr. de 2023
How should C be <=0 if 0 < n < 9 ?
It was a complicated function, so any value is possible, so <=0 is possible. The specific form I wrote was just an example.
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.

Preguntada:

el 29 de Abr. de 2023

Editada:

el 30 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by