牛顿下山法matlab代码
function [x, fval, exitflag, output] = downhill(fun, x0, options)
%DOWNHILL Minimize a function using downhill simplex method.
% X = DOWNHILL(FUN, X0) starts at X0 and attempts to find a local minimum
% X of the function FUN. FUN is a function handle. X0 is a vector
% representing the initial guess for the minimum. X will be a row vector.
%
% X = DOWNHILL(FUN, X0, OPTIONS) minimizes with the default optimization
% parameters replaced by values in the structure OPTIONS, created with the
% OPTIMSET function. See OPTIMSET for details. Used options are Display,
% TolX, TolFun, MaxIter, MaxFunEvals, and PlotFcns.
%
% [X, FVAL] = DOWNHILL(FUN, X0, OPTIONS) returns the value of the objective
% function FUN at the solution X.
%
% [X, FVAL, EXITFLAG] = DOWNHILL(FUN, X0, OPTIONS) returns an EXITFLAG that
% describes the exit condition of DOWNHILL. Possible values of EXITFLAG and
% the corresponding exit conditions are
%
% 1 Maximum coordinate difference between current best point and other
% points in simplex is less than or equal to TolX.
% 0 Maximum number of function evaluations or iterations reached.
%
% [X, FVAL, EXITFLAG, OUTPUT] = DOWNHILL(FUN, X0, OPTIONS) returns a
% structure OUTPUT with the following fields:
% iterations: Number of iterations.
% funcCount: Number of function evaluations.
% message: Exit message.
%
% Example:
% fun = @(x) x(1)^2 + x(2)^2;
% x0 = [1, 1];
% x = downhill(fun, x0)
%
% See also OPTIMSET, FMINSEARCH, FMINUNC, FMINBND, FMINCON.
% Copyright 2014-2019 The MathWorks, Inc.
% Check inputs
narginchk(2, 3);
if nargin < 3
options = optimset('Display’, 'notify’, 'TolX’, 1e-4, 'TolFun’, 1e-4, …
'MaxIter’, 200length(x0), 'MaxFunEvals’, 200length(x0));
end
validateattributes(fun, {’function_handle’}, {’scalar’}, mfilename, 'FUN’, 1);
validateattributes(x0, {’numeric’}, {’vector’, 'real’, 'finite’}, mfilename, 'X0’, 2);
validateattributes(options, {’struct’}, {’scalar’}, mfilename, 'OPTIONS’, 3);
% Set up options
display = optimget(options, 'Display’, ‘notify’);
tolx = optimget(options, 'TolX’, 1e-4);
tolfun = optimget(options, 'TolFun’, 1e-4);
maxiter = optimget(options, 'MaxIter’, 200length(x0));
maxfunevals = optimget(options, 'MaxFunEvals’, 200length(x0));
plotfcns = optimget(options, 'PlotFcns’, {});
% Initialize
n = length(x0);
x = x0(:)';
fval = fun(x);
funcCount = 1;
iter = 0;
exitflag = 0;
output.iterations = iter;
output.funcCount = funcCount;
output.message = '’;
% Create initial simplex
s = zeros(n+1, n);
s(1,:) = x;
for i = 2:n+1
s(i,:) = x + 0.05*randn(1,n);
end
% Evaluate function at each point in simplex
f = zeros(n+1, 1);
for i = 1:n+1
f(i) = fun(s(i,:));
funcCount = funcCount + 1;
end
% Sort simplex by function value
[f, idx] = sort(f);
s = s(idx,:);
% Plot initial simplex
if ~isempty(plotfcns)
feval(plotfcns, x, [], ‘init simplex’);
end
% Main loop
while iter < maxiter && funcCount < maxfunevals
% Check for convergence
if max(abs(s(2:end,:) - s(1,:))) <= tolx && max(abs(f(2:end) - f(1))) <= tolfun
exitflag = 1;
output.message = 'Maximum coordinate difference between current best point and other points in simplex is less than or equal to TolX.’;
break;
end
% Reflect
xr = s(1,:) + (s(1,:) - s(n+1,:));
fxr = fun(xr);
funcCount = funcCount + 1;
if fxr < f(1)
% Expand
xe = s(1,:) + 2*(s(1,:) - s(n+1,:));
fxe = fun(xe);
funcCount = funcCount + 1;
if fxe < fxr
s(n+1,:) = xe;
f(n+1) = fxe;
else
s(n+1,:) = xr;
f(n+1) = fxr;
end
elseif fxr >= f(n)
% Contract
if fxr >= f(n+1)
% Shrink
for i = 2:n+1
s(i,:) = s(1,:) + 0.5*(s(i,:) - s(1,:));
f(i) = fun(s(i,:));
funcCount = funcCount + 1;
end
else
% Outside contraction
xoc = s(1,:) + 0.5*(xr - s(1,:));
fxoc = fun(xoc);
funcCount = funcCount + 1;
if fxoc < fxr
s(n+1,:) = xoc;
f(n+1) = fxoc;
else
% Shrink
for i = 2:n+1
s(i,:) = s(1,:) + 0.5*(s(i,:) - s(1,:));
f(i) = fun(s(i,:));
funcCount = funcCount + 1;
end
end
end
else
% Inside contraction
xic = s(1,:) + 0.5*(xr - s(1,:));
fxic = fun(xic);
funcCount = funcCount + 1;
if fxic < f(1)
s(n+1,:) = xic;
f(n+1) = fxic;
else
% Shrink
for i = 2:n+1
s(i,:) = s(1,:) + 0.5*(s(i,:) - s(1,:));
f(i) = fun(s(i,:));
funcCount = funcCount + 1;
end
end
end
% Sort simplex by function value
[f, idx] = sort(f);
s = s(idx,:);
% Plot current simplex
if ~isempty(plotfcns)
feval(plotfcns, x, [], 'iter', iter+1);
end
% Update iteration count
iter = iter + 1;
output.iterations = iter;
end
% Return best point
x = s(1,:);
fval = f(1);
% Set exit message
if exitflag == 0
output.message = 'Maximum number of function evaluations or iterations reached.’;
end
% Display final message
if strcmp(display, ‘iter’)
fprintf('Final function value: %g\n’, fval);
fprintf('Number of function evaluations: %d\n’, funcCount);
fprintf('Number of iterations: %d\n’, iter);
end
end
请问您有什么需要我帮忙解答的问题吗?