MATLAB: Index in position 1 exceeds array bounds (must not exceed 5)

errorindex-error

I was writing a simple code to find temperature of air and wall at different sections in a tower. But I am getting an error.
ma=0.641478; h=14.95;Cp=1007;Cc=840;a=36;t=0.01; Tao=311;Two=298; Q_in=0;
theta=[150, 150, 300, 300, 300, 600]; s=[0.5,1.5,2.5,3.5,4.5,5.5];
Ta(1,1)=Tao;
Tw(1,1)=Two;
for i=2:6;
Ta(i,1)=(Ta(i-1,1)*(ma*Cp-h*18)+(Tw(i-1,1)*h*a))/(ma*Cp+h*18);
Tw(i,1)=Two;
Q(i-1,1)=645.968*(Ta(i-1,1)-Ta(i,1));
end
for j=2:1:6;
Ta(1,j)=Tao;
Tw(1,j)=Tw(1,j-1)+Q(1,j-1)*(4.973*10^-6)*theta(j);
for i=2:6;
Ta(i,j)=(Ta(i-1,j)*(ma*Cp-h*18)+(Tw(i-1,j)*h*a))/(ma*Cp+h*18);
Q(i-1,j)=645.968*(Ta(i-1,j)-Ta(i,j));
Tw(i,j)=Tw(i,j-1)+(Q(i,j-1)*(4.973*10^-6)*theta(j));
end
end
Index in position 1 exceeds array bounds (must not exceed 5).
Error in tower (line 32)
Tw(i,j)=Tw(i,j-1)+(Q(i,j-1)*(4.973*10^-6)*theta(j))

Best Answer

In the stated expression, you try to use Q(6,1) which is undefined. (In the top loop, Q(i,1) is only initialized up to Q(5,1)).
Best wishes
Torsten.