📄 heat2d.m
字号:
function U = Heat2D(dt, dx, dy, Tmax, Tsnap, value, bounds)
% make sure steps are consistent
Nx = round(1/dx);
dx = 1/Nx;
Ny = round(1/dy);
dy = 1/Ny;
Nt = round(Tmax/dt);
dt = Tmax/Nt;
rhox = dt/dx^2;
rhoy = dt/dy^2;
if rhox + rhoy > 0.5
fprintf(1,'Warning: bad selection of steps\n');
end
C1 = 1-2*rhox-2*rhoy;
Layers = zeros(2, 1+Nx, 1+Ny);
tpast = 1;
tnow = 2;
iTsnap = Tsnap/dt;
[X, Y] = meshgrid(0:dx:1, 0:dy:1);
% set up initial conditions and plot
Layers(tpast, (1+round(bounds(1)/dx)):(1+round(bounds(2)/dx)), ...
(1+round(bounds(3)/dy)):(1+round(bounds(4)/dy))) = value;
U = shiftdim(Layers(tpast,:,:));
figure;
surf(X,Y,U);
title('t=0','Fontsize',12);
% Carry out iterations
for t=1:Nt
for i=2:Nx
for j=2:Ny
Layers(tnow,i,j) = C1*Layers(tpast,i,j) + ...
rhox*(Layers(tpast,i+1,j) + Layers(tpast,i-1,j)) + ...
rhoy*(Layers(tpast,i,j+1) + Layers(tpast,i,j-1));
end
end
% Plot if required
if find(iTsnap == t)
U = shiftdim(Layers(tnow,:,:));
figure;
surf(X,Y,U);
title(['t=', num2str(Tsnap(1)) ],'Fontsize',12);
Tsnap(1) = [];
end
% Swap layers
tnow = 1+mod(t+1,2);
tpast = 1+mod(t,2);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -