I have no idea what mathematical problem you are trying to solve. You haven’t shown us the mathematical description of the relation between B
and S_{1,k} and S_{2,k}. We only have your messy code. Does that result in convex optimization problem? I have no idea.