-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusion_equationexplicit.m
59 lines (40 loc) · 1.08 KB
/
Diffusion_equationexplicit.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
%du/dt = Dd2u/dx2
%using forward euler equation for dudt and central differencing for d2udx2
%Heat Difussion in one dimensional wire using explicit schemes
%Range of space and time
clear
L=1.; %Length of wire
T=1.; %Final time
Tstep = 2500; %Time descretisation
dt=T/Tstep; %Time difference
n=50; %spatial descretisation
dx=L/n;
cond = 1/4; %conductivity
b=2.*cond*dt/(dx*dx); %stability parameter (b=<1)
%initial temperature of the wire: a sinus (initialisation)
for i= 1:n+1
x(i)=(i-1)*dx;
u(i,1)=sin(pi*x(i));
end
%temperature at the boundary(T=0)
for k=1:Tstep+1
u(1,k) = 0.;
u(n+1,k)=0.;
time(k)=(k-1)*dt;
end
%implementation of explicit method
for k=1:Tstep %time loop
for i=2:n %spaceloop
u(i,k+1)=u(i,k)+0.5*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
end
end
figure(1)
plot(x,u(:,1),'-',x,u(:,100),'-',x,u(:,600),'-');
title('Temperature within the explicit method')
xlabel('X')
ylabel('T')
figure(2)
mesh(x,time,u')
title('Temperature within the explicit method')
xlabel('X')
ylabel('T')