How can I solve the problem? Cannot perform the operation: {real affine} .* {convex}

This is my code

``````clear

clc

A_1=[0.1 0;

0.2 0.14];

B_1=[0.32;

-1.7];

A_2=[0.36 0.1;

0 2];

B_2=[0.32;

-2];

A_3=[0.1 0;

0.1 0.32];

B_3=[0.25;

0.45];

A_4=[-0.33 -0.1;

0 1.63];

B_4=[0.22;

0.61];

A_5=[0.25 0;

0.2 2.95];

B_5=[0.12;

0.53];

%%%%%给定的半马的平稳分布

PI_1=0.21;

PI_2=0.19;

PI_3=0.2;

PI_4=0.25;

PI_5=0.15;

%%%假设半马的驻留时间

tau_1=0.07;

tau_2=0.04;

tau_3=0.03;

tau_4=0.02;

tau_5=0.025;

LAMBDA_1=1;

LAMBDA_2=5;

LAMBDA_3=-0.1;

LAMBDA_4=-2;

LAMBDA_5=-0.8;

GAMMA_1=0.1;

GAMMA_2=1;

GAMMA_3=0.2;

GAMMA_4=0.1;

GAMMA_5=0.1;

MU_1=1.005;

MU_2=1.01;

MU_3=1.009;

MU_4=1.001;

MU_5=1.004;

ALPHA_1=0.34;

ALPHA_2=0.45;

ALPHA_3=0.10;

ALPHA_4=0.44;

ALPHA_5=0.88;

sigmma=0.2

%%%求最终的平稳分布

p=PI_1*tau_1+PI_2*tau_2+PI_3*tau_3+PI_4*tau_4+PI_5*tau_5;

p1=PI_1*tau_1/p;

p2=PI_2*tau_2/p;

p3=PI_3*tau_3/p;

p4=PI_4*tau_4/p;

p5=PI_5*tau_5/p;

%%%转移概率矩阵

PI=[0 0.1 0.25 0.25 0.4;

0.1 0 0.2 0.4 0.3;

0.2 0.35 0 0.3 0.15;

0.15 0.3 0.2 0 0.35;

0.3 0.15 0.25 0.3 0];

[nn,mm]=size(B_1);

%%%%LMI

setlmis([])

X1=lmivar(1,[nn 1]);

X2=lmivar(1,[nn 1]);

X3=lmivar(1,[nn 1]);

X4=lmivar(1,[nn 1]);

X5=lmivar(1,[nn 1]);

Y1=lmivar(2,[mm nn]);

Y2=lmivar(2,[mm nn]);

Y3=lmivar(2,[mm nn]);

Y4=lmivar(2,[mm nn]);

Y5=lmivar(2,[mm nn]);

%%%

lmiterm([1 1 1 X1],-1,1);%x1>0

lmiterm([2 1 1 X2],-1,1);%x2>0

lmiterm([3 1 1 X3],-1,1);%x3>0

lmiterm([4 1 1 X4],-1,1);%x4>0

lmiterm([5 1 1 X5],-1,1);%x5>0

%%第一个模态

lmiterm([6 1 1 X1],A_1,1,'s');

lmiterm([6 1 1 Y1],B_1,1,'s');

lmiterm([6 1 1 X1],LAMBDA_1,-1);

lmiterm([7 1 1 X2],-MU_1,1);

lmiterm([7 1 2 X2],1,1);

lmiterm([7 2 2 X1],-1,1);

lmiterm([8 1 1 X3],-MU_1,1);

lmiterm([8 1 2 X3],1,1);

lmiterm([8 2 2 X1],-1,1);

lmiterm([9 1 1 X4],-MU_1,1);

lmiterm([9 1 2 X4],1,1);

lmiterm([9 2 2 X1],-1,1);

lmiterm([10 1 1 X5],-MU_1,1);

lmiterm([10 1 2 X5],1,1);

lmiterm([10 2 2 X1],-1,1);

%%第二个模态

lmiterm([11 1 1 X2],A_2,1,'s');

lmiterm([11 1 1 Y2],B_2,1,'s');

lmiterm([11 1 1 X2],LAMBDA_2,-1);

lmiterm([12 1 1 X1],-MU_2,1);

lmiterm([12 1 2 X1],1,1);

lmiterm([12 2 2 X2],-1,1);

lmiterm([13 1 1 X3],-MU_2,1);

lmiterm([13 1 2 X3],1,1);

lmiterm([13 2 2 X2],-1,1);

lmiterm([14 1 1 X4],-MU_2,1);

lmiterm([14 1 2 X4],1,1);

lmiterm([14 2 2 X2],-1,1);

lmiterm([15 1 1 X5],-MU_2,1);

lmiterm([15 1 2 X5],1,1);

lmiterm([15 2 2 X2],-1,1);

%%第三个模态

lmiterm([16 1 1 X3],A_3,1,'s');

lmiterm([16 1 1 Y3],B_3,1,'s');

lmiterm([16 1 1 X3],LAMBDA_3,-1);

lmiterm([17 1 1 X2],-MU_3,1);

lmiterm([17 1 2 X2],1,1);

lmiterm([17 2 2 X3],-1,1);

lmiterm([18 1 1 X1],-MU_3,1);

lmiterm([18 1 2 X1],1,1);

lmiterm([18 2 2 X3],-1,1);

lmiterm([19 1 1 X4],-MU_3,1);

lmiterm([19 1 2 X4],1,1);

lmiterm([19 2 2 X3],-1,1);

lmiterm([20 1 1 X5],-MU_3,1);

lmiterm([20 1 2 X5],1,1);

lmiterm([20 2 2 X3],-1,1);

%%第四个模态

lmiterm([21 1 1 X4],A_4,1,'s');

lmiterm([21 1 1 Y4],B_4,1,'s');

lmiterm([21 1 1 X4],LAMBDA_4,-1);

lmiterm([22 1 1 X2],-MU_4,1);

lmiterm([22 1 2 X2],1,1);

lmiterm([22 2 2 X4],-1,1);

lmiterm([23 1 1 X3],-MU_4,1);

lmiterm([23 1 2 X3],1,1);

lmiterm([23 2 2 X4],-1,1);

lmiterm([24 1 1 X1],-MU_4,1);

lmiterm([24 1 2 X1],1,1);

lmiterm([24 2 2 X4],-1,1);

lmiterm([25 1 1 X5],-MU_4,1);

lmiterm([25 1 2 X5],1,1);

lmiterm([25 2 2 X4],-1,1);

%%第五个模态

lmiterm([26 1 1 X5],A_5,1,'s');

lmiterm([26 1 1 Y5],B_5,1,'s');

lmiterm([26 1 1 X5],LAMBDA_5,-1);

lmiterm([27 1 1 X2],-MU_5,1);

lmiterm([27 1 2 X2],1,1);

lmiterm([27 2 2 X5],-1,1);

lmiterm([28 1 1 X3],-MU_5,1);

lmiterm([28 1 2 X3],1,1);

lmiterm([28 2 2 X5],-1,1);

lmiterm([29 1 1 X4],-MU_5,1);

lmiterm([29 1 2 X4],1,1);

lmiterm([29 2 2 X5],-1,1);

lmiterm([30 1 1 X1],-MU_5,1);

lmiterm([30 1 2 X1],1,1);

lmiterm([30 2 2 X5],-1,1);

lmis=getlmis;

[tmin,xfeas]=feasp(lmis);

% [tmin,xfeas]=feasp(lmis);

X_1=dec2mat(lmis,xfeas,X1);

X_2=dec2mat(lmis,xfeas,X2);

X_3=dec2mat(lmis,xfeas,X3);

X_4=dec2mat(lmis,xfeas,X4);

X_5=dec2mat(lmis,xfeas,X5);

Y_1=dec2mat(lmis,xfeas,Y1);

Y_2=dec2mat(lmis,xfeas,Y2);

Y_3=dec2mat(lmis,xfeas,Y3);

Y_4=dec2mat(lmis,xfeas,Y4);

Y_5=dec2mat(lmis,xfeas,Y5);

P_1=inv(X_1);

P_2=inv(X_2);

P_3=inv(X_3);

P_4=inv(X_4);

P_5=inv(X_5);

K_1=Y_1*P_1;

K_2=Y_2*P_2;

K_3=Y_3*P_3;

K_4=Y_4*P_4;

K_5=Y_5*P_5;

A.A_1=A_1;

B.B_1=B_1;

K.K_1=K_1;

A.A_2=A_2;

B.B_2=B_2;

K.K_2=K_2;

A.A_3=A_3;

B.B_3=B_3;

K.K_3=K_3;

A.A_4=A_4;

B.B_4=B_4;

K.K_4=K_4;

A.A_5=A_5;

B.B_5=B_5;

K.K_5=K_5;

P.P_1 = P_1;

P.P_2 = P_2;

P.P_3 = P_3;

P.P_4 = P_4;

P.P_5 = P_5;

I=eye(2,2);

cvx_begin

variable a1 a2 a3 a4 a5

variable t nonnegative

variable gamma1 nonnegative

minimize(square_pos(norm(a1*K.K_1,2))+square_pos(norm(a2*K.K_2+a3*K.K_3,2))+square_pos(norm(a2*K.K_2+a3*K.K_3,2))+square_pos(norm(a4*K.K_4+a5*K.K_5,2))+square_pos(norm(a4*K.K_4+a5*K.K_5,2))+gamma1*norm(a1))

cvx_end
``````

When I use cvx, I always have a problem with Cannot perform the operation: {real affine} .* {convex}.
This problem arises due to `gamma1*norm(a1)`. How to solve the problem: Cannot perform the operation: {real affine} .* {convex}

That is non-convex.

However, unless you just chose not to show the constraints (other than `t` and `gamma1` being nonnegative), a globally optimal solution is to choose all variables to be zero. But your code doesn’t even use all the declared CVX variables, and you included a bunch of unused input data, so I have no idea what the actual optimization problem you want to solve is.

Depending on what that actual problem is, perhaps `gamma` can be handled parametrically, i.e., call CVX within a for loop, using a different value of `gamma` on each CVX invocation. This is a standard kind of thing in cross-validation or hyperparameter tuning. Then choose the value of `gamma` which provides the best (lowest) optimal objective value; and use the corresponding optimal variable values (argmin) for that value of `gamma1`.