2D-Heat propagation in a matrix in Matlab

168 views Asked by At

I want to calculate and visualize a heat propagation in a 24 x 24 matrix. There are two hot-spots in the matrix whose temperature stays constant over the time. The outer edges of the matrix also stay constant over the time. I am new to MATLAB and until now I have written following script:

%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;

I figured out the calculation for the heat propagation in two dimensions but the simulation still would not work. Do you see any issues?

2

There are 2 answers

1
M_Tornack On BEST ANSWER

the part:

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

has to be in the for k = 1:cycle loop. The rest should be fine. But at the moment the hot spots are not staying constant. You have to do the same as for the edges.

Regards, Michael

0
Andrei Davydov On

As for your code, looks like your forget set

PlateOld = PlateNew;

on the 41'th line (in the end of "for k = 1:cycles" loop).

Also seems your code produces wrong outer edges constant temperature set on the k 1'st iteration. On the first iteration your PlateNew matrix is empty, so setting

PlateNew(:,1) = 20;
PlateNew(1,:) = 20;

produce filling just one (first) element of the matrix. Your code looks like C-style. Instead MATLAB more efficiently can operate with matrices. So your for loop more efficiently can be implemented as (replaces lines 21-41 in your code)

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