% [X,F] = bfgs(f,paramf,x0[,tol,maxit,H0]) Optimisation by BFGS (quasi-Newton)
%
% Reference: p. 194ff in:
%   Nocedal and Wright: "Numerical Optimization", Springer, 1999.
%
% Example usage: see steepdesc.m.
%
% In:
%   f: a handle to a function that takes as input a list of vectors and
%      returns 3 lists: the function values, the gradients and the Hessians
%      (see rosenbrock.m for an example in 2D).
%   paramf: cell array containing the parameters for f. Use {} to pass no
%      parameters, or to use f's default parameters.
%   x0: n x 1 vector, the starting point.
%   tol: tolerance to stop iterating (minimum gradient norm). Default: 1e-5.
%   maxit: maximum number of iterations. Default: 1000.
%   H0: n x n matrix, initial inverse Hessian approximation. Default: eye(n).
% Out:
%   X: ? x n list of iterates.
%   F: ? x 1 list of the function value at the iterates.
%
% Any non-mandatory argument can be given the value [] to force it to take
% its default value.

% Copyright (c) 2005 by Miguel A. Carreira-Perpinan

function [X,F] = bfgs(f,paramf,x0,tol,maxit,H0)

% ---------- Argument defaults ----------
if ~exist('tol','var') | isempty(tol) tol = 1e-5; end;
if ~exist('maxit','var') | isempty(maxit) maxit = 1000; end;
n = length(x0);
if ~exist('H0','var') | isempty(H0) H0 = eye(n,n); end;
% ---------- End of "argument defaults" ----------

