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 = 010;
alpha = ((thermal_diffusivity*dt)/(h*h));
%% Initializing the problem
T(n_points,n_points) = 0;
T(1,:) = 200;
T(:,1) = 100;
T(n_points,:) = 100;
T(:,n_points) = 100;
T_new(n_points,n_points) = 0;
T_new(1,:) = 200;
T_new(:,1) = 100;
T_new(n_points,:) = 100;
T_new(:,n_points) = 100;
T_transient = 0;
error_mag = 1;
error_req = 1e-6;
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));
T_transient(iterations+1, 1:n_points, 1:n_points) = T_new;
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
%%Plotting a particular timestep
timestep_selected = 100;
x_dom =((1:n_points)-1).*h;
y_dom = 1-((1:n_points)-1).*h;
[X,Y] = meshgrid(x_dom,y_dom);
T_timestep = T_transient(timestep_selected, :, :);
T_timestep = reshape(T_timestep, [n_points, n_points]);
contourf(X,Y,T_timestep, 12)
colorbar
title(['Time = ' num2str(timestep_selected*dt) 's'])
%%Animation after every N timestep
N = 100;
timestep_array = 1:N:iterations;
figure;
for i=1:length(timestep_array)
timestep_selected = timestep_array(i);
T_timestep = reshape(T_timestep, [n_points, n_points]);
contourf(X,Y,T_timestep, 15)
colorbar
title(['Time = ' num2str(timestep_selected*dt) 's'])
pause(0.25);
end
%% Centre line plot
y_centre = (:, ((n_points+1)/2));
figure;
plot(y_centre)
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')