请教 m文件的编写
楼主 · 1 楼
可以从Matlab的帮助文件里面找到解答。
下面是转自Matlab里面的一段帮助文件,可能会有一些帮助的。
FMINUNC finds the minimum of a function of several variables.
X=FMINUNC(FUN,X0) starts at X0 and attempts to find a local minimizer X
of the function FUN. FUN accepts input X and returns a scalar function
value F evaluated at X. X0 can be a scalar, vector or matrix.
X=FMINUNC(FUN,X0,OPTIONS) minimizes with the default optimization
parameters replaced by values in the structure OPTIONS, an argument
created with the OPTIMSET function. See OPTIMSET for details. Used
options are Display, TolX, TolFun, DerivativeCheck, Diagnostics,
FunValCheck GradObj, HessPattern, Hessian, HessMult, HessUpdate,
InitialHessType, InitialHessMatrix, MaxFunEvals, MaxIter,
DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter,
PrecondBandWidth, TolPCG, TypicalX. Use the GradObj option to specify
that FUN also returns a second output argument G that is the partial
derivatives of the function df/dX, at the point X. Use the Hessian
option to specify that FUN also returns a third output argument H that
is the 2nd partial derivatives of the function (the Hessian) at the
point X. The Hessian is only used by the large-scale method, not the
line-search method.
[X,FVAL]=FMINUNC(FUN,X0,...) returns the value of the objective
function FUN at the solution X.
[X,FVAL,EXITFLAG]=FMINUNC(FUN,X0,...) returns an EXITFLAG that describes
the exit condition of FMINUNC. Possible values of EXITFLAG and the
corresponding exit conditions are
1 FMINUNC converged to a solution X.
2 Change in X smaller than the specified tolerance.
3 Change in the objective function value smaller than the specified
tolerance (only occurs in the large-scale method).
0 Maximum number of function evaluations or iterations reached.
-1 Algorithm terminated by the output function.
-2 Line search cannot sufficiently decrease the objective function along
the current search direction (only occurs in the medium-scale method).
[X,FVAL,EXITFLAG,OUTPUT]=FMINUNC(FUN,X0,...) returns a structure OUTPUT
with the number of iterations taken in OUTPUT.iterations, the number
of function evaluations in OUTPUT.funcCount, the algorithm used in
OUTPUT.algorithm, the number of CG iterations (if used) in
OUTPUT.cgiterations, the first-order optimality (if used) in
OUTPUT.firstorderopt, and the exit message in OUTPUT.message.
[X,FVAL,EXITFLAG,OUTPUT,GRAD]=FMINUNC(FUN,X0,...) returns the value
of the gradient of FUN at the solution X.
[X,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN]=FMINUNC(FUN,X0,...) returns the
value of the Hessian of the objective function FUN at the solution X.
Examples
FUN can be specified using @:
X = fminunc(@myfun,2)
where MYFUN is a MATLAB function such as:
function F = myfun(x)
F = sin(x) + 3;
To minimize this function with the gradient provided, modify
the MYFUN so the gradient is the second output argument:
function [f,g]= myfun(x)
f = sin(x) + 3;
g = cos(x);
and indicate the gradient value is available by creating an options
structure with OPTIONS.GradObj set to 'on' (using OPTIMSET):
options = optimset('GradObj','on');
x = fminunc('myfun',4,options);
FUN can also be an anonymous function:
x = fminunc(@(x) 5*x(1)^2 + x(2)^2,[5;1])
If FUN is parameterized, you can use anonymous functions to capture the problem-
dependent parameters. Suppose you want to minimize the objective given in the
function MYFUN, which is parameterized by its second argument A. Here MYFUN is
an M-file function such as
function [f,g] = myfun(x,a)
f = a*x(1)^2 + 2*x(1)*x(2) + x(2)^2; % function
g = [2*a*x(1) + 2*x(2) % gradient
2*x(1) + 2*x(2)];
To optimize for a specific value of A, first assign the value to A. Then
create a one-argument anonymous function that captures that value of A
and calls MYFUN with two arguments. Finally, pass this anonymous function
to FMINUNC:
a = 3; % define parameter first
options = optimset('GradObj','on'); % indicate gradient is provided
x = fminunc(@(x) myfun(x,a),[1;1],options)
下面是转自Matlab里面的一段帮助文件,可能会有一些帮助的。
FMINUNC finds the minimum of a function of several variables.
X=FMINUNC(FUN,X0) starts at X0 and attempts to find a local minimizer X
of the function FUN. FUN accepts input X and returns a scalar function
value F evaluated at X. X0 can be a scalar, vector or matrix.
X=FMINUNC(FUN,X0,OPTIONS) minimizes with the default optimization
parameters replaced by values in the structure OPTIONS, an argument
created with the OPTIMSET function. See OPTIMSET for details. Used
options are Display, TolX, TolFun, DerivativeCheck, Diagnostics,
FunValCheck GradObj, HessPattern, Hessian, HessMult, HessUpdate,
InitialHessType, InitialHessMatrix, MaxFunEvals, MaxIter,
DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter,
PrecondBandWidth, TolPCG, TypicalX. Use the GradObj option to specify
that FUN also returns a second output argument G that is the partial
derivatives of the function df/dX, at the point X. Use the Hessian
option to specify that FUN also returns a third output argument H that
is the 2nd partial derivatives of the function (the Hessian) at the
point X. The Hessian is only used by the large-scale method, not the
line-search method.
[X,FVAL]=FMINUNC(FUN,X0,...) returns the value of the objective
function FUN at the solution X.
[X,FVAL,EXITFLAG]=FMINUNC(FUN,X0,...) returns an EXITFLAG that describes
the exit condition of FMINUNC. Possible values of EXITFLAG and the
corresponding exit conditions are
1 FMINUNC converged to a solution X.
2 Change in X smaller than the specified tolerance.
3 Change in the objective function value smaller than the specified
tolerance (only occurs in the large-scale method).
0 Maximum number of function evaluations or iterations reached.
-1 Algorithm terminated by the output function.
-2 Line search cannot sufficiently decrease the objective function along
the current search direction (only occurs in the medium-scale method).
[X,FVAL,EXITFLAG,OUTPUT]=FMINUNC(FUN,X0,...) returns a structure OUTPUT
with the number of iterations taken in OUTPUT.iterations, the number
of function evaluations in OUTPUT.funcCount, the algorithm used in
OUTPUT.algorithm, the number of CG iterations (if used) in
OUTPUT.cgiterations, the first-order optimality (if used) in
OUTPUT.firstorderopt, and the exit message in OUTPUT.message.
[X,FVAL,EXITFLAG,OUTPUT,GRAD]=FMINUNC(FUN,X0,...) returns the value
of the gradient of FUN at the solution X.
[X,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN]=FMINUNC(FUN,X0,...) returns the
value of the Hessian of the objective function FUN at the solution X.
Examples
FUN can be specified using @:
X = fminunc(@myfun,2)
where MYFUN is a MATLAB function such as:
function F = myfun(x)
F = sin(x) + 3;
To minimize this function with the gradient provided, modify
the MYFUN so the gradient is the second output argument:
function [f,g]= myfun(x)
f = sin(x) + 3;
g = cos(x);
and indicate the gradient value is available by creating an options
structure with OPTIONS.GradObj set to 'on' (using OPTIMSET):
options = optimset('GradObj','on');
x = fminunc('myfun',4,options);
FUN can also be an anonymous function:
x = fminunc(@(x) 5*x(1)^2 + x(2)^2,[5;1])
If FUN is parameterized, you can use anonymous functions to capture the problem-
dependent parameters. Suppose you want to minimize the objective given in the
function MYFUN, which is parameterized by its second argument A. Here MYFUN is
an M-file function such as
function [f,g] = myfun(x,a)
f = a*x(1)^2 + 2*x(1)*x(2) + x(2)^2; % function
g = [2*a*x(1) + 2*x(2) % gradient
2*x(1) + 2*x(2)];
To optimize for a specific value of A, first assign the value to A. Then
create a one-argument anonymous function that captures that value of A
and calls MYFUN with two arguments. Finally, pass this anonymous function
to FMINUNC:
a = 3; % define parameter first
options = optimset('GradObj','on'); % indicate gradient is provided
x = fminunc(@(x) myfun(x,a),[1;1],options)
2 楼
在m文件的编写时,用到fmincon求解
非线性规划问题,请问在m文件中,fmincon的目标函数和约束函数怎么定义,注意,目标函数和约束函数要用到m文件先前定义的矩阵变量。
3 楼
如果另编写m文件定义目标函数和约束函数,要用到主文件中的一系列矩阵变量,怎么办?程序执行说 未定义的 变量