How can i multiply each element of structured array

조회 수: 7 (최근 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 CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by