You are on page 1of 3

clear all

close all
clc

%% Defining the mesh


n_points = 51;
dom_size = 1;
h = dom_size/(n_points-1);
rho = 7750;
heat_cap = 500;
conductivity = 16.2;
thermal_diffusivity = (conductivity/(rho*heat_cap));
dt = 0.1;
alpha = ((thermal_diffusivity*dt)/(h*h));

%% Initializing the problem


T(n_points,n_points) = 150;
T(1,:) = 200;
T(:,1) = 100;
T(n_points,:) = 100;
T(:,n_points) = 100;

T_new(n_points,n_points) = 150;
T_new(1,:) = 200;
T_new(:,1) = 100;
T_new(n_points,:) = 100;
T_new(:,n_points) = 100;

error_mag = 1;
error_req = 1e-4;
iterations = 0;

%% Calculation
while error_mag > error_req
for i = 2:(n_points-1)
for j = 2:(n_points-1)
T_new(i,j) = T(i,j) + alpha*(T(i-1,j) + T(i+1,j) + T(i,j-1) +
T(i,j+1) - 4*T(i,j));
end
end
iterations = iterations + 1;
% Calculation of error
error_mag = 0;
for i = 2:(n_points-1)
for j = 2:(n_points-1)
error_mag = error_mag + abs(T(i,j) - T_new(i,j));
end
end
if rem(iterations,1000) == 0
iterations;
error_mag;
end
% Assigning new to be old
T = T_new;
end
%% Plotting
x_dom = ((1:n_points)-1).*h;
y_dom = 1-((1:n_points)-1).*h;
[X,Y] = meshgrid(x_dom,y_dom);
contourf(X,Y,T, 12)
colorbar
title('Final Temperature Grid')

%% Centre line plot


y_centre = T(:, ((n_points-1)/2));
figure;
plot(y_centre)

You might also like