Minimum of a function

2 ビュー (過去 30 日間)
Ray Louise
Ray Louise 2020 年 4 月 26 日
編集済み: John D'Errico 2020 年 4 月 26 日
I am having a hard time figuring out how to call the file-functions. I want to make a nonlinear contraint , and then another file function to call the initial approximations x0(1) и х0(2), then call the the equtaion below
f = @(х) (х(1) - 2).^2 + (х(2) - 4)
Below are my limits:
c=[-x(1);
-x(2);
2*x(1).^2+x(2).^2-34;
2*x(1)+3*x(2).^2-18];
So far I got to here
function f = fun( x )
f = @(х) (х(1) - 2).^2 + (х(2) - 4)
end
function [ с,сequ ] = mycon( x )
с=[-x(1);
x(2);
-x(3);
2*x(1)+2+(x)+3*x(3).^2-34; %% I'm not sure if this is even written correctly.
сequ=[]; end
This speaks nothing to me and I've been trying every option under the moon to make it run but it doesn't work. How can I even write down the last limit as a MATLAB function? I need a slight boost, please. I read the fmincon documentation but this function is giving me a hard time.

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 4 月 26 日
編集済み: Thiago Henrique Gomes Lobato 2020 年 4 月 26 日
You had some typos in your constrain function and also you don't need to create a function file for your function since you already use an annonymous one. Use this and it should work:
f = @(x) (x(1) - 2).^2 + (x(2) - 4);
x0 = [1,1]; % Here you put your x0 function you said you had
nonlcon = @mycon;
x = fmincon(f,x0,[],[],[],[],[],[],nonlcon)
% Another file
function [ c,cequ ] = mycon( x )
c=[-x(1);
-x(2);
2*x(1).^2+x(2).^2-34;
2*x(1)+3*x(2).^2-18]; %% I'm not sure if this is even written correctly.
cequ=[];
end
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
<stopping criteria details>
x =
2.0000 0.0000
  1 件のコメント
John D'Errico
John D'Errico 2020 年 4 月 26 日
編集済み: John D'Errico 2020 年 4 月 26 日
I would only add that the first two inequalities are just bound constraints on x(1) and x(2). so they could be put in directly as bound constraints, rather than needing a nonlinear inequality constraint. If there were 3 variables, then LB would be just
LB = [0,0,-inf];

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by