필터 지우기
필터 지우기

fmincon nonlinear inequality constraint including variable with range?

조회 수: 2 (최근 30일)
Frank
Frank 2023년 4월 29일
편집: Torsten 2023년 4월 30일
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!

답변 (1개)

albara
albara 2023년 4월 29일
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
Walter Roberson
Walter Roberson 2023년 4월 30일
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 2023년 4월 30일
편집: Torsten 2023년 4월 30일
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).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by