How to fix Attempt to grow array along ambiguous dimension. erro?

2 views (last 30 days)
I want to solve the attached equatio(heat equation) with gauss seidel method but I faced an error.How can I fix it?
L=0.15; %m
dt=1800; %s time step
h=0.03; %m
time=3600; %s
Nx=ceil(L/h);
Nt=ceil(time/dt);
Nodex=Nx+1;
x=linspace(0,L,Nodex);
t=linspace(0,time,Nt);
%%Thermal
T2=20;
T1=32;
rho=880; %kg/m^3
landa=0.2; %W/m.K
T=zeros(Nodex,Nt);
H=zeros(Nodex,Nt);
deltaT=2;
T_c=27;
LH=179000; %J/kg
Cps=2000; %J/kg.K
Cpl=2000; %J/kg.K
Cp=(LH/(2*deltaT))+(Cpl+Cps/2);
%initial condition
T(:,1)=20;
%boundary condition
T(1,:)=T1;
T(end,:)=T2;
gs_iter=0;
for q = 1:inf
F = T;
for j=1:Nt-1
for i=2:Nodex-1
if T(i,j)<(T_c-deltaT)
H(T(i,j))=Cps*(T_c-deltaT);
elseif (T(i,j)<=(T_c+deltaT)) && (T(i,j)>=(T_c-deltaT))
H(T(i,j))=Cps*(T_c-deltaT)+Cp*((T_c+deltaT)-(T_c-deltaT));
elseif T(i,j)>(T_c+deltaT)
H(T(i,j))=Cps*(T_c-deltaT)+Cp*((T_c+deltaT)-(T_c-deltaT))+Cpl*(T(i,j)-T_c+deltaT);
end
H(T(1,j+1))=H(T(1,j+1))+((landa*dt/(rho*(h^2)))*(T(2,j+1)-2*T(1,j+1)+T(2,j+1)));
H(T(i,j+1))=H(T(i,j+1))+((landa*dt/(rho*(h^2)))*(T(i+1,j+1)-2*T(i,j+1)+T(i-1,j+1)));
end
error=max(abs(T-F));
if error < 1e-1
break
end
gs_iter=gs_iter+1;
end
end
Warning: Too many FOR loop iterations. Stopping after 9223372036854775806 iterations.
Attempt to grow array along ambiguous dimension.

Answers (1)

Walter Roberson
Walter Roberson on 4 Mar 2022
H=zeros(Nodex,Nt);
That is a 2D array.
H(T(i,j))=Cps*(T_c-deltaT);
That and similar lines of code index the 2D array H with a single dimension. In MATLAB, that is permitted; it is called "linear indexing".
2D arrays in MATLAB are stored in memory as all of the first column, then all of the second column, then all of the third column, and so on, all as one continuous piece of memory, but the variable has header information about how many rows and columns are present. Instead of requiring that you index the 2D array with a pair of subscripts, MATLAB permits you to index with a single subscript that is the relative offset from the beginning -- so H(1) is the first element of H, same as H(1,1), H(2) is same as H(2,1), H(Nodex) is the same as H(Nodex,1), end of the first column. Then the count wraps into the next column: H(Nodex+1) is the same as H(1,2), H(Nodex+2) is the same as H(2,2) and so on. H(Row,Column) is the same as H((Column-1)*NumberOfRows+Row) . And you are permitted to store into those locations.
At some point you might assign into H(Nodex*Nt), the last element stored in the continuous memory.
Now what happens if you try to assign into H(Nodex*Nt+1) ? It needs to make H larger to hold the extra memory. But should it expand the number of rows, or should it expand the number of columns?
The error message you got indicates that your H(T(i,j)) is beyond H(Nodex*Nt) that is the last space reserved, and that MATLAB does not know whether you want to expand the number of rows or the number of columns.
  1 Comment
Walter Roberson
Walter Roberson on 4 Mar 2022
Note that your picture of your equation never uses T as an index. It talks about H(i,j) being defined in terms of adding a quantity computed from the T array to H(i,j-1) but it never says anything about H(T(i,j)) which would mean using T(i,j) as an index into H.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!