I have a questions about how do I use summation function sum( ).
As there are many versions on the internet, I want a work format.

The CVX version of `sum` operates the same way on variables as the standard MATLAB version does on normal matrices and arrays. This is true for virtually every function in CVX that mirrors a MATLAB equivalent.

For that reason, the MATLAB documentation on the function `sum` should be your resource. We can’t afford to offer the same level of documentation for these functions in CVX as MATLAB offers, so we rely on CVX users to already be accomplished MATLAB users.

Thanks for your help. May I have a sample to know how’s to convert a question into CVX?
Sample:

``````minimize   ∑i=1 to M  (Pi)^2, for example if M=3, (P1)^2+(P2)^2+(P3)^3, Pi is the variable which I want to optimize

Constraint:  ∑i=1 to M  Pi=1
∑i=1 to M  Pi*a*Ei<=b , where a,b are constant, Ei are also array of constant
0<=Pi<=1,  i=1,....M
``````

Btw, is it possible that I get the optimized result Pi, after code of cvx_end, cuz I need the optimized Pi values such as P1, P2, P3…etc to continue calculating in Matlab
Thanks for teaching

Ps I want to get back all Pi, such as P1,P2,P3 values which cause the minimized value of ∑i=1 to M (Pi)^2. Is there any way to do that since the cvx only display as following: “Status: Solved Optimal value (cvx_optval): +0.2”

The Problem as follow, I think it is a convex optimization problem, Is it really a convex?

minimize ∑i=1 to M (Pi)^2, for example if M=3, (P1)^2+(P2)^2+(P3)^3, Pi is the variable which I want to optimize

3 Constraint:

1. ∑i=1 to M Pi=1
2. ∑i=1 to M PiaEi<=b , where a,b are constant, Ei are also array of constant
3. 0<=Pi<=1, i=1,…M

My CVX coding as following:

``````a=0.0005;
b=5;
e={1,3,6,9,12};
x={0,0,0,0,0};
cvx_begin
obj=0;
sub=0;
sub1=0;
variable p(5,1)
for i=1:5
obj=obj+p(i)*p(i)
end
minimize (obj)
x=p                     //Used to output the optimized Pi, such as P1, P2, P3, P4 ,P5 to matlab for further computation
subject to
for i=1:5
sub=sub+p(i)
end
sub==1
for i=1:5
sub1=sub1+p(i)*a*e(i)//error occur on e(i), which is a constant array value set in the matlab code outside cvx
end
sub1<=b
0<=p
p<=1

cvx_end
``````

Is the above program obey the my problem requirement and constraint?

You are using cell arrays `{}` when you should be using numeric arrays `[]`. That’s probably the reason for the error.

We cannot be MATLAB tutors here. If you’re not comfortable in MATLAB it’s going to be quite frustrating to use CVX.

Otherwise, things look pretty good, although your objective is better expressed as `sum_square(p)` and the sum constraint as `sum(p)==1`.

Thank you Michael! You indeed help me a lot! Thanks!!!