- Create a new file in MATLAB and name it, for example, nonlcon.m.
- In the nonlcon.m file, define the nonlinear inequality constraint function:
fmincon nonlinear inequality constraint including variable with range?
3 ビュー (過去 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!
0 件のコメント
回答 (1 件)
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:
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
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
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 Center および File Exchange で Nonlinear Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!