# Problems applying CVX to multiple dimensions

I’m a newbie in CVX. I tried to solve the problem of a subtraction of two convex functions using the Taylor expansion and CCCP algorithms.

Before that, I wrote a piece of code that was simple and similar to the problem:

``````%%The objective function is x^4-x^2
%%The minimize is -0.25
% x=-5:0.0001:5;
% y=x.^4-x.^2;
% plot(x,y)
%%main

x0 = 5;
jj = 1;
while (1)
cvx_begin quiet;
variable x(2,1);
g = (x.^4-(x0.^2+2.*x0.*(x-x0)));
minimize g;
cvx_end;
f1 = x^4-x^2;
x0 =x;

cvx_begin quiet;
variable x;
g = (x^4-(x0^2+2*x0*(x-x0)));
minimize g;
cvx_end;
f2 = x^4-x^2;

J(jj)=jj;
F2(jj)=f2;
jj=jj+1;

if(abs(f1-f2)<=0.001)   % Set the accuracy threshold
break
end
end
x
f2
plot(J,F2)
``````

Output：

``````x =

0.7128

f2 =

-0.2499
``````

This is good, but I had a problem expanding x0 and x to (1,2).

``````x0 = [5;5];                              %Modification
jj = 1;
while (1)
cvx_begin quiet;
variable x(2,1);                     %Modification
g = (x.^4-(x0.^2+2.*x0.*(x-x0)));
minimize g;
cvx_end;
f1 = x^4-x^2;
x0 =x;

cvx_begin quiet;
variable x(2,1);                     %Modification
g = (x^4-(x0^2+2*x0*(x-x0)));
minimize g;
cvx_end;
f2 = x^4-x^2;

J(jj)=jj;
F2(jj)=f2;
jj=jj+1;

if(abs(f1-f2)<=0.001)   % Set the accuracy threshold
break
end
end
``````

“Your objective function is not a scalar.”

If x` `were a 2 by 1 MATLAB double precision variable instead of a scalar, then x.^2 would be a vector. The same holds if `x` is a 2 by 1 CVX variable. So the objective function winds up being a vector.

CVX requires the objective function evaluate to a scalar. Sp x`'*x` could be an objective function. Or `sum(x.^2)`.

Thank you very much !

This is the code I modified:

``````x0 = [5,-5];
while (1)
cvx_begin quiet;
variable x(1,2);
g = sum(x.^4-(x0.^2+2.*x0.*(x-x0)));
%     g = x.^4-(x0.^2+2.*x0.*(x-x0));      %before
minimize g;
cvx_end;
f1 = x.^4-x.^2;
x0 =x;

cvx_begin quiet;
variable x(1,2);
g = sum(x.^4-(x0.^2+2.*x0.*(x-x0)));
%     g = x.^4-(x0.^2+2.*x0.*(x-x0));      %before
minimize g;
cvx_end;
f2 = x.^4-x.^2;

if(abs(f1-f2)<=0.000001)   % Set the accuracy threshold
break
end
end
``````

Output:

``````x =

0.7073   -0.7073

f2 =

-0.2500   -0.2500
``````

The minimum value of the function is -0.25, and the corresponding x is plus or minus 1/root sign (2)

That’s great. I will try to solve more complicated problems with this idea.

Thank you.