2016-12-19 40 views
0

我想計算和可視化24 x 24矩陣中的熱傳播。矩陣中有兩個熱點,其溫度在一段時間內保持不變。矩陣的外邊緣在整個時間內也保持不變。我是新來的MATLAB和到現在爲止我已經寫了下面的腳本: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; 

我想通了熱傳播的計算在兩個方面,但仍仿真是行不通的。你有沒有看到任何問題?

回答

1

部分:

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

必須在for k = 1:cycle循環。其餘的應該沒問題。但目前熱點並不穩定。你必須做同樣的邊緣。

問候, 邁克爾

+0

感謝您的回答。我想到了! – Kris

1

至於您的代碼,看起來像您的忘記設置

PlateOld = PlateNew; 

在41'th線(在末尾 「對於k = 1:循環」 循環) 。

似乎你的代碼在第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