Above code is a part of optimization i am working on(Optimal control of hybrid energy storage system). While running the code, the values of variables(Pb, Psca,Esc,Psc) are zero til t=(tn-2) and incorrect values for Pb(tn-1) & Pb(tn).

Your objective and constraints only apply to the âtnâ elements - maybe thatâs not what you wantâŚ When the CVX portion of the code is run, tn has the last value it had in the for loop, which is 1000.

Pb which you set before the CVX portion of the code, is overwritten inside the CVX code by declaring it a CVX variable, which then has no relation to the previous Pb.

There may be other problems as well. I suggest you look at these comments, carefully lay out what your model is, then carefully implement it in MATLAB and CVX,

This is the exact code layout am working. Besides the decision variables, all other variables are given or calculated before the âcvx beginâ.

What I am trying to implement is,

Every variable is a function of t as t varies from t=1 to tn.

Even the non decision variables are also function of t(1:tn).

For final result i will have the plot for t vs Pb(t) and other variables.

As of now, the CVX runs, either it shows inaccurate/solved or sometimes solved but not the expected values.

This is based on the Penalty Function approximation given in CVX examples. I have checked most of the examples regarding the same and still checking if anyone has done similar in the forum. If you have come across similar optimization problem then please link the same.

Note: The problem was solved using CVX by the author of the paper. But i am sure my model layout is wrong.

The code is running for âtnâ elements by adding one "âforâ loop inside the CVX portion. Even though not the expected results, but its close to the expected one. Will have to carefully look at the mathematical model for any errors.

Is your objective function supposed to only be in terms of the 1000th and 999th elements of various vectors? I donât know, but thatâs what you have done. You are allowed to use sum or a for loop to build up an objective function expression prior to the minimize statement.

You can use a for loop for the constraint, and perhaps you need to do so forEsc(tn)-Esc(tn-delta)== -Psc(tn)*delta;. But for instance,Pb+Psca+Preq== 0; without use of a for loop will impose this equality on all 1000 elements, i.e., specifies 1000 inequalities. Similarly, Esc_min<=Esc<=Esc_max; .

You need to figure out what you are doing with Pb. As I wrote, youâre setting of Pb before cvx_begin is overridden once you declare it a CVX variable after cvx_begin.