This report provides a practical overview of numerical solutions to the heat equation using the finite difference method (FDM). The forward time, centered space (FTCS), the backward time, centered space (BTCS), and Crank-Nicolson schemes are developed, and applied to a simple problem in1volving the one-dimensional heat equation. Complete, working Matlab and FORTRAN codes for each program are presented. The results of running the codes on finer (one-dimensional) meshes, and with smaller time steps are demonstrated. These sample calculations show that the schemes realize theoretical predictions of how their truncation errors depend on mesh spacing and time step. The Matlab codes are straightforward and allow us to see the differences in implementation between explicit method (FTCS) and implicit methods (BTCS). The codes also allow us to experiment with the stability limit of the FTCS scheme.
2. 2
PROGRAM LIST
PROG.
NO.
PROGRAM DESCRIPTION COMPILER PAGE
NO.
1 Error difference between analytical
solution and finite difference method
FORTRAN
95
3
2 Solving 1D steady state differential heat
equation
FORTRAN
95
3 Solving 2D steady state heat equation FORTRAN
95
4 Solving 1D transient heat equation MATLAB
5 Solving 2D transient heat equation MATLAB
6 Investigation of the sprinkler response
time with activation temperature
MATLAB
7 Solving steady state advection-diffusion
equation
FORTRAN
95
8 Solving 1D time dependent advection
diffusion using FOU scheme
FORTRAN
95
3. 3
FINITE DIFFERENCE MODELLING FOR HEAT TRANSFER PROBLEMS
Rahul Roy
Department of Mechanical Engineering, Jadavpur
University, Kolkata 700032, India
INTRODUCTION
This report provides a practical overview of numerical solutions to the heat equation using the
finite difference method (FDM). The forward time, centered space (FTCS), the backward time,
centered space (BTCS), and Crank-Nicolson schemes are developed, and applied to a simple
problem in1volving the one-dimensional heat equation. Complete, working Matlab and
FORTRAN codes for each program are presented. The results of running the codes on finer
(one-dimensional) meshes, and with smaller time steps are demonstrated. These sample
calculations show that the schemes realize theoretical predictions of how their truncation
errors depend on mesh spacing and time step. The Matlab codes are straightforward and allow
us to see the differences in implementation between explicit method (FTCS) and implicit
methods (BTCS). The codes also allow us to experiment with the stability limit of the FTCS
scheme.
FINITE DIFFERENCE METHOD
The finite difference method is one of several techniques for obtaining numerical solutions. In
all numerical solutions the continuous partial differential equation (PDE) is replaced with a
discrete approximation. In this context the word “discrete” means that the numerical solution is
known only at a finite number of points in the physical domain. The user of the numerical
method can select the number of those points. In general, increasing the number of points not
only increases the resolution, but also the accuracy of the numerical solution. The discrete
approximation results in a set of algebraic equations that are evaluated (or solved) for the
values of the discrete unknowns.
The mesh is the set of locations where the discrete solution is computed. These points are called
nodes, and if one were to draw lines between adjacent nodes in the domain the resulting image
would resemble a net or mesh. Two key parameters of the mesh are ∆x, the local distance
between adjacent points in space, and ∆t, the local distance between adjacent time steps.
The core idea of the finite-difference method is to replace continuous derivatives with so called
difference formulas that involve only the discrete values associated with positions on the mesh.
Applying the finite-difference method to a differential equation involves re- placing all
derivatives with difference formulas. In the heat equation there are derivatives with respect to
time, and derivatives with respect to space. Using different combinations of mesh points in the
difference formulas results in different schemes. In the limit as the mesh spacing (∆x and ∆t) go
to zero, the numerical solution obtained with any useful scheme will approach the true solution
to the original differential equation. However, the rate at which the numerical solution
approaches the true solution varies with the scheme. In addition, there are some practically
useful schemes that can fail to yield a solution for bad combinations of ∆x and ∆t.
4. 4
PROGRAM 1
A simple, basic Fortran95 code is written to find the tangent of the function 𝑓 𝑥 = cos (𝑥) by
using finite difference approach. This approach uses the concept of discretization of space and
time into distinct points or units. Every equation is satisfied for each distinct point or steps and
a numerical iteration is carried out to reach a optimum solution. The numerical solution
approaches the true solution as the number of grids is increased. The finite difference
approximation becomes more accurate as grid spacing decreases.
FORTRAN CODE FOR PROGRAM 1
1 program Heat
2 implicit none
3 integer :: n
4 integer :: i
5 real,allocatable :: y(:),dydx(:)
6 real :: x,dx
7 write(*,*) 'Enter No. of grid Points'
8 read(*,*) n
9 allocate (y(n) , dydx(n))
10 dx=10.0/(n-1)
11 do i=1,n
12 x=(i-1)*dx
13 y(i)=cos(x)
14 end do
15 call derivative (y,n,dx,dydx)
16 do i=1,n
17 x=(i-1)*dx
18 print *, dydx(i), -sin(x) , -sin(x)-dydx(i)
19 end do
20 deallocate (y,dydx)
21 contains
22 subroutine derivative (a,np,h,aprime)
23 integer :: np
24 real :: a(np),h
25 real :: aprime(np)
26 do i=1,np-1
27 aprime(i)=(a(i+1)-a(i))/h
28 end do
29 aprime(np) = 0
30 end subroutine derivative
31 end program Heat
The above program illustrates the difference between the values obtained from actual derivatives and
the finite difference approximations. The difference between the two values gives the error due to the
scheme. A graph has been plotted to obtain the results of the finite difference method in Figure 1. The
results show that there is only slight error between the two values.
5. 5
Figure 1. Differentiation of the function f(x) using (i) Finite Difference Method
(ii) Calculus Method
PROGRAM 2
A FORTRAN95 program which applies the finite difference method to estimate the
solution of the steady state (time independent) heat equation over a one-dimensional
region, which can be thought of as a thin metal rod. We will assume the rod extends over
the range A <= X <= B. The quantity of interest is the temperature U(X) at each point in the
rod. We will assume that the temperature of the rod is fixed at known values at the two
endpoints. The material of the rod is considered as homogenous and isotropic.
Symbolically, we write the boundary conditions: U(A)=Ua and U(B)=Ub. Inside the rod, we
assume that a version of the steady (time independent) heat equation applies. This
assumes that the situation in the rod has "settled down", so that the temperature
configuration has no further tendency to change. The equation we will consider is:
−
𝑑
𝑑𝑥
𝑘(𝑥)
𝑑𝑈 𝑥
𝑑𝑥
= 𝐹(𝑥)
Here, the right hand side term F(X) allows us to consider internal heat sources in the
metal - perhaps a portion of the rod is sitting above a blow torch. The coefficient K(X) is a
measure of heat conductivity. It measures the rate at which the heat from a local hot spot
will spread out. If the heat source function F(X) is zero everywhere, and if K(X) is constant,
then the solution U(X) will be the straight line function that matches the two endpoint
values. Making F(X) positive over a small interval will "heat up" that portion. We can
simulate a rod that is divided into regions of different materials by setting the function
K(X) to have a given value K1 over some subinterval of [A,B] and value K2 over the rest of
the region. To estimate the value of the solution, we will pick a uniform mesh of N points
6. 6
X(1) through X(N), from A to B. At the i-th point, we will compute an estimated
temperature U(i). To do this, we will need to use the boundary conditions and the
differential equation. Since X(1) = A and X(N) = B, we can use the boundary conditions to
set U(1) = Ua and U(N) = Ub. For the points in the interior, we need to approximate the
differential equation in a way that allows us to determine the solution values. We will do
this using a finite difference approximation. Suppose we are working at node X(I), which is
associated with U(I). Then a centered difference approximation to the double derivative
is:
−
𝑑
𝑑𝑥
𝑘 𝑥
𝑑𝑈 𝑥
𝑑𝑥
= −
(𝑘(𝑋 𝑖 +
Δ𝑥
2
)(𝑈 𝑋 𝑖 + 1 − 𝑈 𝑋 𝑖 )
Δ𝑥
−
(𝑘(𝑋 𝑖 −
Δ𝑥
2
)(𝑈 𝑋 𝑖 − 𝑈 𝑋 𝑖 − 1 )
Δ𝑥!
If we rearrange the terms in this approximation, and set it equal to F(X(i)), we get the
finite difference approximation to the differential equation at X(i):
−𝑘 𝑋 𝑖 −
Δ𝑥
2
∗ 𝑈 𝑋 𝑖 − 1 + 𝑘 𝑋 𝑖 −
Δ𝑥
2
+ 𝑘 𝑋 𝑖 +
Δ𝑥
2
∗ 𝑈 𝑋 𝑖 − 𝑘 𝑋 𝑖 +
Δ𝑥
2
∗ 𝑈 𝑋 𝑖 + 1
= Δ𝑥 ∗ Δ𝑥 ∗ 𝐹(𝑋 𝑖 )
This means that we have N-2 equations, each of which involves an unknown U(i) and its
left and right neighbors, plus the two boundary conditions, which give us a total of N
equations in N unknowns. We can set up and solve these linear equations using a matrix A
for the coefficients, and a vector RHS for the right hand side terms, and calling a function
to solve the system A*U=RHS will give us the solution. Because finite differences are only
an approximation to derivatives, this process only produces estimates of the solution.
Usually, we can reduce this error by decreasing Δ𝑥.
FORTRAN CODE FOR PROGRAM 2
1 program HEAT
2 implicit none
3 integer :: n
4 real :: a, b, dx, f, k
5 integer :: i
6 real :: rhs(n), tri(3,n)
7 real :: u(n), x(n)
8 real :: ua, ub, xm, xp
9 a=0
10 b=1
11 ua=0
12 ub=1
13 n=11
14 dx=(b-a)/(n-1)
15 do i=1,n
16 x(i)=((n-1)*a+(i-1)*b)/(n-1)
17 end do
18 tri(1,1)=0
19 tri(2,1)=1
7. 7
20 tri(3,1)=0
21 rhs(1)=ua
22 do i=2,n-1
23 xm=(x(i-1)+x(i))/2
24 xp=(x(i)+x(i+1)/2
25 tri(1,i)=(-xm)/(dx*dx)
26 tri(2,i)=(xm+xp)/(dx*dx)
27 tri(3,i)=(-xp)/(dx*dx)
28 rhs(i)=0
29 end do
30 tri(1,n)=0
31 tri(2,n)=0
32 tri(3,n)=0
33 u(1:n)=rhs(1:n)
34 do i=2,n
35 tri(2,i)=tri(2,i)-tri(3,i-1)*tri(1,i)/tri(2,i-1)
36 u(i)=u(i)-u(i-1)*tri(1,i)/tri(2,i-1)
37 end do
38 u(n)=u(n)/tri(2,n)
39 do i=n-1,1,-1
40 u(i)=(u(i)-tri(3,i)*u(i+1))/tri(2,i)
41 end do
42 u(n)=u(n)/tri(2,n)
43 do i=n-1,1,-1
44 u(i)=(u(i)-tri(3,i)*u(i+1))/tri(2,i)
45 end do
46 end program HEAT
The nomenclature for the above program is given by:
Ø n is the number of spatial points.
Ø a, b are the left and right endpoints.
Ø ua, ub are the temperature values at the left and right endpoints.
Ø k is the name of the function which evaluates K(X);
Ø f is the name of the function, which evaluates the right hand side F(X).
Ø x is the X coordinates of nodes (output);
Ø u is the computed value of the temperature at the nodes.
4 different boundary conditions are taken for the above program to simulate different
results:
1. Test 1: uses K(X) = 1, F(X) = 0, so the solution should be the straight line that
connects the boundary values.
2. Test 2: uses K(X) which is set to different constants over three sub regions, and
F(X) = 0, so the solution will be a piecewise linear function that connects the
boundary values.
3. Test 3: uses K(X) = 1, F(X) defines a heat source, so the solution can rise above the
boundary values.
4. Test 4: uses K(X) = 1, F(X) defines a heat source and a heat sink, so the solution can
go above and below the boundary values.
10. 10
FORTRAN CODE FOR PROGRAM 3
1 program HEAT
2 implicit none
3 integer :: i, j, k
4 integer :: m, n
5 integer :: L, H
6 integer :: T1, T2, T3, T4
7 real :: A(5,5), B(5,5), C(5,5)
8 T1=300
9 T2=100
10 T3=50
11 T4=75
12 m=5
13 n=5
14 do 10 j=1,n
15 do 20 i=1,m
16 C(i,j)=0
17 B(i,j)=0
18 IF (j==1) THEN A(i,j)=50
19 else if(j==n) THEN A(i,j)=300
20 else if(i==1) THEN A(i,j)=75
21 else if(i==m) THEN A(i,j)=100
22 else A(i,j)=0
23 END IF
24 20 END DO
25 10 END DO
26 DO 70 k=1,10
27 DO 50 j=2,n-1
28 DO 60 i=2,m-1
29 A(i,j)=(A(i+1,j)+A(i-1,j)+A(i,j+1)+A(i,j-1))*(0.25)
30 C(i,j)=B(i,j)/A(i,j)
31 C(i,j)=1-C(i,j)
32 60 END DO
33 50 END DO
34 do 30 j=1,n
35 do 40 i=1,m
36 write(*,*) i,j, A(i,j),C(i,j)
37 B(i,j)=A(i,J)
38 40 END DO
39 30 END DO
40 70 END DO
41 end program HEAT
13. 13
PROGRAM 4
Consider the one-dimensional, transient (i.e. time-dependent) heat conduction equation
without heat generating sources.
𝜌𝑐!
𝜕𝑇
𝜕𝑡
=
𝜕
𝜕𝑥
𝑘
𝜕𝑇
𝜕𝑥
If the thermal conductivity, density and heat capacity are constant over the model domain, the
equation can be simplified to:
𝜕𝑇
𝜕𝑡
=
𝑘
𝜌𝑐!
𝜕!
𝑇
𝜕𝑥!
= 𝛼
𝜕!
𝑇
𝜕𝑥!
We are interested in the temperature evolution versus time, T(x,t), which satisfies the
above equation, given an initial temperature distribution. The first step in the finite
differences method is to construct a grid with points on which we are interested in solving
the equation, called discretization. The next step is to replace the continuous derivatives
of the above with their finite difference approximations. First, the explicit forward time,
centered space or FTCS approximation method is used in solving the heat equation.
𝑇!
!!!
− 𝑇!
!
∆𝑡
= 𝛼
𝑇!!!
!
− 2𝑇!
!
+ 𝑇!!!
!
∆𝑥 !
𝑇!
!!!
= 𝑇!
!
+ 𝛼∆𝑡
𝑇!!!
!
− 2𝑇!
!
+ 𝑇!!!
!
∆𝑥 !
Here, n represents the temperature at the current time step whereas n + 1 represents the
new (future) temperature. The subscript i refers to the location. Both n and i are integers;
n varies from 1 to nt (total number of time steps) and i varies from 1 to nx (total number
of grid points in x-direction). Because the temperature at the current time step (n) is
known, we can use the equation to compute the new temperature without solving any
additional equations. Such a scheme is and explicit finite difference method and was made
possible by the choice to evaluate the temporal derivative with forward differences. We
know that this numerical scheme will converge to the exact solution for small ∆x and ∆t
because it has been shown to be consistent – that its discretization process can be
reversed, through a Taylor series expansion, to recover the governing partial differential
equation – and because it is stable for certain values of ∆t and ∆x: any spontaneous
perturbations in the solution (such as round-off error) will either be bounded or will
decay. The last step is to specify the initial and the boundary conditions.
1. Total time=60sec
2. The initial temperature is taken as 0.
3. The temperature at the left and right hand side of the wall is taken as 40°C and
20°C respectively.
4. The number of grids is taken as 10.
5. Length of the domain=0.1
6. Alpha is taken as 0.0001
14. 14
FLOWCHART FOR PROGRAM 4
MATLAB CODE FOR FTCS SCHEME
1 L=0.1
2 alpha=0.0001
3 t_final=60
4 n=10
5 T0=0
6 T1s=40
7 T2s=20
8 dx=L/n
9 dt=0.1
10 x=dx/2:dx:L-dx/2
11 T=ones(n,1)*T0
12 dTdt=zeros(n,1)
13 t=0:dt:t_final
14 for j=1:length(t)
15 for i=2:n-1
16 dTdt(i)=alpha*(T(i+1)+T(i-1)-2*T(i))/dx^2
17 end
18 dTdt(1)=alpha*(T(2)+T1s-2*T(1))/dx^2
19 dTdt(n)=alpha*(T2s+T(n-1)-2*T(n))/dx^2
20 T=T+dTdt*dt
21 figure(1)
22 plot(x,T, 'Linewidth',3)
23 axis([0 L 0 50])
24 xlabel('Distance (m)')
25 ylabel('Temperature (circC)')
26 pause(0.1)
27 end
15. 15
t=0.1s t=3s
t=13s t=40s
Figure 7: Output plot for FTCS scheme at various time-step
It has been observed from the above results that the initial temperature is at 0°C. After 3s,
the surface temperatures of the wall are hotter than 0°C. Hence, the heat flux comes at the
beginning at both sides and the temperature starts to rise over the whole wall. It gradually
rises until it reaches a steady state. The final temperature distribution is a straight line,
which is validated from the analytical results.
A 3D plot has been taken by taking the time axis as the third axis and plotting the
temperature distribution along the length of the wall. The total time taken to reach the
steady state is taken as 80 sec. To get a better view, the temperature on both the surfaces
of the wall is taken as 70°C and 90°C.
16. 16
Figure 8: 3D plot of temperature distribution of time-dependent 1D heat equation
For the backward time, centered space (BTCS) method, the backward difference is
selected to get:
𝑇!
!
− 𝑇!
!!!
∆𝑡
= 𝛼
𝑇!!!
!
− 2𝑇!
!
+ 𝑇!!!
!
∆𝑥 !
Unlike the FTCS method, the above equation cannot be rearranged to obtain a simple
algebraic formula. The equation is an implicit function and is a system of equations for the
values of T at the internal nodes of the spatial mesh. To see the set of system of equations
more clearly, we rearrange to get the resulting equation as:
−
𝛼
∆𝑥 !
𝑇!!!
!
+
1
Δ𝑡
+
2𝛼
∆𝑥 !
𝑇!
!
−
𝛼
∆𝑥 !
𝑇!!!
!
=
𝑇!
!!!
Δ𝑡
The set of equations can be represented in matrix form A*T=B to get the temperature
distribution at the interior nodes.
MATLAB CODE FOR BTCS SCHEME
1 nt=10
2 nx=20
3 alpha=0.1
4 L=1
5 Tmax=0.5
6 dx=L/(nx-1);
7 dt=tmax/(nt-1);
19. 19
20 Fo=alpha*dt/(dx.^2);
21 Bi=h*dx/ks;
22 k=1
23 while t<t2
24 for i=2:ni+1
25 for j=1:nj
26 if j==1
27 T(i,j,k+1)=Fo*(2*T(i-1,j,k)+T(i+,j,k)+T(i,j+1,k)+2*Bi*Th)+(1-4*Fo-2*Bi*Fo)*T(i,j,k);
28 else if j>1 && j<nj
29 T(i,j,k+1)=Fo*(T(i+1,j,k)+T(i-1,j,k)+T(i,j+1,k)+T(i,j-1,k))+(1-4*Fo)*T(i,j,k);
30 elseif j==nj
31 T(i,j,k+1)=Fo*(2*T(i-1,j,k)+T(i+1,j,k)+T(i,j-1,k)+2*Bi*Tc)+(1-4*Fo-
2*Bi*Fo)*T(i,j,k);
32 end
33 end
34 end
35 for j = 1:nj
36 T(1,j,k+1)= T(3,j,k) ;
37 T(ni+2,j,k+1)= T(ni,j,k);
38 end
39 t(k+1)= t(k) + dt;
40 k=k + 1;
41 end
42 Temp(1:ni,1:nj)=T(2:ni+1,1:nj,k-1);
43 for i = 1:ni
44 x(i)=dx*(i-1) ;
45 end
46 for j = 1:nj
47 y(j) = dx*(j-1) ;
48 end
20. 20
PROGRAM 6
A MATLAB program for the investigation of the sprinkler response time predictive
capability has been written. A heat detector response model is intended to predict the
time at which a heat detector is expected to operate given the properties of the detector
and the fire environment to which it is exposed.
The activation time of the sprinkler is the time at which the temperature of the sprinkler
bulb reaches the nominal activation temperature. Convective heat transfer from the
flowing gases in the ceiling jet to the sprinkler bulb is the primary heat transfer
mechanism. However, for an enclosure where the ceiling jet is immersed in a hot layer,
additional heat transfer from the hot layer to the sprinkler occurs. There are also heat
conduction losses from the sprinkler head to the attached pipework.
The differential equation describing the rate of change of temperature of the sensing
element of the sprinkler is given by:
𝑑𝑇!
𝑑𝑥
=
𝑉!(𝑇! − 𝑇!)
𝑅𝑇𝐼
The predicted sprinkler response time is based on a single burning object located within
the room of fire origin.
MATLAB CODE FOR PROGRAM 6
1 clear all
2 close all
3 clc
4 vel_g=5;
5 RTI=40 ;
6 T_amb=293;
7 T_g=500;
8 T_d_final=400;
9 dt= 0.1 ;
10 t(1) = 0 ;
11 T_d(1) = T_ inf;
12 i=1;
13 while T_d < T_d_final
14 del_T_d= sqrt(vel_g)/RTI*(T_g-T_d(i))*dt ;
15 T_d(i+1) = T_d(i) + del_T_d ;
16 t(i+1)= t(i) + dt ;
17 i = i + 1 ;
18 if i > 50000
19 fprintf('autobreak')
20 break
21 end
22 end
23 figure
24 plot(t,T_d)
22. 22
PROGRAM 7
The advection-diffusion equation describes physical phenomena where particles, energy,
or other physical quantities are transferred inside a physical system due to two processes:
diffusion and advection. Advection is a transport mechanism of a substance or conserved
property by a fluid due to the fluid’s bulk motion. Diffusion is the net movement of
molecules or atoms from a region of high concentration to a region of low concentration.
The advection-diffusion equation is a relatively simple equation describing flows, or
alternatively, describing a stochastically-changing system.
The general form of the 1D advection-diffusion is given as:
𝑑𝑈
𝑑𝑡
= 𝜖
𝑑!
𝑈
𝑑𝑥!
− 𝑎
𝑑𝑈
𝑑𝑥
+ 𝐹
where U is the temperature for this given problem, t is the time, 𝜖 is the diffusion
coefficient, a is the average velocity, F describes the “sources” or “sinks” of the quantity U.
The four terms represent the transient, diffusion, advection and source or sink term
respectively. For the steady state,
!"
!"
= 0.
A FORTRAN95 code has been written to numerically approximate the solution of the
advection-diffusion equation typically using the finite difference method (FDM). Our
problem reduces to:
−𝜖
𝑑!
𝑈
𝑑𝑥!
+ 𝑎
𝑑𝑈
𝑑𝑥
= 0, 0 < 𝑥 < 1 ; 𝑈 0 = 0 ; 𝑈 1 = 0
We discretize the interval [0,1] into NX nodes. We write the boundary conditions at the
first and last nodes. At the i-th interior node, we replace derivatives by differences:
𝑑𝑈
𝑑𝑥
=
(𝑈 𝑥 + 𝑑𝑥 − 𝑈(𝑥 − 𝑑𝑥))
2𝑑𝑥
𝑑!
𝑈
𝑑𝑥!
=
(𝑈 𝑥 + 𝑑𝑥 − 2𝑈 𝑥 + 𝑈 𝑥 − 𝑑𝑥 )
(𝑑𝑥)!
This becomes a set of NX linear equations in the NX unknown values of U. The exact
solution to this differential equation is known:
𝑈 =
(1 − 𝑒!"
)
(1 − 𝑒!)
𝑤ℎ𝑒𝑟𝑒 𝑟 =
𝑎
𝜖
The results for the exact analytical solution and the solution obtained from FDM has been
compared and plotted in graph. It has been seen that there is an insignificant difference
between the two values.
23. 23
FORTRAN CODE FOR PROGRAM 7
1 program Heat
2 implicit none
3 real :: a, b
4 real, allocatable :: a3(:,:)
5 real :: dx
6 real, allocatable :: f(:)
7 integer :: i,j
8 real :: k, r, v
9 integer :: nx
10 real, allocatable :: u(:),w(:),x(:)
11 real :: xm
12 v=1
13 k=0.05
14 nx=101
15 a=0
16 b=1
17 dx=(b-a)/(nx-1)
18 allocate (x(1:nx))
19 if (nx==1) then
20 x(1)=(a+b)/2
21 else
22 do i=1,nx
23 x(i)=((nx-i)*a+(i-1)*b)/(nx-1)
24 end do
25 end if
26 allocate (a3(1:nx,1:3))
27 a3(1:nx,1:3)=0
28 allocate (f(1:nx))
29 f(1:nx)=0
30 a3(1,2)=1
31 f(1)=0
32 do i=2,nx-1
33 a3(i,1)=-v/(2*dx)-k/(dx*dx)
34 a3(i,2)=(2*k)/(dx*dx)
35 a3(i,3)=v/(2*dx)-k/(dx*dx)
36 f(i)=0
37 end do
38 a3(nx,2)=1
39 f(nx)=1
40 allocate (u(1:nx))
41 do i=1,nx
42 if( a3(i,2)==0) then
43 write(*,*) “error”
44 end if
45 end do
46 u(1:nx)=f(1:nx)
47 do i=2,nx
24. 24
48 xm=a3(i,1)/a3(i-1,2)
49 a3(i,2)=a3(i,2)-xm*a3(i-1,3)
50 u(i)=u(i)-xm*u(i-1)
51 end do
52 u(nx)=u(nx)/a3(nx,2)
53 do i=nx-1,1,-1
54 u(i)=(u(i)-a3(i,3)*u(i+1))/a3(i,2)
55 end do
56 r=v*(b-a)/k
57 allocate(w(1:nx))
58 w(1:nx)=(1-exp(r*x(1:nx)))/(1-exp(r))
59 deallocate(a3)
60 deallocate(f)
61 deallocate(u)
62 deallocate(w)
63 deallocate(x)
64 end
65 end program Heat
Figure 12: Output plot for Program 7