MATLAB Answers

Minimum of a function

3 views (last 30 days)
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:
So far I got to here
function f = fun( x )
f = @(х) (х(1) - 2).^2 + (х(2) - 4)
function [ с,сequ ] = mycon( x )
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.


Sign in to comment.

Accepted Answer

Thiago Henrique Gomes Lobato
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 )
2*x(1)+3*x(2).^2-18]; %% I'm not sure if this is even written correctly.
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
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];

Sign in to comment.

More Answers (0)