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 CenterFile Exchange에서 Optimization에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by