Array indices must be positive integers or logical values

So i have the following Constraints for a heat storage (ws) as part of my yalmip optimization problem:
Constraints=[]
for t = 1:24
Constraints = [Constraints, dE(t) <= dEmax_WS, E_ws(t) <= Emax_ws, E_ws(t) == E_ws(t-1)+dE(t), E_ws(0) == 10];
end
When running, i get an indexing error:
"Error using sdpvar/subsref (line 161) -Array indices must be positive integers or logical values."
I assume the problem is the last equation, which should determine the current state of charge by adding the charge rate to the charge of the previous time step.
I know one way to change this is to start the loop at t=2 and define E_ws(1)=10. Is there another smoother way which lets me keep the equation in the same loop as the "regular" constraints?
Thank you!

 채택된 답변

KSSV
KSSV 2023년 1월 16일
This line:
E_ws(0) == 10
gives you error. MATLAB indexing starts from 1. It cannot be zero. Think of it.

댓글 수: 4

Hey @KSSV,
sorry, that part E(0)=10 was actually not supposed to be in there (was part of my trial and error). But without that line i still get the same error.
As mentioned in the last part of my post, i already managed to solve it by indexing E_ws(1)=10 and starting the loop at t=2. However this is really annoying and i am looking for a way to implement the condition together with the regular conditions in the same for loop.
Hope that somehow makes sense. Sorry for the misunderstanding and also the confusing way to ask the question.
This line:
E_ws(t) == E_ws(t-1)+dE(t)
t-1 will b3 0 when t = 1 in the loop.
I tried this as well, but I am still getting the same error:
clear all
%Constraints
Constraints=[];
for t = 1:24
Constraints = [Constraints, dE(t) <= dEmax_ws, E_ws(t) <= Emax_ws, E_ws(t) == E_ws(t-1)+dE(t)];
end
when t=1 then E_ws(t-1) is not a valid index

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 1월 16일

댓글:

2023년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by