2D-Matlab中矩阵中的热传播

时间:2016-12-19 02:12:37

标签: matlab matrix propagation

我想计算并可视化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;

我想出了二维热传播的计算,但模拟仍然不起作用。你看到有什么问题吗?

2 个答案:

答案 0 :(得分:1)

部分:

pause(0.2);
contourf(PlateNew)
colorbar
title('Heat propagation in a plate');
PlateOld = PlateNew;

必须在for k = 1:cycle循环中。其余的应该没问题。但此刻热点并未保持不变。你必须像边缘一样做。

此致 迈克尔

答案 1 :(得分:1)

至于你的代码,看起来像你的忘记集

PlateOld = PlateNew;

在41&#39行(在&#34的末尾;对于k = 1:周期"循环)。

您的代码似乎也会在k 1' s迭代中产生错误的外边缘恒温设置。在第一次迭代中,您的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