I keep getting the error "subscript must either be real positive integers or logicals" even when I place an 'if' statement

It seems I keep getting the error "subscript must either be real positive integers or logicals".
This must be because I have started of from i = 1, hence i - 1 = 0 and thats where the problem is. I thought that my ' if ' loop would have solved that issue, but it does not seem to be working.
Thank you,
My script is below:
U=1
S = 1.7*1.2*ones(1, 10);
Tout = 5
ma = 1
ca = 2
DTM = 1
deltat = 1
index = 1:30;
%Prealloacate
SQAmb = zeros(numel(index), numel(S));
QAmb_Total = zeros(size(index));
deltaTin = zeros(size(index));
Tin = zeros(size(index));
%for loop
for i = index
if (i)==1
Tin(i) = 20
else
Tin(i) = Tin(i-1)+deltaTin(i)
end
SQAmb(i,:) = S*U*(Tout-Tin(i));
%Total Ambient Load in Array Form
QAmb_Total(i) = sum(SQAmb(i,:))
%change in air cabin temperature where deltat is timestep
deltaTin(i) = (QAmb_Total(i)/((ma*ca)+DTM))*(deltat)
%New car cabin temperature
Tin(i) = Tin(i-1)+deltaTin(i)
end

 Réponse acceptée

I thought that my ' if ' loop would have solved that issue, but it does not seem to be working.
It is working in the if block, but not afterwards.
The line that throws the error is:
%New car cabin temperature
Tin(i) = Tin(i-1)+deltaTin(i)

6 commentaires

I can't work out what the error is in that line, what is wrong there?
The same as was the problem in your original line. When ‘i’ is 1, the subscript is 0. You likely need another if block around it, or otherwise define:
Tin(1) = 20;
before the loop, and then start your loop at 2 instead of 1:
for i = 2:max(index)
...
end
That is likely the easiest way.
For example:
%for loop
Tin(1) = 20
for i = 2:max(index)
Tin(i) = Tin(i-1)+deltaTin(i)
SQAmb(i,:) = S*U*(Tout-Tin(i));
%Total Ambient Load in Array Form
QAmb_Total(i) = sum(SQAmb(i,:))
%change in air cabin temperature where deltat is timestep
deltaTin(i) = (QAmb_Total(i)/((ma*ca)+DTM))*(deltat)
%New car cabin temperature
Tin(i) = Tin(i-1)+deltaTin(i)
end
If you do this, you will need to account for the initial values being 0 in ‘SQAmb’, ‘QAmb_Total’, and ‘deltaTin’.
Thank you, your solution worked athough I found another way which lets me start from i = 1.
As always, my pleasure.
Out of curiosity, what is your solution?
I moved the equations for when i=1 before the else statement and the then when i>1 after the else statement. It was longer but seemed to work.
Revamped script is below:
U=1
S = 1.7*1.2*ones(1, 10);
Tout=5
ma=1
ca=2
DTM=1
deltat=1
index=1:30;
%Prealloacate
SQAmb = zeros(numel(index), numel(S)); %Preallocation of Ambient Load matrix
QAmb_Total = zeros(size(index)); %Preallocation of Total Ambient Load matrix
deltaTin = zeros(size(index));
Tin = zeros(size(index));
for i=index
if (i)==1
SQAmb(i,:) = S*U*(Tout-20)
%Total Ambient Load in Array Form
QAmb_Total(i)=sum(SQAmb(i,:))
%change in air cabin temperature where deltat is timestep
deltaTin(i)=(QAmb_Total(i)/((ma*ca)+DTM))*(deltat)
%New car cabin temperature
Tin(i)=20+deltaTin(i)
else
SQAmb(i,:) = S*U*(Tout-Tin(i-1))
%Total Ambient Load in Array Form
QAmb_Total(i)=sum(SQAmb(i,:))
%change in air cabin temperature where deltat is timestep
deltaTin(i)=(QAmb_Total(i)/((ma*ca)+DTM))*(deltat)
%New car cabin temperature
Tin(i)=Tin(i-1)+deltaTin(i)
end
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by