I keep getting undefined function when preallocating

3 visualizzazioni (ultimi 30 giorni)
Hi,
I am trying to create a for loop wherein the temperature that is outputted is then subbed back in, I have preallocated but i get the error undefined function '(index)' when it comes to the line: S1QAmb(index)=zeros(size(index)) %surface 1, ambient load. Since the size of the array will change with the size of the index, I did not think the (index) needed to be defined. Also, if any other mistakes are spotted, please do let me know. Thank you.
My script is below:
U=1
S1=1.7*1.2 %Surface area of surface 1
S2= 1.7*1.2 %Surface 2 area
S3= 1.7*1.2 %surface 3 area
S4= 1.7*1.2 %surface 4 area
S5= 1.7*1.2 %surface 5 area
S6= 1.7*1.2 %surface 6 area
S7= 1.7*1.2 %surface 7 area
S8= 1.7*1.2 %surface 8 area
S9= 1.7*1.2 %surface 9 area
S10= 1.7*1.2 %surface 10 area
Tout=5
ma=1
ca=2
DTM=1
deltat=1
%Prealloacate
S1QAmb(index)=zeros(size(index)) %surface 1, ambient load
S2QAmb(index)=zeros(size(index)) %surface 2, ambient load
S3QAmb(index)=zeros(size(index)) %surface 3, ambient load
S4QAmb(index)=zeros(size(index)) %surface 4, ambient load
S5QAmb(index)=zeros(size(index)) %surface 5, ambient load
S6QAmb(index)=zeros(size(index)) %surface 6, ambient load
S7QAmb(index)=zeros(size(index)) %surface 7, ambient load
S8QAmb(index)=zeros(size(index)) %surface 8, ambient load
S9QAmb(index)=zeros(size(index)) %surface 9, ambient load
S10QAmb(index)=zeros(size(index)) %surface 10, ambient load
QAmb_Total(index)=zeros(size(index)) %Total Ambient Load
deltaTin(index)=zeros(size(index))
Tin(index)=zeros(size(index))
for index=(1:30)
if (index)==1
Tin(index)=20
else
Tin(index)=Tin(index-1)+deltaTin(index)
end
%Ambient Load
S1QAmb(index)=S1*U*(Tout-+Tin(index)) %surface 1, ambient load
S2QAmb(index)=S2*U*(Tout-+Tin(index)) %surface 2, ambient load
S3QAmb(index)=S3*U*(Tout-+Tin(index)) %surface 3, ambient load
S4QAmb(index)=S4*U*(Tout-+Tin(index)) %surface 4, ambient load
S5QAmb(index)=S5*U*(Tout-+Tin(index)) %surface 5, ambient load
S6QAmb(index)=S6*U*(Tout-+Tin(index)) %surface 6, ambient load
S7QAmb(index)=S7*U*(Tout-+Tin(index)) %surface 7, ambient load
S8QAmb(index)=S8*U*(Tout-+Tin(index)) %surface 8, ambient load
S9QAmb(index)=S9*U*(Tout-+Tin(index)) %surface 9, ambient load
S10QAmb(index)=S10*U*(Tout-+Tin(index)) %surface 10, ambient load
%Total Ambient Load in Array Form
QAmb_Total(index)=S1QAmb(index)+S2QAmb(index)+S3QAmb(index)+S4QAmb(index)+S5QAmb(index)+S6Amb(index)+S7QAmb(index)+S8QAmb(index)+S9QAmb(index)+S10QAmb(index)
%changein air cabin temperature where deltat is timestep
deltaTin(index)=(QAmb_Total(index)/((ma*ca)+DTM))*(deltat)
%New car cabin temperature
Tin(index)=Tin(index-1)+deltaTin(index)
end

Risposta accettata

Thorsten
Thorsten il 20 Feb 2019
Modificato: Thorsten il 20 Feb 2019
index = 1:30;
% Preallocate
S1QAmb = zeros(size(index)); % index has to be defined before it can be used
for i = index
S1QAmb(i) = S1*U*(Tout+Tin(i)); % you wrote -+; that's invalid syntax
% and so on
end
You should also consider to use an array for S
S = 1.7*1.2*ones(1, 10);
and compute all S values in one line in the for loop
SQAmb(i,:) = S*U*(Tout+Tin(i));
with SQAmb preallocated as a matrix
SQAmb = zeros(numel(index), numel(S));
  5 Commenti
Jamil Dudhwala
Jamil Dudhwala il 20 Feb 2019
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 woould have sold that issue, but it does not seem to be working.
Thank you,
My revised 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
Thorsten
Thorsten il 21 Feb 2019
Modificato: Thorsten il 21 Feb 2019
The if clause is fine. The problem occurs in the last but one line. You need to handle the case i == 1 also for this line.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by