Let F(p,q) be
F(p,q) = sum(j, [ r(j) - sum(i, p(i,j)*q(i,j)) ]^2 )
Then dF/dp is the matrix A formed by elements
A(i,j) = 2 * [r(j) - sum(k, p(k,j)*q(k,j))] * (-q(i,j))
dF/dq is the matrix B formed by elements
B(i,j) = 2 * [r(j) - sum(k, p(k,j)*q(k,j))] * (-p(i,j))
2
solved Taking derivative of a function