-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheat_1D_implicit.m
69 lines (59 loc) · 1.51 KB
/
heat_1D_implicit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
% Solves the 1D heat equation with an explicit finite difference scheme
%
% From Thorsten Becker's webpage
%
clear all
close all
% Physical parameters
L = 100; % Length of modeled domain [m]
Tmagma = 1200; % Temperature of magma [C]
Trock = 300; % Temperature of country rock [C]
kappa = 1e-6; % Thermal diffusivity of rock [m2/s]
W = 5; % Width of dike [m]
day = 3600*24; % # seconds per day
dt = 10*day; % Timestep [s]
% Numerical parameters
nx = 201; % Number of gridpoints in x-direction
nt = 40; % Number of timesteps to compute
dx = L/(nx-1); % Spacing of grid
x = -L/2:dx:L/2;% Grid
% Setup initial temperature profile
T = ones(size(x))*Trock;
T(find(abs(x)<=W/2)) = Tmagma;
% XXX Construct A matrix
A = sparse(nx,nx);
A(1,1)=1;
sx = kappa*dt/dx^2;
for ii=2:nx-1
A(ii,ii-1) = -sx;
A(ii,ii) = 1+2*sx;
A(ii,ii+1) = -sx;
end
A(nx,nx)=1;
time = 0;
T = T';
for n=1:nt % Timestep loop
% Compute new temperature
Tnew = zeros(nx,1);
Tnew = inv(A)*T;
% Set boundary conditions
Tnew(1) = T(1);
Tnew(nx) = T(nx);
% Update temperature and time
T = Tnew;
time = time+dt;
% Plot solution
figure(1), clf
plot(x,Tnew);
Ymat(n,:) = n*dt*(ones(1,nx));
Xmat(n,:) = x;
Tmat(n,:) = Tnew;
axis([-50 50 200 1300])
xlabel('x [m]')
ylabel('Temperature [^oC]')
title(['Temperature evolution after ',num2str(time/day),' days'])
drawnow
end
figure
surf(Xmat,Ymat,Tmat)
%shading flat