(the equation has been written in latex format)
\begin{equation}
\nabla_X f(X) = \begin{bmatrix}
0 & & \lambda_1\max\{0,2X_{1,2}\} & & 0 & & \cdots & & 0 \\
0 & & 0 & & \lambda_2\max\{0,2X_{2,3}\} & & \cdots & & 0 \\
\vdots & & \vdots & & & & & & \vdots \\
0 & & 0 & & 0 & & \cdots & & \lambda_n\max\{0,2X_{n-1,n}\} \\
\end{bmatrix}
\end{equation}
Should I directly define it or is there any other smarter way to do it?
\begin{equation}
\nabla_X f(X) = \begin{bmatrix}
0 & & \lambda_1\max\{0,2X_{1,2}\} & & 0 & & \cdots & & 0 \\
0 & & 0 & & \lambda_2\max\{0,2X_{2,3}\} & & \cdots & & 0 \\
\vdots & & \vdots & & & & & & \vdots \\
0 & & 0 & & 0 & & \cdots & & \lambda_{n-1} \max\{0,2X_{n-1,n}\} \\
0 & & 0 & & 0 & & \cdots & & 0
\end{bmatrix}
\end{equation}
Thanks for your input, and yes the gradient matrix is sparse. Only (n-1) non-zero elements.
----------------------------------x---------------------x-------------------
Error using trace (line 13)
Matrix must be square.
Error in Prog>@(X)trace(B*X) (line 11)
problem.cost = @(X) trace(B*X);
Error in getCost (line 38)
cost = problem.cost(x);
Error in getCostGrad (line 56)
cost = getCost(problem, x, storedb, key);
Error in trustregions (line 388)
[fx, fgradx] = getCostGrad(problem, x, storedb, key);
Error in Prog (line 14)
[u0, xcost, info, options] = trustregions(problem);
------------------------x-------------------x-----------------------------
Do you have any idea why I am getting this error? By the way, I checked my input matrix dimension, and it is a square symmetric matrix.
The code is as follows-
---------------------------x-----------------x-----------------------------
function [u0,xcost,info,options] = Prog(B)
% Input:
% B - Symmetric matrix
%% Initialize few parameters
n = size(B,1);
%% MANOPT
% Create the problem structure.
manifold = spectrahedronfactory(n,1);
problem.M = manifold;
% Define the problem cost function and its Euclidean gradient.
problem.cost = @(X) trace(B*X);
problem.egrad = @(X) B;
% Solve.
[u0, xcost, info, options] = trustregions(problem);
end
---------------------------x------------x------------------------------