Minimum of a function

1 次查看(过去 30 天)
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
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 个)

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by