Minimum of a function
2 views (last 30 days)
Show older comments
Ray Louise
on 26 Apr 2020
Edited: John D'Errico
on 26 Apr 2020
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.
0 Comments
Accepted Answer
Thiago Henrique Gomes Lobato
on 26 Apr 2020
Edited: Thiago Henrique Gomes Lobato
on 26 Apr 2020
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 Comment
John D'Errico
on 26 Apr 2020
Edited: John D'Errico
on 26 Apr 2020
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];
More Answers (0)
See Also
Categories
Find more on Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!