Linear Methods of Applied Mathematics

Solving the Heat Equation

(c) Copyright 1994-1997 by Evans M. Harrell II and James V. Herod. 
     All rights reserved.

Notes for the instructor.

This contains calculations and examples which correlate with chapter 9 of the WWW text by Harrell and Herod.  Students can be encouraged to cut and paste from this notebook to do homework.

Instructions

This notebook uses Mathematica  to perform calculations for Harrell and Herod's hypertext book, Linear Methods of Applied Mathematics.   The student needs only a basic knowledge of Mathematica  to use the notebook, which is designed both to show how to work problems in the text and to provide a template for using Mathematica  to work other problems of the student's own design.  

Calculations will be performed when the reader presses enter in a given calculation cell (bold print).  It is best to activate the cells in order, so that Mathematica  will be able to call on operators and functions defined in earlier cells.  Red color coding is used to warn the reader when a given calculation relies on an earlier one, and magenta color coding is used to warn the reader when a calculation requires Mathematica's vector analysis package to be loaded.

A useful substitution, which we shall often make, is:

TrigId = {Cos[Pi n_] -> (-1)^n, Sin[Pi n_] -> 0};

Please activate this cell before proceeding.  Also, for the multidimensional calculations we shall invoke the vector calculus analysis with:

Needs["Calculus`VectorAnalysis`"]

The Laplace Operator

The Laplace operator and other vector derivatives are recognized by Mathematica's vector analysis package.  The creators of Mathematica  have chosen to designate the Laplace operator as Laplacian (though many writers prefer the spelling  Laplacean).

Mathematica  knows the definition of the Laplace operator symbolically as well as operationally:

Laplacian[f[x,y,z]] == Div[Grad[f[x,y,z]]]

Unless and until we instruct Mathematica  otherwise, Cartesian variables named {x,y,z} are assumed.

Examples

Grad[x y/z]

Div[{y/z, x/z, -((x y)/z^2)}]

Curl[{y/z, x/z, -((x y)/z^2)}]

(The curl will not be important in this chapter.)

Laplacian[x y/z]

Separation of variables and examples

As in the "flat" text, we separate variables for a rectangle with homogeneous boundary conditions on three sides.  Suppose that 0 < x < a and 0 < y < b.  On this rectangle, u[x,y] solves Laplace's equation in two variables.

We can define Laplace's equation as a logical operator, in case we simply wish to test whether a function is harmonic (= solution of Laplace's equation):

IsItHarmonic[u_] := Laplacian[u] == 0

Examples

IsItHarmonic[x^2 + y]

IsItHarmonic[Sin[x]Sinh[y] + 5]

Separation of variables

Separation of variables works much as it did for the heat equation and the wave equation.  As with our earlier equations, we begin with the ansatz  that u is a product solution 
                 u[x,y] = X[x] Y[y]
and substitute into Laplace's equation; it is again convenient to divide the result by u.  We use first information from the PDE, then the information from homogeneous boundary conditions, and lastly information from inhomogeneous boundary conditions, which are treated much like the initial conditions for the earlier PDE's.

Simplify[Laplacian[X[x] Y[y]]/(X[x] Y[y]) == 0]

Evidently, if the y-independent quantity X''/X equals the x-independent quantity -Y''/Y, both must be a constant, and we have the familiar ordinary differential equations,
	      X''  =  mu X
	      -Y'' =  mu Y

(notice the different sign).

Rather than tackling non-homogeneous boundary conditions on all four edges of a rectangle all at once, we begin by setting three of the four boundary functions to 0.  For definiteness, we also make a specific choice of the fourth while developing the ideas.  The specific example we analyze is:

Model Problem IX.1. Let us solve Laplace's equation with boundary conditions which are homogeneous on three of the four sides of a rectangle:

	u(0,y) = 0								 (9.7)
	u(1,y) = 1								 (9.8) 	
	u(x,0) = 0								 (9.9) 	
	u(x,2) = 0.								(9.10)

A good rule of thumb in this subject is to deal with the most homogeneous boundaries first.  It is the variable y which has two homogeneous boundary conditions in this case.  It satisfies essentially the same eigenvalue problem as we have seen in previous chapters:
	      -Y'' =  mu Y
	      Y[0] = 0, Y[2] = 0

HeatOp[f_] := HeatOp[f,1]

In this cell you may try out the wave operator.

HeatOp[t^3 Sin[x]^2]

That function was not a solution.  The following one is:

HeatOp[Exp[-t] Sin[x]] == 0

Solving the heat equation with the method of separation of variables - derivation

To simplify matters a bit, we assume that k = 1 and x ranges from 0 to 1.  The more general solution can be obtained by scaling and shifting the variables t and x.  

We try to solve the wave equation with the ansatz that u[t,x] is of the special form T[t] X[x], known as a product solution.

u[t_,x_] := T[t] X[x]

HeatOp[u[t,x]]

This simplifies if we divide through by T[t] X[x]:

%/(T[t] X[x])

Simplify[%]

If u solves the wave equation, this expression must be zero. (We could quibble about what happens when T or X = 0, but that would either mean that u is the uninteresting "trivial" solution or else the problem occurs only at isolated values of x and t.)  Thus 
                        T'/T = X''/X.
Since the left side is independent of x and the right side is independent of t, both sides must in fact equal a constant. We don't know its value yet, so let us call it - mu. (It turns out to be negative.)
Thus X''[x] = - mu X[x]:

DSolve[- X''[x]== mu X[x],X[x],x]

We now remind Mathematica about Euler's interesting formula for complex exponentials:

% /. {E^(I Sqrt[mu] x) -> Cos[Sqrt[mu] x] + 
               I Sin[Sqrt[mu] x],
         E^(- I Sqrt[mu] x) -> Cos[Sqrt[mu] x] -
                  I Sin[Sqrt[mu] x]}

Evidently, we could reexpress this as

Clear[X,D1,D2]

X[x_] := D1 Cos[Sqrt[mu] x] + 
        D1 Sin[Sqrt[mu] x]

X[0]

This means that in order to satisfy the BC at x=0, we must have:

X[x_] := Sin[Sqrt[mu] x]

Solve[X[L] == 0, mu]

Clear[X]

X[x_,n_Integer] := Sin[n Pi x/L]

Since the second derivative of this is - (n Pi/L)^2 Sin[n Pi x/L], we see that

mu[n_Integer] := -(n Pi/L)^2

Now let's look at the other part of the PDE which we "separated off":
T'[t] = - mu T[t]

As we saw, there are many possible values of \mu which are consistent with the boundary condition for the function X, and the same possibilities occur in equation (6.3) (multiply \mu by k here if k is not set to 1). Therefore
         T[t_,n_] = A[n] Exp[- (n \pi)^2 t].
         
We cannot go further with the function T without being given the initial condition, that is, information about what happens at t=0.

The general solution we come up with is a linear combination of the particular solutions we get by separating the equation. It is not yet obvious that this is a completely general solution, but it is. The most general linear combination is of the form:

u[t_,x_] = Sum[c[n] Exp[- (n Pi) t ] Sin[n Pi x],        {n,1,infinity}]

The coefficients A[n] have to be determined from the initial condition, u(0,x); they are the Fourier sine coefficients of u(0,x).  (See the examples in the next section.)

Automating the solution to the heat equation
The general solution of the heat equation with 
						(i)  homogeneous Dirichlet boundary conditions at x=a and x=b, 
and 
      (ii)  initial condition
      
	           u[0,x] == f[x]
can be automated with the Mathematica commands in this section.

We break the solution into two steps, the first of which calculates the Fourier sine coefficients for the initial condition.

B[f_,m_, {a_,b_}] := (2/(b-a)) Integrate[                           (Sin[m Pi intvar/(b-a)] f  /. x -> intvar),                {intvar, a, b}]            /. TrigId

HeatSol[f_,k_,t_,{x_,a_,b_}, infinity_] :=                              Sum[B[f,n, {a,b}] Exp[-(n Pi) k t] Sin[n Pi x/(b-a)],          {n,1,infinity}]

These commands allow a lot of flexibility.  It will often be more convenient to simplify them a standard form, assuming that the variables have been scaled so that k == 1 and 0 < x < 1:

B[f_,m_] := 2 Integrate[                                                  (Sin[m Pi intvar] f  /. x -> intvar),                         {intvar, 0,1}]            /. TrigId

HeatSol[f_,t_,x_, infinity_] :=                                           Sum[B[f,n] Exp[- (n Pi) t ] Sin[n Pi x],                      {n,1,infinity}]

Examples - heat equation

1.  k = 1, 0 < x < 1, and the rod is initially at a constant temperature.  (Model Problem VIII.1)

Model Problem VIII.1.  Find the future temperature within a rod of length 1, k=1, insulated along its length, and with ends in thermal contact with ice water, if at time 0 the rod is at a uniform temperature u(0,x) = 50.

HeatSol[50,t,x, 7]

(We approximate infinity by the large number 7!)  Let us plot the solution in time steps of 0.2:

Table[Plot[%,{x,0,1}, PlotRange -> {0,60}], {t, 0, 1, .2}]

2.  k = 1, 0 < x < 1, and the rod is initially at temperature 100 x.

Model Problem VIII.1.  Find the future temperature within a rod of length 1, k=1, insulated along its length, and with ends in thermal contact with ice water, if at time 0 the rod is at a uniform temperature u(0,x) = 50.

HeatSol[100 x,t,x, 7]

(We approximate infinity by the large number 7!)  Let us plot the solution in time steps of 0.2:

Table[Plot[%,{x,0,1}, PlotRange -> {0,110}], {t, 0, 1, .2}]

3.  Observing the smoothing property, approach to equilibrium, and maximum principle.

In the graphs of the previous two examples we easily several general features:
       1.  u[t,x] is a smooth function
       2.  u[t,x] tends to the equilibrium value 0
       3.  The way in which u[t,x] approaches equilibrium is the temperature distribution rather rapidly rearranges itself to have the shape of the fundamental normal mode Sin[Pi x].
       4.  The maximum and minimum values of the temperature occur at the space-time boundary:  x = 0, x = 1, t = 0, or t = tmax (whatever the stopping time is).

The initial conditions for these problems were, however, particularly simple.  Here are some other solutions of the heat equation the graphs of which will reveal the same features.  We construct them as linear combinations of simple solutions of the heat equation, with various boundary conditions.

1.

HeatOp[2t + x^2- 5] == 0

u1[t_,x_] = 2t + x^2- 5 + Sum[(-1)^(n+1) 10 /(1 + Sqrt[n]) Exp[- n^2 Pi^2 t]       Sin[n Pi x], {n, 1, 6}];

Table[Plot[u1[t,x],{x,0,1}, PlotRange -> {-15,15}], {t, 0, .2, .05}]

2.

HeatOp[Exp[- n^2 Pi^2 t] Sin[n Pi x + n]] == 0

u2[t_,x_] = Sum[(-1)^n n Exp[- n^2 Pi^2 t]       Sin[n Pi x + n], {n, 1, 6}];

Table[Plot[u2[t,x],{x,0,1}, PlotRange -> {-15,15}], {t, 0, .1, .02}]

The temperature distribution with radiative loss of energy, using method of separation of variables

If the insulation along the rod is imperfect, we may model the loss with a modified heat operator of the form:

ModHeatOp[u_] := D[u,t] - D[u,{x,2}] - .2 u

We shall impose Dirichlet conditions at 0 and 1.

Simplify[ModHeatOp[T[t] X[x]]/(T[t] X[x])]

The eigenvalue equation becomes:

DSolve[- X''[x] + (1/5) X[x] == mu X[x],X[x],x]

In order that x[0] == 0, C[2] = - C[1], so the eigenfunctions are proportional to

X[x_,mu_] := Sin[Sqrt[5 mu - 1] x/Sqrt[5]]

In order for this to equal 0 when x=1, mu -> (5 n^2 Pi^2 - 1)/5, so the separated solutions become 
        Exp[- ((1 - 5 n^2 Pi^2)/5) t] Sin[n Pi x]

ModHeatSol[f_,t_,x_, infinity_] :=                                           Sum[B[f,n] Exp[- ((5 n^2 Pi^2 - 1)/5) t ] Sin[n Pi x],                      {n,1,infinity}]

ModHeatSol[4 x^2,t,x,8];

Table[Plot[%,{x,0,1}, PlotRange -> {0,5}], {t, 0, 1, .2}]

Exercises

1.  Use Mathematica to automate the solution of the heat equation with Neumann boundary conditions at a and b.  Use the result to solve some specificinitial-boundary-value problems of your own design.

2.  Modify the solutions in this notebook to solve an initial-boundary-value problem with nonhomogeneous boundary conditions, for example:
	              ut  == uxx
	              ux[t,0] = 0, ux[t,1] = 1
	              u[0,x] = 0.

3.  We may model a rod in ice water which is in thermal contact at its ends, imperfectly insulated along its length, and with an internal source of heat (chemical or radioactive) by:
	              ut  == uxx= - (1/5) u + 1
	              ux[t,0] = 0, u[t,1] = 0
	              u[0,x] = 0. 
	Solve.