# Getting same final value for various time-steps

2 views (last 30 days)
Turgut Ataseven on 31 May 2022
Commented: Turgut Ataseven on 31 May 2022
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 CommentsShow 1 older commentHide 1 older comment
Jan on 31 May 2022
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
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?
Turgut Ataseven on 31 May 2022
@Jan Thanks for the reply.
I get the same output "final" as I vary ending value of k (number of time steps).
keepgoing = true;
for k=1:7999 % this value %%
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
end
T matrix converges to same values after approximately 260 steps. It is probably because both sides of the equation has "k+1".

Torsten on 31 May 2022
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

### Categories

Find more on MATLAB in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!