Why do I get this error?I think there is no error in my writing, but NaN affects the operation of my program afterwards, how can I avoid it?

When one CVX problem (outer for loop iteration) fails, the variable values are populated with NaN. When you use this variable value as input data for the next CVX problem, there is nan in a CVX expression, which results in the error.

If you have access to Mosek 9.x, use that with CVX 2.2 Otherwise, follow the directions at CVXQUAD: How to use CVXQUAD's Pade Approximant instead of CVX's unreliable Successive Approximation for GP mode, log, exp, entr, rel_entr, kl_div, log_det, det_rootn, exponential cone. CVXQUAD's Quantum (Matrix) Entropy & Matrix Log related functions . This will improves the chances that CVX solves a problem. However, unsafeguarded SCA is a very unreliable algorithm, and you may be better off using a non-convex solver available under YALMIP. This forum is filled with people who unsuccessfully tried to apply unsfaeguarded SCA.