0

Sorry to bother you guys, but 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 sold 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
  • 1
    The error is in the last line before the `end`. Instead of checking for `i == 1`, I would set `Tin(1) = 20` before the loop and then just loop from `index = 2:30`. – beaker Feb 20 '19 at 17:57
  • @beaker: Correct. I missed the last line. Reopened if you want to answer. – gnovice Feb 20 '19 at 18:06
  • 1
    @gnovice I'm kind of confused by the program logic. `Tin` is computed twice with different values of `deltaTin`. Not sure I can suggest an actual solution. – beaker Feb 20 '19 at 18:11
  • Your `if` loop will not cancel what it's after.@beaker has a nice idea. – alpereira7 Feb 22 '19 at 13:32

0 Answers0