# Optimize sum of quadratic forms

For an optimization problem I want to minimize the following cost function in order to find an optimal vector sequence $\{\mathbf{b}\}_{i = 0}^N$ for the vector $\mathbf{b}$ of size $n\times 1$:

$$\min_\mathbf{b} \sum_{i = 1}^N \mathbf{a}_i^TA\mathbf{a}_i + \mathbf{b}_i^TB\mathbf{b}_i$$

where A and B are diagonal matrices of size $n\times n$ which are given. $\mathbf{a}$ is also a vector sequence $\{\mathbf{a}\}_{i = 0}^N$ and is known. The vector sequence should be of size $n \times N$. The

Using Matlab command fmincon I tried to implement this using a for-loop for the objective function but then I receive only one vector for $\mathbf{b}$, no sequence.

function [ J ] = objective_function(b, a, A, B, N)
J = 0;
for i = 1:N
J = J + a(:,i)'*A*a(:,i) + b'*B*b;
end


Now in the case where $n = 2$, I am trying to stack the single quadratic forms inside matrices in order to express the objective function as something like this:

$$\sum_{i = 1}^N \mathbf{a}_i^TA\mathbf{a}_i + \mathbf{b}_i^TB\mathbf{b}_i =\\ a_{11}A_{11}a_{11} + a_{12}A_{22}a_{12} + a_{21}A_{11}a_{21} + a_{22}A_{22}a_{22} + ... + a_{N1}A_{11}a_{N1} + a_{N2}A_{22}a_{N2} + \\ b_{11}B_{11}b_{11} + b_{12}B_{22}b_{12} + b_{21}B_{11}b_{21} + b_{22}B_{22}b_{22} + ... + b_{N1}B_{11}b_{N1} + b_{N2}B_{22}b_{N2} = \\ tr \left(\begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \\ \vdots & \vdots \\ a_{N1} & a_{N2} \\ \end{pmatrix} \begin{pmatrix} A_{11} & 0 \\ 0 & A_{22} \\ \end{pmatrix} \begin{pmatrix} a_{11} & a_{21} & \dots & a_{N1}\\ a_{12} & a_{22} & \dots & a_{N2} \\ \end{pmatrix} \right) + \\ tr \left(\begin{pmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \\ \vdots & \vdots \\ b_{N1} & b_{N2} \\ \end{pmatrix} \begin{pmatrix} B_{11} & 0 \\ 0 & B_{22} \\ \end{pmatrix} \begin{pmatrix} b_{11} & b_{21} & \dots & b_{N1}\\ b_{12} & b_{22} & \dots & b_{N2} \\ \end{pmatrix} \right) = \\ tr \begin{pmatrix} a_{11}A_{11}a_{11} + a_{12}A_{22}a_{12} & a_{11}A_{11}a_{21} + a_{12}A_{22}a_{22} & \ldots & a_{11}A_{11}a_{N1} + a_{12}A_{22}a_{N2} \\ a_{21}A_{11}a_{11} + a_{22}A_{22}a_{12} & a_{21}A_{11}a_{21} + a_{22}A_{22}a_{22} & \ldots & a_{21}A_{11}a_{N1} + a_{22}A_{22}a_{N2} \\ \vdots & \vdots & \ddots & \vdots \\ a_{N1}A_{11}a_{11} + a_{N2}A_{22}a_{12} & a_{N1}A_{11}a_{21} + a_{N2}A_{22}a_{22} & \ldots & a_{N1}A_{11}a_{N1} + a_{N2}A_{22}a_{N2} \end{pmatrix} + \\ tr \begin{pmatrix} b_{11}B_{11}b_{11} + b_{12}B_{22}b_{12} & b_{11}B_{11}b_{21} + b_{12}B_{22}b_{22} & \ldots & b_{11}B_{11}b_{N1} + b_{12}B_{22}b_{N2} \\ b_{21}B_{11}b_{11} + b_{22}B_{22}b_{12} & b_{21}B_{11}a_{21} + b_{22}B_{22}b_{22} & \ldots & b_{21}A_{11}b_{N1} + b_{22}B_{22}b_{N2} \\ \vdots & \vdots & \ddots & \vdots \\ b_{N1}B_{11}b_{11} + b_{N2}B_{22}b_{12} & b_{N1}B_{11}b_{21} + b_{N2}B_{22}b_{22} & \ldots & b_{N1}A_{11}b_{N1} + b_{N2}B_{22}b_{N2} \end{pmatrix}$$

Here $a_{ij}$ referes to the i-th vector $\mathbf{a}$ and the j-th entry of $\mathbf{a}_i$. $tr$ is the trace of the matrices, because only the diagonal elements should be summed in order to get the same result as first equation.

My questions are:

Is there a better approach to express the objective function? Or is it possible to use a for loop with fmincon and get a optimized vector sequence $\{\mathbf{b}\}_{i = 0}^N$?

Why even introduce $a$ into the affair? It is fixed and can thus be removed from the expression. What you have left is a sum of quadratics, which is minimized with $b=0$ if the diagonal is non-negative, or unbounded otherwise.
If you want to express it nicely in $b$, stack all $b$ into one column vector, and create a matrix with $B$ repeated $N$ times on the block diagonal.
$$b1^TBb_1 + b_2^TBb_2 = \begin{pmatrix}b_1\\b_2\end{pmatrix}^T\begin{pmatrix}B & 0\\0 & B\end{pmatrix}\begin{pmatrix}b_1\\b_2\end{pmatrix}$$
Although this particular case is trivial as the solution is given by $b=0$ or unbounded, in general, if you only have a (convex) quadratic objective, there are much better alternatives than fmincon specialized to the case of quadratic programming (assuming the problem isn't trivially small, or nonconvex)