%2D Heat Transfer using Matlab
%https://www.youtube.com/watch?v=x47QZxVF1gY
n = 4;
w = 3;
h = 3;
x = linespace(0,w,n);
y = linespace(0,h,n);
% create an array from 1 to 10 with 5 values
%values = linspace(1, 10, 5)
%ans = 1.0000 3.2500 5.5000 7.7500 10.0000
T = zeros(n);% Temprature
T(1,1:n) = 100; %Top
T(n,1:n) = 300; %Bottom
T(1:n,1) = 500; % Left
T(1:n,n) = 200; %Right
tolerance = 1e-6;
error = 1;
k = 0;%?Iteration times
while error > tolerance
k = k+1;
Told = T;
for i = 2:n-1
for j = 2:n-1
T(i,j) = .25 * (T(i,j-1) + T(i,j+1) + T(i+1,j) + T(i-1,j));
end
end
error = max(max(abs(Told - T)));
end
subplot(3,1,1),contour(x,y,T),colormap
title('Temprature (steady State)'),xlabel('x'),ylabel('y'),colorbar
subplot(3,1,3),pcolor(x,y,T),shading interp
title('Temprature (steady State)'),xlabel('x'),ylabel('y'),colorbar
subplot(3,1,3)
surf(T')
xlabel('x')
ylabel('y')
zlabel('T')
,colorbar
然后把n改成40可以得到更加平滑的结果