How can I construct a objective function that sum from 1 to N?


(Xiao Nan) #1

just like this problem, how to write it in cvx?


(Burak) #2

Hi Xiao,
You can initiate a Cost Function(myJ) before the CVX Loop as

myJ = [ ];

Then, in CVX you can use the following Quadratic Cost(as an example) Function that sums from 1 to N;

cvx_begin
for ix = 1 : N
myJ = myJ + X(:,ix)'QX(:,ix) %Sum Symbol Cost function construction
end
subject to
.
.
.

minimize myJ
cvx_end

Hope it helps :slight_smile:

Regards,
Burak


(Xiao Nan) #3

Thank you for your answer, it really helps.
Because my result isn’t right, so I can’t sure if I write my subject like that is true, could you give me some advice?

variables x11(N) x12(N) u11(N) u12(N) y1(N)
obj1=0;
for k=1:N
obj1=obj1+[x11(k);x12(k)]’[2,0;0,2][x11(k);x12(k)]+[u11(k);u12(k)+y1(k)]’[u11(k);u12(k)+y1(k)]+nu(k)y1(k)
end
minimize(obj1);
subject to
x11(1:end)==Ap1(1,1)
[x11k;x11(1:end-1)]+Ap1(1,2)
[x12k;x12(1:end-1)]+Bp1(1,1)u11(1:end)+Bp1(1,2)(u12(1:end)+y1(1:end));
x12(1:end)==Ap1(2,1)[x11k;x11(1:end-1)]+Ap1(2,2)[x12k;x12(1:end-1)]+Bp1(2,1)u11(1:end)+Bp1(2,2)(u12(1:end)+y1(1:end));
cvx_end
s1=cvx_optval


(Xiao Nan) #4

OK, I have solved it, thank u so much.


(Mark L. Stone) #5

iskender9961 's proposed solution which includes
myJ = [];
would result in an empty objective, and therefore CVX would be solving a feasibility problem, i.e., returning any feasible solution.

I see in your solution you used
obj1 = 0
instead, i.e., initializing to 0 rather than []. So you have done that the right way. I haven’t checked the correctness of the rest of what you’ve done.