Can someone help me with this Matlab code on 2D heat equation
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Heat Transfer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!