How can i multiply each element of structured array

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by