【问题标题】:2D-Heat propagation in a matrix in MatlabMatlab中矩阵中的二维热传播
【发布时间】:2016-12-19 02:12:37
【问题描述】:

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

我计算出了二维热传播的计算方法,但模拟仍然无法正常工作。有什么问题吗?

【问题讨论】:

    标签: matlab matrix propagation


    【解决方案1】:

    部分:

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

    必须在for k = 1:cycle 循环中。其余的应该没问题。但目前热点并没有保持不变。您必须对边缘执行相同的操作。

    问候, 迈克尔

    【讨论】:

    • 感谢您的回答。我想通了!
    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2015-05-23
      相关资源
      最近更新 更多