Matlab 矩阵中的二维热传播
2D-Heat propagation in a matrix in Matlab
我想计算和可视化 24 x 24 矩阵中的热传播。矩阵中有两个热点,其温度随时间保持恒定。矩阵的外边缘也随时间保持不变。我是 MATLAB 的新手,到目前为止我已经编写了以下脚本:
%declaration and initialization of matrix with
%24 x 24 cells, start temp. = 20°C
PlateOrg (1:24, 1:24) = 20;
% size of matrix
[height, width] = size(PlateOrg);
cells = height * width;
% percent that is given from one cell to its neighbouring cells
prop = 0.2;
%These are the hot-spots, they heat up the neighbouring cells
%but do not heat up themselves any higher -> their
%temperature is constant over the simulation
PlateOrg(4,4:20) = 100;
PlateOrg(5:9,20) = 80;
PlateOrg(11:19,4) = 50;
PlateOrg(20,4:20) = 60;
%First Contourf 2D-Diagram with start temperatures
contourf(PlateOrg);
PlateOld = [];
PlateOld = PlateOrg;
PlateNew = [];
% the amount of cycles
cycles = 50;
% Calculation of the heat propagation
for k = 1:cycles
%the outer edges stay constant at temp. = 20°C
%for the whole simulation
PlateNew(:,1) = 20;
PlateNew(1,:) = 20;
PlateNew(24,:) = 20;
PlateNew(:,24) = 20;
%every cell gets 20 percent heat of four neighbouring cells
for i=2:height-1
for j=2:width-1
PlateNew(i,j)=...
(1-4*prop)*PlateOld(i,j)+prop*...
(PlateOld(i,j-1)+PlateOld(i,j+1)+...
PlateOld(i-1,j)+PlateOld(i+1,j));
end
end
end
pause(0.2);
contourf(PlateNew)
colorbar
title('Heat propagation in a plate');
PlateOld = PlateNew;
我想通了二维热传播的计算,但模拟仍然无法正常工作。你看到任何问题了吗?
部分:
pause(0.2);
contourf(PlateNew)
colorbar
title('Heat propagation in a plate');
PlateOld = PlateNew;
必须在 for k = 1:cycle
循环中。其余的应该没问题。但目前热点并没有保持不变。您必须对边缘执行相同的操作。
此致,
迈克尔
至于你的代码,看起来像你的遗忘集
PlateOld = PlateNew;
在第 41 行(在 "for k = 1:cycles" 循环的末尾)。
而且您的代码似乎在第 k 次迭代中产生了错误的外边缘恒温设置。在第一次迭代中,您的 PlateNew 矩阵为空,因此设置
PlateNew(:,1) = 20;
PlateNew(1,:) = 20;
生成仅填充矩阵的一个(第一个)元素。
您的代码看起来像 C 风格。相反,MATLAB 可以更有效地处理矩阵。因此,您的 for 循环可以更有效地实现为(替换代码中的第 21-41 行)
PlateNew = PlateOld;
% the amount of cycles
cycles = 50;
% Calculation of the heat propagation
for k = 1:cycles
%every cell gets 20 percent heat of four neighbouring cells
PlateNew = filter2(prop * [0 1 0; 1 1/prop-4 1; 0 1 0], PlateNew);
%the outer edges stay constant at temp. = 20°C
%for the whole simulation
PlateNew(:,1) = 20;
PlateNew(1,:) = 20;
PlateNew(24,:) = 20;
PlateNew(:,24) = 20;
end
我想计算和可视化 24 x 24 矩阵中的热传播。矩阵中有两个热点,其温度随时间保持恒定。矩阵的外边缘也随时间保持不变。我是 MATLAB 的新手,到目前为止我已经编写了以下脚本:
%declaration and initialization of matrix with
%24 x 24 cells, start temp. = 20°C
PlateOrg (1:24, 1:24) = 20;
% size of matrix
[height, width] = size(PlateOrg);
cells = height * width;
% percent that is given from one cell to its neighbouring cells
prop = 0.2;
%These are the hot-spots, they heat up the neighbouring cells
%but do not heat up themselves any higher -> their
%temperature is constant over the simulation
PlateOrg(4,4:20) = 100;
PlateOrg(5:9,20) = 80;
PlateOrg(11:19,4) = 50;
PlateOrg(20,4:20) = 60;
%First Contourf 2D-Diagram with start temperatures
contourf(PlateOrg);
PlateOld = [];
PlateOld = PlateOrg;
PlateNew = [];
% the amount of cycles
cycles = 50;
% Calculation of the heat propagation
for k = 1:cycles
%the outer edges stay constant at temp. = 20°C
%for the whole simulation
PlateNew(:,1) = 20;
PlateNew(1,:) = 20;
PlateNew(24,:) = 20;
PlateNew(:,24) = 20;
%every cell gets 20 percent heat of four neighbouring cells
for i=2:height-1
for j=2:width-1
PlateNew(i,j)=...
(1-4*prop)*PlateOld(i,j)+prop*...
(PlateOld(i,j-1)+PlateOld(i,j+1)+...
PlateOld(i-1,j)+PlateOld(i+1,j));
end
end
end
pause(0.2);
contourf(PlateNew)
colorbar
title('Heat propagation in a plate');
PlateOld = PlateNew;
我想通了二维热传播的计算,但模拟仍然无法正常工作。你看到任何问题了吗?
部分:
pause(0.2);
contourf(PlateNew)
colorbar
title('Heat propagation in a plate');
PlateOld = PlateNew;
必须在 for k = 1:cycle
循环中。其余的应该没问题。但目前热点并没有保持不变。您必须对边缘执行相同的操作。
此致, 迈克尔
至于你的代码,看起来像你的遗忘集
PlateOld = PlateNew;
在第 41 行(在 "for k = 1:cycles" 循环的末尾)。
而且您的代码似乎在第 k 次迭代中产生了错误的外边缘恒温设置。在第一次迭代中,您的 PlateNew 矩阵为空,因此设置
PlateNew(:,1) = 20;
PlateNew(1,:) = 20;
生成仅填充矩阵的一个(第一个)元素。 您的代码看起来像 C 风格。相反,MATLAB 可以更有效地处理矩阵。因此,您的 for 循环可以更有效地实现为(替换代码中的第 21-41 行)
PlateNew = PlateOld;
% the amount of cycles
cycles = 50;
% Calculation of the heat propagation
for k = 1:cycles
%every cell gets 20 percent heat of four neighbouring cells
PlateNew = filter2(prop * [0 1 0; 1 1/prop-4 1; 0 1 0], PlateNew);
%the outer edges stay constant at temp. = 20°C
%for the whole simulation
PlateNew(:,1) = 20;
PlateNew(1,:) = 20;
PlateNew(24,:) = 20;
PlateNew(:,24) = 20;
end