Index in position 3 exceeds array bounds (must not exceed 80000).
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi.
I am trying to modify my Crank Nicholson method code so that it breaks when 14.9<T(11,11,k+1)<15.1 condition is satisfied.
This is the equation
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1014835/image.jpeg)
Since time interval is unknown,time variable t, time step variable p and variables including p and t are commented out. I arbitrarily picked p values to obtain the solution but that is not how the code should work. Also whenever I run the code, i got the following error message
Index in position 3 exceeds array bounds (must not exceed 80000).
Error in Crank (line 260)
T(i,j,k+1)=1/(1+2*r)*(r/2*(T(i-1,j,k+1)+ T(i+1,j,k+1)+ T(i,j+1,k+1)+ T(i,j-1,k+1))+(1-2*r)*T(i,j,k)+r/2*(T(i-1,j,k)+
T(i+1,j,k)+ T(i,j+1,k)+ T(i,j-1,k)));
How must the code be modified so that it is in an infinite loop and breaks when 14.9<T(11,11,k+1)<15.1 condition is satisfied?
Thanks.
%t=4; % Total time (s) % Commented out
delta_t=0.05; % Time step
L=2; % Length of each edge
delta_x=0.1; % Spacing
% Boundary temperatures
T1=10;
T2=10;
T3=10;
T4=10;
% Initial temperature
T5=400;
n=((L/delta_x)+1)^2; % Total no of nodes
m=sqrt(n); % Number of nodes in each row and column
%r1=(t/delta_t)+1; % Number of time steps, commented out
%p=round(r1); % Number of time steps (rounded)
r=0.033;
% Placing initial and boundary conditions
%T=zeros(m,m,p); % Pre-allocating
for k=1:80000 % Time-step loop,this line was k=1:p normally
for i=1:m % x coord loop
for j=1:m % y coord loop
if(i==1)&&(j==1)
T(i,j,k)=(T4+T1)/2;
elseif(i==1)&&(j==m)
T(i,j,k)=(T2+T1)/2;
elseif(i==m)&&(j==m)
T(i,j,k)=(T2+T3)/2;
elseif(i==m)&&(j==1)
T(i,j,k)=(T4+T3)/2;
elseif(i==1&&(j>1&&j<m))
T(i,j,k)=T1;
elseif(j==m&&(i>1&&i<m))
T(i,j,k)=T2;
elseif(i==m&&(j>1&&j<m))
T(i,j,k)=T3;
elseif(j==1&&(i>1&&i<m))
T(i,j,k)=T4;
else
T(i,j,k)=T5;
end
end
end
end
% Solution
keepgoing = true;
for k=1:80000 % This line was k=1:p normally too
for i=2:m-1
for j=2:m-1
%%%%%%% line 260 below %%%%%%%%%%
T(i,j,k+1)=1/(1+2*r)*(r/2*(T(i-1,j,k+1)+ T(i+1,j,k+1)+ T(i,j+1,k+1)+ T(i,j-1,k+1))+(1-2*r)*T(i,j,k)+r/2*(T(i-1,j,k)+ T(i+1,j,k)+ T(i,j+1,k)+ T(i,j-1,k)));
if (i==11 && j==11 && T(11,11,k+1)>14.9 && T(11,11,k+1)<15.1)
keepgoing =false;
break;
end
end
if ~keepgoing; break; end
end
if ~keepgoing; break; end
end
imagesc(T(:,:,80000));
colorbar;
title('Temperature Profile')
댓글 수: 0
채택된 답변
Torsten
2022년 5월 29일
keepgoing = true;
for k=1:79999 % This line was k=1:p normally too
instead of
keepgoing = true;
for k=1:80000 % This line was k=1:p normally too
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!