importmanopt
n = 1000;
A = randn(n);
A = .5*(A+A.');
% Create the problem structure.
manifold = spherefactory(n);
problem.M = manifold;
problem.cost = @mycost; % Cost function
problem.egrad = @myegrad; % Euclidean gradient of the cost function
% Numerically check gradient consistency (optional).
checkgradient(problem);
% Solve.
[x, xcost, info, options] = trustregions(problem);
function [f, store] = mycost(A, x, store)
if ~isfield(store, 'Ax')
store.Ax = A*x; % The store memory is associated to a specific x
end
Ax = store.Ax;
f = -x'*Ax; % No need to cache f: cost values are cached 'under the hood'
end
function [g, store] = myegrad(A, x, store)
% This could be placed in a separate function
% to avoid code duplication.
if ~isfield(store, 'Ax')
store.Ax = A*x;
end
Ax = store.Ax;
% Euclidean gradient; this is also cached 'under the hood'.
g = -2*Ax;
end
problem.cost = @(x, store) mycost(A, x, store); % Cost function
problem.egrad = @(x, store) myegrad(A, x, store); % Euclidean gradient of the cost function
steepestdescent(problem,[],options.tolgradnorm = 1e-4)
It will not work!
options = struct(); % this line is optional, but it's good practice.
options.tolgradnorm = 1e-4;
steepestdescent(problem, [], options);