Can someone help me with this Matlab code on 2D heat equation

10 ビュー (過去 30 日間)
Eshna Sasha
Eshna Sasha 2021 年 4 月 2 日
コメント済み: Eshna Sasha 2021 年 4 月 2 日
I want to plot the temperature contour for the equation ∂T/∂t = ∂2T/∂x2 + ∂2T/∂y2,
The initial condition is taken to be t=0
It throws an error: Index in position 1 exceeds array bounds (must not exceed 1).
Here's my code:
>> nx=10;
>> ny=nx;
>> nt=100;
>> x=linspace(0,1,nx);
>> y=linspace(0,1,ny);
>> dx=x(2)-x(1);
>> dy=dx;
>> error=9e9;
>> tolerance=1e-4;
>> dt=1e-3;
>> T_L=0;
>> T_T=1;
>> T_R=0;
>> T_B=0;
>> T=300*ones(nx,ny);
>> T(2:ny-1,1)= T_L;
>> T(2:ny-1,nx)=T_R;
>> T(1,2:nx-1)=T_T;
>> T(ny,2:nx-1)=T_B;
>> T_old= T;
>> T_initial=0;
>> k1=1.1*(dt/(dx^2));
>> k2=1.1*(dt/(dy^2));
>> Jacobi_iteration=1;
>> for k= 1:nt
error=9e9;
while(error>tolerance)
for i=2:nx-1
for j= 2:ny-1
Term_1= 1/(1+(2*k1)+(2*k2));
Term_2= k1*Term_1;
Term_3= k2*Term_1;
H= (T_old(i-1,j))+(T_old(i+1,j));
V=(T_old(i,j+1))+(T_old(i,j-1));
T(i,j)= (T_initial(i,j)*Term_1)+ (H*Term_2)+(V*Term_3);
end
end
error=max(max(abs(T_old-T)));
T_old=T;
Jacobi_iteration= Jacobi_iteration+1;
end
T_intial=0;
%Plotting
figure(1)
contourf(x,y,T)
clabel(contourf(x,y,T))
colobar
colormap(jet)
set(gca,'ydir','reverse')
xlabel('X-Axis')
y-label('Y-Axis')
title(sprintf('No. of Unsteady Jacobi Iterations(Implicit)=%d', Jacobi_iteration));
pause(0.03)
end

採用された回答

Ravi Kumar
Ravi Kumar 2021 年 4 月 2 日
Your T_initial is declared as a scalar, you are getting the error when you try to index into it beyond its size. Make that zeros of same size as T. You will find the corrected code below. On a related note, if you want to solve more general problems, check PDE Toolbox Heat Transfer workflow.
Regards,
Ravi
nx=10;
ny=nx;
nt=100;
x=linspace(0,1,nx);
y=linspace(0,1,ny);
dx=x(2)-x(1);
dy=dx;
error=9e9;
tolerance=1e-4;
dt=1e-3;
T_L=0;
T_T=1;
T_R=0;
T_B=0;
T=300*ones(nx,ny);
T(2:ny-1,1)= T_L;
T(2:ny-1,nx)=T_R;
T(1,2:nx-1)=T_T;
T(ny,2:nx-1)=T_B;
T_old= T;
T_initial=zeros(size(T));
k1=1.1*(dt/(dx^2));
k2=1.1*(dt/(dy^2));
Jacobi_iteration=1;
for k= 1:nt
error=9e9;
while(error>tolerance)
for i=2:nx-1
for j= 2:ny-1
Term_1= 1/(1+(2*k1)+(2*k2));
Term_2= k1*Term_1;
Term_3= k2*Term_1;
H= (T_old(i-1,j))+(T_old(i+1,j));
V=(T_old(i,j+1))+(T_old(i,j-1));
T(i,j)= (T_initial(i,j)*Term_1)+ (H*Term_2)+(V*Term_3);
end
end
error=max(max(abs(T_old-T)));
T_old=T;
Jacobi_iteration= Jacobi_iteration+1;
end
T_intial=0;
%Plotting
figure(1)
contourf(x,y,T)
clabel(contourf(x,y,T))
colorbar
colormap(jet)
set(gca,'ydir','reverse')
xlabel('X-Axis')
ylabel('Y-Axis')
title(sprintf('No. of Unsteady Jacobi Iterations(Implicit)=%d', Jacobi_iteration));
pause(0.03)
end
  1 件のコメント
Eshna Sasha
Eshna Sasha 2021 年 4 月 2 日
A big thank you for the code. But after conducting 102 Jacobi iterations, I get this. Something is wrong for my case. Is it because of this? T=300*ones(nx,ny);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEigenvalue Problems についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by