# How can i multiply each element of structured array

11 visualizaciones (últimos 30 días)
NN el 28 de Jul. de 2021
Comentada: NN el 2 de Ag. de 2021
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.
prob.Objective =dt*Price'*PbattupsV +dt*Price'*PbattdchV*Nb_dch1V'+ dt*Price'*PbattchV*Nb_ch1V' ;
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (2)

Alan Weiss el 28 de Jul. de 2021
Editada: Alan Weiss el 28 de Jul. de 2021
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 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
NN el 28 de Jul. de 2021
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 el 29 de Jul. de 2021
Editada: NN el 29 de Jul. de 2021
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.

Iniciar sesión para comentar.

Alan Weiss el 29 de Jul. de 2021
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 comentariosMostrar 2 comentarios más antiguosOcultar 2 comentarios más antiguos
Alan Weiss el 1 de Ag. de 2021
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 el 2 de Ag. de 2021
sure. will do that.Thank you very much for the support

Iniciar sesión para comentar.

### Categorías

Más información sobre Simulink en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by