在练习2中使用了到了两个函数:optimset和fminunc。
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
%set the GradObj option to on,which tells fminunc that our function
%returns both the cost and gradient.
%This allows fminunc to use the gradient when minimizing the function.
%set the MaxIter option to 400, so that fminunc will run for at most 400
%steps before it terminates.
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
1、通过help查看optimset函数
>> help optimset
optimset Create/alter optimization OPTIONS structure.
OPTIONS = optimset('PARAM1',VALUE1,'PARAM2',VALUE2,...) creates an
optimization options structure OPTIONS in which the named parameters have
the specified values. Any unspecified parameters are set to [] (parameters
with value [] indicate to use the default value for that parameter when
OPTIONS is passed to the optimization function). It is sufficient to type
only the leading characters that uniquely identify the parameter. Case is
ignored for parameter names.
NOTE: For values that are strings, the complete string is required.
OPTIONS = optimset(OLDOPTS,'PARAM1',VALUE1,...) creates a copy of OLDOPTS
with the named parameters altered with the specified values.
OPTIONS = optimset(OLDOPTS,NEWOPTS) combines an existing options structure
OLDOPTS with a new options structure NEWOPTS. Any parameters in NEWOPTS
with non-empty values overwrite the corresponding old parameters in
OLDOPTS.
optimset with no input arguments and no output arguments displays all
parameter names and their possible values, with defaults shown in {}
when the default is the same for all functions that use that parameter.
Use optimset(OPTIMFUNCTION) to see parameters for a specific function.
OPTIONS = optimset (with no input arguments) creates an options structure
OPTIONS where all the fields are set to [].
OPTIONS = optimset(OPTIMFUNCTION) creates an options structure with all
the parameter names and default values relevant to the optimization
function named in OPTIMFUNCTION. For example,
optimset('fminbnd')
or
optimset(@fminbnd)
returns an options structure containing all the parameter names and
default values relevant to the function 'fminbnd'.
optimset PARAMETERS for MATLAB
Display - Level of display [ off | iter | notify