Sequential convex programming

The following is a sequential convex programming.
However,the objective function is made of expressions. When I check the final value of sum(Q), it does
not equal the value calculated by myself. Could anyone please tell me the reason?
K=9;g=50rand(K,K);noise=1; pk=20rand(K,1);

for m=1:1:20

  cvx_begin
      
  variable p(K) nonnegative
      
  expression part_1(K,1)
      
  expression part_2(K,1)
      
  expression part_3(K,1)
           
  expression Q(K,1)
             
             for k=1:1:K  
               
                for j=1:1:K
                    if j~=k
                      part_1(k)=part_1(k)+g(j,k)*p(j);
                    end
                end
                part_1(k)=part_1(k)+g(k,k)*p(k)+noise;
                for j=1:1:K
                    if j~=k
                      part_2(k)=part_2(k)+g(j,k)*pk(j);
                    end
                end
                for j=1:1:K
                    if j~=k
                       part_3(k)=part_3(k)+g(j,k)/(part_2(k)+noise)*(p(j)-pk(j));
                    end
                end
                Q(k)=Q(k)-log(part_1(k))+part_3(k)+log(part_2(k)+noise);
             end
      minimize (sum(Q)) 
      subject to
              for l=1:1:K
                  p(k)<= 20
              end        
 cvx_end    

pk=p;

end

What exactly is the discrepancy you are claiming? I ran this a couple of times (with different random numbers) with the solver SDPT3, and all the problems were successfully solved, and at the conclusion, sum(Q) was equal to cvx_optval. Are your problems being successfully solved?

Thank you for your reply ! However, when I tried some very small random numbers of g, such as g=10^(-7)*rand(K,K), the sum(Q) was not equal to cvx_optval again.

Sorry, I actually mean that the value of sum(Q0) is not equal to cvx_optval. the matlab procedure is as follows:
K=9;g=10^(-8)rand(K,K);noise=1; pk=20rand(K,1);

for m=1:1:3

cvx_begin

variable p(K) nonnegative

expression part_1(K,1)

expression part_2(K,1)

expression part_3(K,1)

expression Q(K,1)

         for k=1:1:K  

            for j=1:1:K
                if j~=k
                  part_1(k)=part_1(k)+g(j,k)*p(j);
                end
            end
            part_1(k)=part_1(k)+g(k,k)*p(k)+noise;
            for j=1:1:K
                if j~=k
                  part_2(k)=part_2(k)+g(j,k)*pk(j);
                end
            end
            for j=1:1:K
                if j~=k
                   part_3(k)=part_3(k)+g(j,k)/(part_2(k)+noise)*(p(j)-pk(j));
                end
            end
            Q(k)=Q(k)-log(part_1(k))+part_3(k)+log(part_2(k)+noise);
         end
  minimize (sum(Q)) 
  subject to
          for k=1:1:K
              p(k)<= 20
          end        

cvx_end

part_10=zeros(K,1);

part_20=zeros(K,1);

part_30=zeros(K,1);

Q0=zeros(K,1);

object=0;

         for k=1:1:K  

            for j=1:1:K
                if j~=k
                  part_10(k)=part_10(k)+g(j,k)*p(j);
                end
            end
            part_10(k)=part_10(k)+g(k,k)*p(k)+noise;
            for j=1:1:K
                if j~=k
                  part_20(k)=part_20(k)+g(j,k)*pk(j);
                end
            end
            for j=1:1:K
                if j~=k
                   part_30(k)=part_30(k)+g(j,k)/(part_20(k)+noise)*(p(j)-pk(j));
                end
            end
            Q0(k)=Q0(k)-log(part_10(k))+part_30(k)+log(part_20(k)+noise);
         end


       object=sum(Q0);  

pk=p;

end

Oh, I am so sorry. I actually wonder why sum(Q0), i.e., the value of ojective function calculated by myself, is not equal to cvx_optval. The whole matlab procedure is shown in the Answer 1. For example, after one iteration, i.e., m=1, sum(Q0) is -9.163*10^(-7), however, cvx_optval is -1.2971e-006.

I do get a (nontrivial) roundoff error difference between the Q and Q0 vectors. I will leave the explanation to others.

Hmm, those are very small numbers. I’m thinking they’re within the natural numerical error of the solvers. It is always best to scale your coefficients to be “near” unity. Numbers near 10^-7 are probably too small and will confuse the solvers. (They will think they are “basically” zero.)

Leaving aside merits of good scaling, what do solver tolerances or numerical errors have to do with this discrepancy? Is Q as computed by CVX numerically different than Q0 because of computation rearrangements/transformations by CVX, therefore causing roundoff differences between them?

Yes. CVX always transforms models to have a linear objective. So if your objective is nonlinear, it’s going to be moved to a constraint. Unless the constraint is exactly satisfied, cvx_optval will be different than the value calculated after the fact.

Yes, well that explains it. Thanks.