2d pde, implicit euler in time, central differencing in space. time loop with jacobi solver, discretized unknown ae aw an as ap, to plot temperature on a plate a 0, 1, 2, 5, 10, 100 seconds in matlab

Here's a possible implementation:

main.m
% Plate parameters
Lx = 1;  % length in x-direction
Ly = 1;  % length in y-direction
nx = 81; % number of grid points in x-direction
ny = 81; % number of grid points in y-direction
dx = Lx/(nx-1);
dy = Ly/(ny-1);

% Thermal diffusivity
alpha = 1;

% Time parameters
t_end = 100;
dt = 0.01;
nt = round(t_end/dt);

% Initialize temperature
T = zeros(nx,ny);

% Boundary conditions
T(1,:) = 0;    % left
T(nx,:) = 0;   % right
T(:,1) = 100;  % bottom
T(:,ny) = 0;   % top

% Coefficients
ae = alpha/dx^2;
aw = alpha/dx^2;
an = alpha/dy^2;
as = alpha/dy^2;
ap = -(2*ae + 2*as);

% Time loop
for n = 1:nt
    % Jacobi solver
    T_old = T;
    for j = 2:ny-1
        for i = 2:nx-1
            T(i,j) = (1/ap)*(...
                ae*T_old(i+1,j) + aw*T_old(i-1,j) + ...
                an*T_old(i,j+1) + as*T_old(i,j-1));
        end
    end
    
    % Plotting
    if any(n == [1,2,5,10,100])
        figure;
        imagesc(T');
        colorbar;
        title(['Temperature at t=',num2str(n*dt)]);
        xlabel('x');
        ylabel('y');
        axis equal tight;
    end
end
1077 chars
56 lines

This code uses the implicit Euler method in time and central differencing in space to solve a 2D heat equation with constant thermal diffusivity alpha. The time loop contains a Jacobi solver that iteratively updates the temperature T until it converges to the new solution. The discretized unknowns are T(i,j) at the grid points (i,j), and the coefficients ae, aw, an, as, ap depend on the grid spacing and the thermal diffusivity.

To plot the temperature at particular times, the code checks if the current time step n is in a predefined list, and if so, it creates a new figure with an image plot of the temperature using imagesc(T') (note the transpose, which makes the plot have the same orientation as the plate). The plot is labeled with the current time n*dt and other relevant information. The axis equal tight command is used to make the plot square and remove any extra margins.

gistlibby LogSnag