Find the minimum of a function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
How can I find the min of this function Minf0(x,y)=x^2+y^2 S.t x ≥ 2 , x+y=3
채택된 답변
Hi @Melika Eft
The constrained optimization problem probably can be formulated like this:
fun = @(x) x(1)^2 + x(2)^2;
x0 = [1, 2]; % initial guess
A = [-1, 0]; % A*x ≤ b
b = 2;
Aeq = [1, 1]; % Aeq*x = beq
beq = 3;
x = fmincon(fun, x0, A, b, Aeq, beq)
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.
x = 1×2
1.5000 1.5000
For more info, please lookup
help fmincon
FMINCON finds a constrained minimum of a function of several variables.
FMINCON attempts to solve problems of the form:
min F(X) subject to: A*X <= B, Aeq*X = Beq (linear constraints)
X C(X) <= 0, Ceq(X) = 0 (nonlinear constraints)
LB <= X <= UB (bounds)
FMINCON implements four different algorithms: interior point, SQP,
active set, and trust region reflective. Choose one via the option
Algorithm: for instance, to choose SQP, set OPTIONS =
optimoptions('fmincon','Algorithm','sqp'), and then pass OPTIONS to
FMINCON.
X = FMINCON(FUN,X0,A,B) starts at X0 and finds a minimum X to the
function FUN, subject to the linear inequalities A*X <= B. FUN accepts
input X and returns a scalar function value F evaluated at X. X0 may be
a scalar, vector, or matrix.
X = FMINCON(FUN,X0,A,B,Aeq,Beq) minimizes FUN subject to the linear
equalities Aeq*X = Beq as well as A*X <= B. (Set A=[] and B=[] if no
inequalities exist.)
X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB) defines a set of lower and upper
bounds on the design variables, X, so that a solution is found in
the range LB <= X <= UB. Use empty matrices for LB and UB
if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below;
set UB(i) = Inf if X(i) is unbounded above.
X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON) subjects the minimization
to the constraints defined in NONLCON. The function NONLCON accepts X
and returns the vectors C and Ceq, representing the nonlinear
inequalities and equalities respectively. FMINCON minimizes FUN such
that C(X) <= 0 and Ceq(X) = 0. (Set LB = [] and/or UB = [] if no bounds
exist.)
X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS) minimizes with
the default optimization parameters replaced by values in OPTIONS, an
argument created with the OPTIMOPTIONS function. See OPTIMOPTIONS for
details. For a list of options accepted by FMINCON refer to the
documentation.
X = FMINCON(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
structure with the function FUN in PROBLEM.objective, the start point
in PROBLEM.x0, the linear inequality constraints in PROBLEM.Aineq
and PROBLEM.bineq, the linear equality constraints in PROBLEM.Aeq and
PROBLEM.beq, the lower bounds in PROBLEM.lb, the upper bounds in
PROBLEM.ub, the nonlinear constraint function in PROBLEM.nonlcon, the
options structure in PROBLEM.options, and solver name 'fmincon' in
PROBLEM.solver. Use this syntax to solve at the command line a problem
exported from OPTIMTOOL.
[X,FVAL] = FMINCON(FUN,X0,...) returns the value of the objective
function FUN at the solution X.
[X,FVAL,EXITFLAG] = FMINCON(FUN,X0,...) returns an EXITFLAG that
describes the exit condition. Possible values of EXITFLAG and the
corresponding exit conditions are listed below. See the documentation
for a complete description.
All algorithms:
1 First order optimality conditions satisfied.
0 Too many function evaluations or iterations.
-1 Stopped by output/plot function.
-2 No feasible point found.
Trust-region-reflective, interior-point, and sqp:
2 Change in X too small.
Trust-region-reflective:
3 Change in objective function too small.
Active-set only:
4 Computed search direction too small.
5 Predicted change in objective function too small.
Interior-point and sqp:
-3 Problem seems unbounded.
[X,FVAL,EXITFLAG,OUTPUT] = FMINCON(FUN,X0,...) returns a structure
OUTPUT with information such as total number of iterations, and final
objective function value. See the documentation for a complete list.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = FMINCON(FUN,X0,...) returns the
Lagrange multipliers at the solution X: LAMBDA.lower for LB,
LAMBDA.upper for UB, LAMBDA.ineqlin is for the linear inequalities,
LAMBDA.eqlin is for the linear equalities, LAMBDA.ineqnonlin is for the
nonlinear inequalities, and LAMBDA.eqnonlin is for the nonlinear
equalities.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD] = FMINCON(FUN,X0,...) returns the
value of the gradient of FUN at the solution X.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = FMINCON(FUN,X0,...)
returns the value of the exact or approximate Hessian of the Lagrangian
at X.
Examples
FUN can be specified using @:
X = fmincon(@humps,...)
In this case, F = humps(X) returns the scalar function value F of
the HUMPS function evaluated at X.
FUN can also be an anonymous function:
X = fmincon(@(x) 3*sin(x(1))+exp(x(2)),[1;1],[],[],[],[],[0 0])
returns X = [0;0].
If FUN or NONLCON are parameterized, you can use anonymous functions to
capture the problem-dependent parameters. Suppose you want to minimize
the objective given in the function myfun, subject to the nonlinear
constraint mycon, where these two functions are parameterized by their
second argument a1 and a2, respectively. Here myfun and mycon are
MATLAB file functions such as
function f = myfun(x,a1)
f = x(1)^2 + a1*x(2)^2;
function [c,ceq] = mycon(x,a2)
c = a2/x(1) - x(2);
ceq = [];
To optimize for specific values of a1 and a2, first assign the values
to these two parameters. Then create two one-argument anonymous
functions that capture the values of a1 and a2, and call myfun and
mycon with two arguments. Finally, pass these anonymous functions to
FMINCON:
a1 = 2; a2 = 1.5; % define parameters first
options = optimoptions('fmincon','Algorithm','interior-point'); % run interior-point algorithm
x = fmincon(@(x) myfun(x,a1),[1;2],[],[],[],[],[],[],@(x) mycon(x,a2),options)
See also OPTIMOPTIONS, OPTIMTOOL, FMINUNC, FMINBND, FMINSEARCH, @, FUNCTION_HANDLE.
Documentation for fmincon
doc fmincon
댓글 수: 3
Melika Eft
2022년 9월 24일
편집: Melika Eft
2022년 9월 24일
Thank you so much ; very helpful @sam chak
A = [-1, 0]; % A*x ≤ b
b = -2;
instead of
A = [-1, 0]; % A*x ≤ b
b = 2;
Or even better:
lb = [2 -Inf];
ub = [Inf Inf]
You can see it from the result: x = 1.5 is not feasible since 1.5 < 2.
Sam Chak
2022년 9월 24일
@Torsten, thanks for the highlight.
@Melika Eft, please take note to learn that the solution is infeasible.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
