How can i multiply each element of structured array

5 ビュー (過去 30 日間)
NN
NN 2021 年 7 月 28 日
コメント済み: NN 2021 年 8 月 2 日
I am trying to do an optimization problem in matlab.
below is the objective function .Here second and third term Price and Nb_dch1V, Price and Nb_ch1V are struct arrays (1 x 1 struct)
with two fields :
time 288 x 1 double
signal 1 x 1 struct
Signal again has two fields :
values 288 x 241 double
dimensions 241 .
hence is this objective function correct?
I am getting error
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Objective must be a scalar OptimizationExpression or a struct containing a scalar OptimizationExpression.
Please help
prob.Objective =dt*Price'*PbattupsV +dt*Price'*PbattdchV*Nb_dch1V'+ dt*Price'*PbattchV*Nb_ch1V' ;

回答 (2 件)

Alan Weiss
Alan Weiss 2021 年 7 月 28 日
編集済み: Alan Weiss 2021 年 7 月 28 日
You need to extract the correct elements from your structures before using them. I am not sure what you mean to have as an objective. Maybe this:
var1 = dt*Price.time'*PbattupsV; % Check that this is a scalar expression
var2 = dt*Price.time'*PbattdchV*Nb_dch1V.time'; % Check that this is a scalar expression
var3 = dt*Price.time'*PbattchV*Nb_ch1V.time'; % Check that this is a scalar expression
prob.Objective = var1 + var2 + var3;
Alan Weiss
MATLAB mathematical toolbox documentation
  3 件のコメント
NN
NN 2021 年 7 月 28 日
and if i change Price.time to Price , it gives the same errror as before :
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • Objective must be a scalar OptimizationExpression or a struct containing a scalar OptimizationExpression.
NN
NN 2021 年 7 月 29 日
編集済み: NN 2021 年 7 月 29 日
I have tried this way also,
var2 = dt.*Price'.*PbattdchV;
var3 = dt.*Price'.*PbattchV;
prob.Objective =dt.*Price'.*PbattupsV+(var2.*Nb_dch1')+ (var3.*Nb_ch1');
but this gives the error :
An error occurred while running the simulation and the simulation was terminated
Caused by:
Argument dimensions 1-by-241 and 241-by-1 must agree.
Kindly advice

サインインしてコメントする。


Alan Weiss
Alan Weiss 2021 年 7 月 29 日
I'm sorry that you are having these problems, but you must realize that all of them are due to size mismatches in your structures. I am not sure why you are using structures; it might be easier if you simply used matrix variables.
In any case, if you have a 1-by-241 variable and a 241-by-1 variable that you are trying to add, then you should convert both to the same size. There are many ways of converting vectors. If M is the 1-by-241 variable, you can convert it to a 241-by-1 variable in any of the following ways:
M = M(:); % might be the best
M = M';
M = M.';
M = reshape(M,241,1);
M = reshape(M,[],1); % might be the best
M = reshape(M,[241,1]);
Use whichever you like.
Alan Weiss
MATLAB mathematical toolbox documentation
  4 件のコメント
Alan Weiss
Alan Weiss 2021 年 8 月 1 日
I suggest that you learn to use the debugger. Set a break point before you create prob.Objective. Carefully examine all your variables. The objective must be a scalar.
Alan Weiss
MATLAB mathematical toolbox documentation
NN
NN 2021 年 8 月 2 日
sure. will do that.Thank you very much for the support

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeManual Performance Optimization についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by