Getting same final value for various time-steps
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
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 time when 14.9<T(11,11,k+1)<15.1 but whatever the p interval is, I always face with the same result ("final" matrix). What should be done to make the code work properly?
%t=4; % Total time (s)
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
%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:8000 % Time-step loop
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:7999
for i=2:m-1
for j=2:m-1
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(i,j,k+1)>14.9 && T(i,j,k+1)<15.1)
keepgoing =false;
break;
end
end
if ~keepgoing; break; end
end
if ~keepgoing; break; end
end
final=T(:,:,k);
imagesc(T(:,:,k));
colorbar;
title('Temperature Profile')
Thanks.
3 个评论
Jan
2022-5-31
Easier to debug:
T=zeros(m,m, 8000);
% for k=1:8000 The body does not depend on k, so omit this loop!
for i=1:m % x coord loop
for j=1:m % y coord loop
if i==1
if j==1
T(i,j,:) = (T4+T1)/2;
elseif j==m
T(i,j,:) = (T2+T1)/2;
else
T(i,j,:) = T1;
end
elseif i==m
if j==1
T(i,j,:) = (T4+T3)/2;
elseif j==m
T(i,j,:) = (T2+T3)/2;
else
T(i,j,:) = T3;
end
else
if j==1
T(i,j,:) = T4;
elseif j==m
T(i,j,:) = T2;
else
T(i,j,:) = T5;
end
end
end
end
Or what about:
T = zeros(m, m, 8000);
T(1, 1, :) = (T4+T1) / 2;
T(1, m, :) = (T2+T1) / 2;
T(1, 2:m-1, :) = T1;
T(m, 1, :) = (T4+T3) / 2;
T(m, m, :) = (T2+T3) / 2;
T(m, 2:m-1, :) = T3;
T(2:m-1, 1, :) = T4;
T(2:m-1, m, :) = T2;
T(2:m-1, 2:m-1, :) = T5;
Now, what is your problem? You get the same output final if you vary what?
回答(1 个)
Torsten
2022-5-31
for k=1:7999
for i=2:m-1
for j=2:m-1
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)));
end
end
if T(11,11,k+1)>14.9 && T(11,11,k+1)<15.1
break
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!