# How can formulate difference between to variable

hello. I have a expreesion I1(t)-I2(t) , that I1(t) and I2(t) are variables . cvx makes error:
Error using + (line 83)
Disciplined convex programming error:
Illegal operation: {log-affine} - {log-affine}

Error in - (line 21)
z = plus( x, y, true, cheat );

Error in Untitled8 (line 92)
I1(t)-I2(t)>=1;
best regard- morteza

`{log-affine} - {log-affine}` is nor allowed by CVX. Read the FAQ Why isn’t CVX accepting my model? READ THIS FIRST! and the CVX Users’ Guide http://cvxr.com/cvx/doc/dcp.html .and also the answer by mxg at Log of sigmoid function .

If you show us your problem, maybe we can determine whether there is a reformulation which CVX will accept. But you should read the material I linked, and try to determine whether you have a convex problem, hopefully which can be formulated consistent with CVX’s DCP rules.

hi. my problem is:
minimize(suc1*(I1(t)-I1(t-1)).^2)
subject to
I1(t)>=c0*(I1(t)-I1(t-1));
thanks

I’m not sure what you are doing. Here is what i did, and it works. Is this what you want? Perhaps your t is greater than 2, but the way you have specified the problem, there are only 2 different components of I1 which are used.

``````t = 2; suc1=1; c0 = 1;
cvx_begin
variable I1(2)
minimize(suc1*(I1(t)-I1(t-1)).^2)
I1(t)>=c0*(I1(t)-I1(t-1))
cvx_end
``````

Here is the output

``````    Calling SDPT3 4.0: 4 variables, 1 equality constraints
------------------------------------------------------------

num. of constraints =  1
dim. of sdp    var  =  2,   num. of sdp  blk  =  1
dim. of linear var  =  1  2th semidefinite block is actually diagonal

*******************************************************************
SDPT3: Infeasible path-following algorithms
*******************************************************************
version  predcorr  gam  expon  scale_data
HKM      1      0.000   1        0
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
0|0.000|0.000|4.5e+00|8.4e+00|3.0e+02| 1.000000e+01  0.000000e+00| 0:0:00| chol  1  1
1|1.000|1.000|1.5e-06|8.7e-02|1.8e+01| 7.923866e+00 -8.002861e+00| 0:0:00| chol  1  1
2|0.996|0.989|5.4e-08|9.5e-03|2.6e-01| 8.636799e-02 -7.734601e-02| 0:0:00| chol  1  1
3|0.989|0.989|1.8e-08|9.6e-04|9.2e-03| 9.489746e-04  1.385119e-04| 0:0:00| chol  1  1
4|0.991|0.991|1.0e-10|9.5e-05|6.2e-04| 1.042778e-05  9.886934e-05| 0:0:00| chol  1  1
5|1.000|1.000|1.4e-10|8.7e-06|4.5e-05| 1.717404e-07  9.828241e-06| 0:0:00| chol  1  1
6|0.989|0.989|9.1e-12|9.4e-08|5.0e-07| 1.871970e-09  1.071081e-07| 0:0:00| chol  1  1
7|1.000|0.989|4.3e-14|1.0e-09|9.1e-09| 1.815563e-09 -6.186786e-10| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
number of iterations   =  7
primal objective value =  1.81556305e-09
dual   objective value = -6.18678637e-10
gap := trace(XZ)       = 9.07e-09
relative gap           = 9.07e-09
actual relative gap    = 2.43e-09
rel. primal infeas (scaled problem)   = 4.32e-14
rel. dual     "        "       "      = 1.04e-09
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual     "        "       "      = 0.00e+00
norm(X), norm(y), norm(Z) = 4.6e+00, 6.2e-10, 1.0e+00
norm(A), norm(b), norm(C) = 2.0e+00, 2.0e+00, 2.0e+00
Total CPU time (secs)  = 0.04
CPU time per iteration = 0.01
termination code       =  0
DIMACS: 4.3e-14  0.0e+00  1.0e-09  0.0e+00  2.4e-09  9.1e-09
-------------------------------------------------------------------

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.81556e-09

>>  disp(I1)
4.531035615038723
4.531035615038723
``````

The error messages in your first post don’t match with what you wrirte in your second post, so maybe you didn’t correctly write what you wanted in the second post?

dear mark , thank you very much.

hello. the above problem is running corectly. but when I use it in the The following problem, cvx makes error.
a0=0.045;a1=0.56;a2=1.3;
b0=0.87;b1=2.1;
c0=0.45;c1=3.4;n=6;
suc1=0.8;suc2=0.64;suc3=0.81;
sdc1=0.51;sdc2=0.13;sdc3=0.47;
q=0.97;g=1.13;
ir=4;blt=15;yp=0.88;yw=0.76;
d0=0.079;d1=1.26;pr1=100;r1(n)=0, r2(n)=0, r3(n)=0, r4(n)=0, r5(n)=0 ,r6(n)=0, r7(n)=0;
a11=0.15;a22=0.84;a33=1;
b11=0.19;b21=0.56;b31=0.94;b12=0.69;b22=1.23;b32=0.67;
b13=0.34;b23=0.61;b33=1.9;
% w(7)=5;

for t=[2 3 4 5 6 7]
x([2 3 4 5 6 7])=[25 210 150 200 300 187]’;

% I1(t-1)=0;
cvx_begin gp
variables p1(t) p2(t) p3(t) p4(t) p5(t)
variables I1(2) I2(2) I3(2) I4(t) I5(t)
variables pb(t) wb(t) w(t)

minimize((a0p1(t).^2+a1p1(t)+a2).I1(t)+…
(b0
p2(t)+b1).I2(t)+…
(c0
p3(t)+c1).I3(t)+q(p1(t).I1(t)+p2(t).I2(t)+p3(t).I3(t)+p4(t).I4(t)…
+p5(t).I5(t))+a11(b11
p1(t).I1(t)+b21p2(t).I2(t)+b31p3(t).I3(t))…
+a22
(b12
p1(t).I1(t)+b22p2(t).I2(t)+b32p3(t).I3(t))+a33(b13
p1(t).I1(t)…
+b23
p2(t).I2(t)+b33p3(t).I3(t))+ (suc1(I1(t)-I1(t-1)).^2)+(suc2
(I2(t)-I2(t-1)).^2)+(suc3*(I3(t)-I3(t-1)).^2)…
+(sdc1*(I1(t)-I1(t-1)).^2)+(sdc2*(I2(t)-I2(t-1)).^2)+(sdc3*(I3(t)-I3(t-1)).^2+(50/365*((ir.(1+ir).^blt)…
/((1+ir).^blt-1))
((yppb(t)+ywwb(t))+(yp+yw)pb(t)))+g(d0pr1+d1p1(t)).I1(t) )
subject to
5
I1(t)<=p1(t);
p1(t)<=100I1(t);
4
I2(t)<=p2(t);
p2(t)<=50*I2(t);
5<=p3(t)*I3(t);
p3(t)*I3(t)<=40;
6<=p4(t)*I4(t);
p4(t)*I4(t)<=45;
8<=p5(t)*I5(t);
p5(t)I5(t)<=35;
pb(t)I2(t)>=-60;
pb(t)I2(t)<=50;
-60<=wb(t);
wb(t)<=60;
0<=I1(t);
I1(t)<=1;
0<=I2(t);
I2(t)<=1;
0<=I3(t);
I3(t)<=1;
0<=I4(t);
I4(t)<=1;
0<=I5(t);
I5(t)<=1;
I1(t)>=c0
(I1(t)-I1(t-1));
I2(t)>=c0
(I2(t)-I2(t-1));
I3(t)>=c0
(I3(t)-I3(t-1));
p1(t)*I1(t)+p2(t)*I2(t)+p3(t)*I3(t)+p4(t)*I4(t)+p5(t)*I5(t)<=x(t);
cvx_end
end
the error is:

Error using + (line 83)
Disciplined convex programming error:
Illegal operation: {log-affine} - {log-affine}

Error in - (line 21)
z = plus( x, y, true, cheat );

Error in Untitled8 (line 41)
minimize((a0p1(t).^2+a1p1(t)+a2).*I1(t)+…