#### 1.1.1 Pure diﬀusion. Both ends at zero temperature

1.1.1.1 Example 1
1.1.1.2 Example 2
1.1.1.3 Example 3
1.1.1.4 Example 4

Problem $u_{t}=ku_{xx}$ BC\begin{align*} u\left ( 0,t\right ) & =0\qquad t>0\\ u\left ( L,t\right ) & =0\qquad t>0 \end{align*}

Initial conditions$u\left ( x,0\right ) =f\left ( x\right ) \qquad 0<x<L$ Solution

Using separation of variables, let $$u\left ( x,t\right ) =X\left ( x\right ) T\left ( t\right )$$. Substituting this back into the PDE gives\begin{align*} T^{\prime }X & =X^{\prime \prime }T\\ \frac{1}{k}\frac{T^{\prime }}{T} & =\frac{X^{\prime \prime }}{X}=-\lambda \end{align*}

Where the separation constant is some real value $$-\lambda$$. This gives the following two ODE’s to solve\begin{align} T^{\prime }+\lambda kT & =0\tag{1}\\ X^{\prime \prime }+\lambda X & =0 \tag{2} \end{align}

Starting with the spatial ODE in order to obtain the eigenvalues and eigenfunctions. The boundary conditions on the spatial ODE become\begin{align*} X\left ( 0\right ) & =0\\ X\left ( 1\right ) & =0 \end{align*}

Since equation (2) is a constant coeﬃcient ODE, its characteristic equation is $$r^{2}+\lambda =0$$, which has the solution $$r=\pm \sqrt{-\lambda }$$, therefore its solution is given by\begin{align} X\left ( x\right ) & =c_{1}e^{rx}+c_{2}e^{-rx}\nonumber \\ & =c_{1}e^{\sqrt{-\lambda }x}+c_{2}e^{-\sqrt{-\lambda }x} \tag{3} \end{align}

There are three cases to consider, depending on if $$\lambda <0,\lambda =0,\lambda >0$$. Each one of these cases give a diﬀerent solution.

Case 1 Assuming $$\lambda <0$$. Therefore $$-\lambda$$ is positive and $$\sqrt{-\lambda }$$ is also positive. Let $$\sqrt{-\lambda }=\mu ,$$ where $$\mu$$ is some positive number. The solution (3) can now be written as$$X\left ( x\right ) =c_{1}e^{\mu x}+c_{2}e^{-\mu x} \tag{3A}$$ This can be rewritten in terms of hyperbolic trig functions (to make it easier to manipulate) as$$X\left ( x\right ) =c_{1}\cosh \left ( \mu x\right ) +c_{2}\sinh \left ( \mu x\right ) \tag{3B}$$ Where the constants $$c_{i}$$ in (3A) are diﬀerent from the constants in (3B), but kept the same for simplicity of notation so as not to introduce new constants. Applying left boundary conditions to (3B) gives$0=c_{1}$ The solution (3B) now reduces to$X\left ( x\right ) =c_{2}\sinh \left ( \mu x\right )$ Applying right side boundary conditions to the above results in$0=c_{2}\sinh \left ( \mu L\right )$ But $$\sinh \left ( \mu L\right ) \neq 0$$ since it was assumed $$\mu$$ is not zero and $$\sinh$$ is only zero when its argument is zero. The only possibility then is that $$c_{2}=0,$$ which leads to trivial solution. Therefore $$\lambda <0$$ is not an eigenvalue.

Case 2. Assuming $$\lambda =0$$. The ODE becomes $$X^{\prime \prime }=0$$, which has the solution $X\left ( x\right ) =c_{1}x+c_{2}$ Applying left side B.C. gives$0=c_{2}$ The solution now reduces to $X\left ( x\right ) =c_{1}x$ Applying right side B.C. gives$0=c_{1}L$ Leading to the trivial solution. Therefore $$\lambda =0$$ is not an eigenvalue.

Case 3 Assuming $$\lambda >0$$. In this case equation $$\sqrt{-\lambda }$$ is complex and equation (3) can be expressed in terms of trig functions using Euler relation which results in$$X\left ( x\right ) =c_{1}\cos \left ( \sqrt{\lambda }x\right ) +c_{2}\sin \left ( \sqrt{\lambda }x\right ) \tag{4}$$ Applying left side B.C. gives$0=c_{1}$ Solution (4) now reduces to$$X\left ( x\right ) =c_{2}\sin \left ( \sqrt{\lambda }x\right ) \tag{5}$$ Applying right side B.C. gives$0=c_{2}\sin \left ( \sqrt{\lambda }L\right )$ Non-trivial solution implies $$\sin \left ( \sqrt{\lambda }L\right ) =0$$ or $$\sqrt{\lambda }L=n\pi$$ for $$n=1,2,3,\cdots$$. Therefore the eigenvalues are$\lambda _{n}=\left ( \frac{n\pi }{L}\right ) ^{2}\qquad n=1,2,3,\cdots$ And the corresponding eigenfunctions from (5) are$$X_{n}\left ( x\right ) =c_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) \tag{6}$$ Now that the eigenvalues are known, the solution to the time ODE (1) can be found.$T^{\prime }+\lambda _{n}kT=0$ This has the solution (using an integrating factor method)$$T_{n}\left ( t\right ) =e^{-\lambda _{n}kt} \tag{7}$$ The constant of integration is not needed for (7) since it will be absorbed with the constant of integration coming from solution of the spatial ODE (6) when these solutions are multiplied with each others below. Therefore the fundamental solution is$u_{n}\left ( x,t\right ) =T_{n}\left ( t\right ) X_{n}\left ( x\right )$ Linear combination of fundamental solutions is also a solution (since this is a linear PDE). Therefore the general solution is given by\begin{align} u\left ( x,t\right ) & =\sum _{n=1}^{\infty }u_{n}\nonumber \\ & =\sum _{n=1}^{\infty }T_{n}\left ( t\right ) X_{n}\left ( x\right ) \nonumber \\ & =\sum _{n=1}^{\infty }c_{n}e^{-k\lambda _{n}t}\sin \left ( \sqrt{\lambda _{n}}x\right ) \tag{8} \end{align}

The initial condition is now used to determine $$c_{n}$$, . At $$t=0$$, $$u\left ( x,0\right ) =f\left ( x\right )$$ and the above becomes$f\left ( x\right ) =\sum _{n=1}^{\infty }c_{n}\sin \left ( \sqrt{\lambda _{n}}x\right )$ Multiplying both sides of the above equation by eigenfunction $$\sin \left ( \sqrt{\lambda _{m}}x\right )$$ and integrating over the domain of $$f\left ( x\right )$$ gives$\int _{0}^{L}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx=\int _{0}^{L}\sum _{n=1}^{\infty }c_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx$ Interchanging the order of summation and integration gives$\int _{0}^{L}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx=\sum _{n=1}^{\infty }c_{n}\int _{0}^{L}\sin \left ( \sqrt{\lambda _{n}}x\right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx$ By the orthogonality of the sine functions, all terms in the right side vanish except when $$n=m$$, leading to\begin{align*} \int _{0}^{L}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx & =c_{m}\int _{0}^{L}\sin ^{2}\left ( \sqrt{\lambda _{m}}x\right ) dx\\ & =c_{m}\frac{L}{2} \end{align*}

Therefore (replacing $$m$$ back to $$n$$ now, since it is arbitrary)$c_{n}=\frac{2}{L}\int _{0}^{L}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=1,2,3,\cdots$

Summary of solution \begin{align*} u\left ( x,t\right ) & =\sum _{n=1}^{\infty }c_{n}e^{-k\lambda _{n}t}\Phi _{n}\left ( x\right ) \\ \Phi _{n}\left ( x\right ) & =\sin \left ( \sqrt{\lambda _{n}}x\right ) \\ c_{n} & =\frac{2}{L}\int _{0}^{L}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=1,2,3,\cdots \\ \lambda _{n} & =\left ( \frac{n\pi }{L}\right ) ^{2}\qquad n=1,2,3,\cdots \end{align*}

##### 1.1.1.1 Example 1

let $$L=1$$ meter, let $$k$$ which is the Thermal diﬀusivity be $$=0.01$$ $$\frac{m^{2}}{\sec }$$and let the initial conditions $$f\left ( x\right ) =x\left ( 1-x\right ) .$$ The animation is run for 30 seconds.

Code used to the above, including a Manipulate to compare with the numerical solution is below. To use diﬀerent initial conditions, just change the deﬁnition of $$f(x)$$ in the code and reevaluate.

##### 1.1.1.2 Example 2

Solve$\frac{\partial u}{\partial t}=k\frac{\partial ^{2}u}{\partial x^{2}}\qquad 0<x<L,t>0$ Boundary conditions\begin{align*} u\left ( 0,t\right ) & =0^{0}\\ u\left ( L,t\right ) & =0^{0} \end{align*}

Initial conditions$u\left ( x,0\right ) =\left \{ \begin{array} [c]{ccc}x & & 0<x<\frac{L}{2}\\ L-x & & \frac{L}{2}<x<L \end{array} \right .$ Solution. $$\lambda _{n}=\left ( \frac{n\pi }{L}\right ) ^{2}$$$u\left ( x,t\right ) =\frac{4L}{\pi ^{2}}\sum _{n=\text{odd}}^{\infty }\left ( \frac{1}{n^{2}}\sin \left ( \frac{n\pi }{2}\right ) \right ) \exp \left ( -k\lambda _{n}t\right ) \sin \left ( \sqrt{\lambda _{n}}x\right )$

Source code is

##### 1.1.1.3 Example 3

A bar length $$L=10$$ cm with insulated sides is initially at $$100$$ degrees. starting at $$t=0$$, the ends are held at zero degree. Find the temperature distribution in the bar at time $$t$$$\frac{\partial u}{\partial t}=\alpha ^{2}\frac{\partial ^{2}u}{\partial x^{2}}\qquad 0<x<L,t>0$ Boundary conditions\begin{align*} u\left ( 0,t\right ) & =0^{0}\\ u\left ( L,t\right ) & =0^{0} \end{align*}

Initial conditions$u\left ( x,0\right ) =T_{0}=100^{0}$ Solution

Let $u=X\left ( x\right ) T\left ( t\right )$ Substituting this into the PDE gives$\frac{1}{\alpha ^{2}}T^{\prime }X=X^{\prime \prime }T$ Dividing by $$XT$$$\frac{1}{\alpha ^{2}}\frac{T^{\prime }}{T}=\frac{X^{\prime \prime }}{X}$ Therefore$\frac{1}{\alpha ^{2}}\frac{T^{\prime }}{T}=\frac{X^{\prime \prime }}{X}=-\lambda$ Where $$\lambda$$ is some constant. Hence we obtain two ODE’s to solve. They are\begin{align*} X^{\prime \prime }+\lambda X & =0\\ X\left ( 0\right ) & =0\\ X\left ( L\right ) & =0 \end{align*}

And$T^{\prime }+\lambda \alpha ^{2}T=0$ case $$\lambda =0$$ \begin{align*} X^{\prime \prime } & =0\\ X & =Ax+B \end{align*}

From ﬁrst boundary conditions, $$X\left ( 0\right ) =0$$, the above becomes$0=B$ Hence $$X=Ax$$. From second boundary conditions $$0=AL$$, or $$A=0$$, therefore trivial solution and $$\lambda =0$$ is not an eigenvalue.

case $$\lambda >0$$$X^{\prime \prime }+\lambda X=0$ The solution is $X=A\cos \left ( \sqrt{\lambda }x\right ) +B\sin \left ( \sqrt{\lambda }x\right )$ From ﬁrst B.C.  $$0=A$$ and the solution becomes $$X=B\sin \left ( \sqrt{\lambda }x\right )$$. From second B.C.$0=B\sin \left ( \sqrt{\lambda }L\right )$ For non-trivial solution, we want $$\sin \left ( \sqrt{\lambda }L\right ) =0$$ or \begin{align*} \sqrt{\lambda }L & =n\pi \\ \lambda & =\left ( \frac{n\pi }{L}\right ) ^{2}\qquad n=1,2,3,\cdots \end{align*}

Therefore $X_{n}\left ( x\right ) =b_{n}\sin \left ( \frac{n\pi }{L}x\right )$ Now we solve the time ODE  $$T^{\prime }+\lambda _{n}\alpha ^{2}T=0$$. This has solution$T_{n}=a_{n}e^{-\lambda _{n}\alpha ^{2}t}$ Therefore the solution becomes\begin{align*} u & =\sum _{n=1}^{\infty }X_{n}\left ( x\right ) T_{n}\left ( t\right ) \\ & =\sum _{n=1}^{\infty }b_{n}\sin \left ( \frac{n\pi }{L}x\right ) a_{n}e^{-\left ( \frac{n\pi }{L}\right ) ^{2}\alpha ^{2}t}\\ & =\sum _{n=1}^{\infty }B_{n}\sin \left ( \frac{n\pi }{L}x\right ) e^{-\left ( \frac{n\pi }{L}\alpha \right ) ^{2}t} \end{align*}

To ﬁnd $$B_{n}$$, we use orthogonality. At $$t=0$$, we are given $$u\left ( x,0\right )$$. Hence at $$t=0$$ the solution becomes$u\left ( x,0\right ) =\sum _{n=1}^{\infty }B_{n}\sin \left ( \frac{n\pi }{L}x\right )$ Multiplying both sides by $$\sin \left ( \frac{m\pi }{L}x\right )$$ and integrating\begin{align*} \int _{0}^{L}u\left ( x,0\right ) \sin \left ( \frac{m\pi }{L}x\right ) dx & =\int _{0}^{L}\sum _{n=1}^{\infty }B_{n}\sin \left ( \frac{n\pi }{L}x\right ) \sin \left ( \frac{m\pi }{L}x\right ) dx\\ & =\sum _{n=1}^{\infty }B_{n}\int _{0}^{L}\sin \left ( \frac{n\pi }{L}x\right ) \sin \left ( \frac{m\pi }{L}x\right ) dx\\ & =B_{m}\frac{L}{2} \end{align*}

Therefore\begin{align*} B_{m} & =\frac{2}{L}\int _{0}^{L}u\left ( x,0\right ) \sin \left ( \frac{m\pi }{L}x\right ) dx\\ & =\frac{2}{L}\int _{0}^{L}T_{0}\sin \left ( \frac{m\pi }{L}x\right ) dx\\ & =\frac{2}{L}T_{0}\left ( \frac{-L}{m\pi }\cos \left ( \frac{m\pi }{L}x\right ) _{0}^{L}\right ) \\ & =\frac{-2}{m\pi }T_{0}\left ( \cos \left ( m\pi \right ) -\cos \left ( 0\right ) \right ) \\ & =\frac{-2}{m\pi }T_{0}\left ( \left ( -1\right ) ^{m}-1\right ) \end{align*}

Therefore, the solution is\begin{align*} u\left ( x,t\right ) & =\sum _{n=1}^{\infty }B_{n}\sin \left ( \frac{n\pi }{L}x\right ) e^{-\left ( \frac{n\pi }{L}\alpha \right ) ^{2}t}\\ & =\frac{-2T_{0}}{\pi }\sum _{n=1}^{\infty }\frac{1}{n}\left ( \left ( -1\right ) ^{n}-1\right ) \sin \left ( \frac{n\pi }{L}x\right ) e^{-\left ( \frac{n\pi }{L}\alpha \right ) ^{2}t}\\ & =\frac{4T_{0}}{\pi }\sum _{n=1,3,5,\cdots }^{\infty }\frac{1}{n}\sin \left ( \frac{n\pi }{L}x\right ) e^{-\left ( \frac{n\pi }{L}\alpha \right ) ^{2}t} \end{align*}

Here is an animation (1D views)

This is 3D animation of the same solution above

Source code is

##### 1.1.1.4 Example 4

Problem

Solve $$u_{t}=u_{xx}$$ with boundary conditions $$u\left ( -1,t\right ) =0,u\left ( 1,t\right ) =0$$ and initial conditions $$u\left ( x,0\right ) =f\left ( x\right ) .$$

Solution

There are two ways to solve this. Either directly using $$u\left ( -1,t\right ) =0,u\left ( 1,t\right ) =0$$ which is long. Or a shorter way, which is to map the domain to $$0\cdots 2$$ and then map the solution back to $$-1\cdots 1$$.

The ﬁrst (long) method is as follows

Let $$u=X\left ( x\right ) T\left ( t\right )$$. By separation of variables we obtain the following two ODEs. The spatial ODE is\begin{align} X^{\prime \prime }\left ( x\right ) +\lambda X\left ( x\right ) & =0\tag{1}\\ X\left ( -1\right ) & =0\nonumber \\ X\left ( 1\right ) & =0\nonumber \end{align}

And the time ODE$$T^{\prime }\left ( t\right ) +\lambda T=0 \tag{2}$$ We can quickly see that $$\lambda >0$$ is only possible case. Hence solution to (1) is$$X\left ( x\right ) =A\cos \left ( \sqrt{\lambda }x\right ) +B\sin \left ( \sqrt{\lambda }x\right ) \tag{2A}$$ Applying boundary conditions $$X\left ( -1\right ) =0$$ gives\begin{align} 0 & =A\cos \left ( -\sqrt{\lambda }\right ) +B\sin \left ( -\sqrt{\lambda }\right ) \nonumber \\ & =A\cos \sqrt{\lambda }-B\sin \sqrt{\lambda } \tag{3} \end{align}

Applying boundary conditions $$X\left ( 1\right ) =0$$ gives$$0=A\cos \sqrt{\lambda }+B\sin \sqrt{\lambda } \tag{4}$$ (4),(3) give the system$$\begin{pmatrix} \cos \sqrt{\lambda } & -\sin \sqrt{\lambda }\\ \cos \sqrt{\lambda } & \sin \sqrt{\lambda }\end{pmatrix}\begin{pmatrix} A\\ B \end{pmatrix} =\begin{pmatrix} 0\\ 0 \end{pmatrix} \tag{5}$$ For non-trivial solution we want\begin{align*} \begin{vmatrix} \cos \sqrt{\lambda } & -\sin \sqrt{\lambda }\\ \cos \sqrt{\lambda } & \sin \sqrt{\lambda }\end{vmatrix} & =0\\ \cos \sqrt{\lambda }\sin \sqrt{\lambda }+\cos \sqrt{\lambda }\sin \sqrt{\lambda } & =0\\ 2\cos \sqrt{\lambda }\sin \sqrt{\lambda } & =0\\ \cos \sqrt{\lambda }\sin \left ( \sqrt{\lambda }\right ) & =0 \end{align*}

But $$\cos \sqrt{\lambda }\sin \left ( \sqrt{\lambda }\right ) =\frac{1}{2}\sin \left ( 2\sqrt{\lambda }\right )$$, hence $$\frac{1}{2}\sin \left ( 2\sqrt{\lambda }\right ) =0$$ or $$2\sqrt{\lambda }=n\pi$$ for $$n=1,2,\cdots$$. Therefore$\sqrt{\lambda _{n}}=\frac{n\pi }{2}\qquad n=1,2,\cdots$ Each eigenvalue has associated eigenvector. For $$n=1$$, (5) becomes\begin{align*} \begin{pmatrix} \cos \frac{\pi }{2} & -\sin \frac{\pi }{2}\\ \cos \left ( \frac{\pi }{2}\right ) & \sin \left ( \frac{\pi }{2}\right ) \end{pmatrix}\begin{pmatrix} A_{1}\\ B_{1}\end{pmatrix} & =\begin{pmatrix} 0\\ 0 \end{pmatrix} \\\begin{pmatrix} 0 & -1\\ 0 & 1 \end{pmatrix}\begin{pmatrix} A_{1}\\ B_{1}\end{pmatrix} & =\begin{pmatrix} 0\\ 0 \end{pmatrix} \end{align*}

First equation gives $$-B_{1}=0$$. Hence $$B_{1}=0$$ and $$A_{1}$$ can be anything. Hence ﬁrst eigenfunction from (2A) is$X_{1}\left ( x\right ) =A_{1}\cos \left ( \frac{\pi }{2}x\right )$ For $$n=2$$ (5) becomes\begin{align*} \begin{pmatrix} \cos \pi & -\sin \pi \\ \cos \left ( \pi \right ) & \sin \left ( \pi \right ) \end{pmatrix}\begin{pmatrix} A_{2}\\ B_{2}\end{pmatrix} & =\begin{pmatrix} 0\\ 0 \end{pmatrix} \\\begin{pmatrix} -1 & 0\\ -1 & 0 \end{pmatrix}\begin{pmatrix} A_{2}\\ B_{2}\end{pmatrix} & =\begin{pmatrix} 0\\ 0 \end{pmatrix} \end{align*}

First equation gives $$-A_{2}=0$$. Hence $$A_{2}=0$$ and $$B_{2}$$ can be anything. Hence second eigenfunction from (2A) is$X_{2}\left ( x\right ) =B_{2}\sin \left ( \pi x\right )$ We continue this way and ﬁnd that for $$n=1,3,5,\cdots$$ the eigenfunctions are$X_{n}\left ( x\right ) =A_{n}\cos \left ( n\frac{\pi }{2}x\right )$ And for $$n=2,4,6,\cdots$$ the eigenfunctions are$X_{n}\left ( x\right ) =B_{n}\sin \left ( n\frac{\pi }{2}x\right )$ Therefore the spatial solution is\begin{align*} X\left ( x\right ) & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( n\frac{\pi }{2}x\right ) +\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( n\frac{\pi }{2}x\right ) \\ & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}x\right ) +\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) \end{align*}

The solution to the time domain ODE is $$T\left ( t\right ) =e^{-\lambda _{n}t}$$, therefore the complete solution is\begin{align*} u_{n}\left ( x,t\right ) & =X_{n}T_{n}\\ u\left ( x,t\right ) & =\sum _{n=0}^{\infty }X_{n}T_{n}\\ & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}x\right ) e^{-\lambda _{n}t}+\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) e^{-\lambda _{n}t} \end{align*}

To ﬁnd $$A_{n}$$, we apply orthogonality. At $$t=0$$. For $$n=1,3,\cdots$$ case\begin{align*} f\left ( x\right ) & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}x\right ) \\ \int _{-1}^{1}f\left ( x\right ) \cos \left ( \sqrt{\lambda _{m}}x\right ) dx & =\int _{-1}^{1}\left ( \sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}x\right ) \right ) \cos \left ( \sqrt{\lambda _{m}}x\right ) dx\\ & =A_{m}\int _{-1}^{1}\cos ^{2}\left ( \sqrt{\lambda _{m}}x\right ) dx\\ & =A_{m} \end{align*}

To ﬁnd $$B_{n}$$, we apply orthogonality. At $$t=0$$. For $$n=2,4,\cdots$$ case\begin{align*} f\left ( x\right ) & =\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) \\ \int _{-1}^{1}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx & =\int _{-1}^{1}\left ( \sum _{n=1,3,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) \right ) \sin \left ( \sqrt{\lambda _{m}}x\right ) dx\\ & =B_{m}\int _{-1}^{1}\sin ^{2}\left ( \sqrt{\lambda _{m}}x\right ) dx\\ & =B_{m} \end{align*}

Hence the solution is\begin{align} u\left ( x,t\right ) & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}x\right ) e^{-\lambda _{n}t}+\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) e^{-\lambda _{n}t}\tag{6}\\ \sqrt{\lambda _{n}} & =\frac{n\pi }{2}\qquad n=1,2,\cdots \nonumber \\ A_{n} & =\int _{-1}^{1}f\left ( x\right ) \cos \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=1,3,5,\cdots \nonumber \\ B_{n} & =\int _{-1}^{1}f\left ( x\right ) \sin \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=2,4,6,\cdots \nonumber \end{align}

To verify

Example 1 here is solution for $$f\left ( x\right ) =1-x^{2}$$ which satisﬁes boundary conditions. Using this we ﬁnd

\begin{align*} A_{n} & =\int _{-1}^{1}\left ( 1-x^{2}\right ) \cos \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=1,3,5,\cdots \\ & =\frac{-16}{\left ( n\pi \right ) ^{3}}\left ( n\pi \cos \left ( \frac{n\pi }{2}\right ) -2\sin \left ( \frac{n\pi }{2}\right ) \right ) \end{align*}

And\begin{align*} B_{n} & =\int _{-1}^{1}\left ( 1-x^{2}\right ) \sin \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=2,4,6,\cdots \\ & =0 \end{align*}

Hence analytical solution (6) is\begin{align*} u\left ( x,t\right ) & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}\pi x\right ) e^{-\lambda _{n}t}\\ A_{n} & =\frac{-16}{\left ( n\pi \right ) ^{3}}\left ( n\pi \cos \left ( \frac{n\pi }{2}\right ) -2\sin \left ( \frac{n\pi }{2}\right ) \right ) \\ \sqrt{\lambda _{n}} & =\frac{n\pi }{2}\qquad n=1,3,5,\cdots \end{align*}

Animation of the above

Example 2 $$f\left ( x\right ) =\left ( 1-x^{2}\right ) x$$ which satisﬁes boundary conditions. Using this we ﬁnd\begin{align*} A_{n} & =\int _{-1}^{1}\left ( 1-x^{2}\right ) x\cos \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=1,3,5,\cdots \\ & =0 \end{align*}

And\begin{align*} B_{n} & =\int _{-1}^{1}\left ( 1-x^{2}\right ) x\sin \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=2,4,6,\cdots \\ & =\frac{-16}{\left ( n\pi \right ) ^{4}}\left ( 6n\pi \cos \left ( \frac{n\pi }{2}\right ) +\left ( -12+n^{2}\pi ^{2}\right ) \sin \left ( \frac{n\pi }{2}\right ) \right ) \end{align*}

Hence analytical solution (6) is\begin{align*} u\left ( x,t\right ) & =\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}\pi x\right ) e^{-\lambda _{n}t}\\ B_{n} & =\frac{-16}{\left ( n\pi \right ) ^{4}}\left ( 6n\pi \cos \left ( \frac{n\pi }{2}\right ) +\left ( -12+n^{2}\pi ^{2}\right ) \sin \left ( \frac{n\pi }{2}\right ) \right ) \\ \sqrt{\lambda _{n}} & =\frac{n\pi }{2}\qquad n=2,4,\cdots \end{align*}

Animation of the above

Example 3

$$f\left ( x\right ) =(1-x^{2})(x+1)$$ which satisﬁes boundary conditions. Using this we ﬁnd\begin{align*} A_{n} & =\int _{-1}^{1}(1-x^{2})(x+1)\cos \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=1,3,5,\cdots \\ & =\frac{-16}{\left ( n\pi \right ) ^{3}}\left ( n\pi \cos \left ( \frac{n\pi }{2}\right ) -2\sin \left ( \frac{n\pi }{2}\right ) \right ) \end{align*}

And\begin{align*} B_{n} & =\int _{-1}^{1}(1-x^{2})(x+1)\sin \left ( \sqrt{\lambda _{n}}x\right ) dx\qquad n=2,4,6,\cdots \\ & =\frac{-16}{\left ( n\pi \right ) ^{4}}\left ( 6n\pi \cos \left ( \frac{n\pi }{2}\right ) +\left ( -12+n^{2}\pi ^{2}\right ) \sin \left ( \frac{n\pi }{2}\right ) \right ) \end{align*}

Hence analytical solution (6) is\begin{align*} u\left ( x,t\right ) & =\sum _{n=1,3,\cdots }^{\infty }A_{n}\cos \left ( \sqrt{\lambda _{n}}x\right ) e^{-\lambda _{n}t}+\sum _{n=2,4,\cdots }^{\infty }B_{n}\sin \left ( \sqrt{\lambda _{n}}x\right ) e^{-\lambda _{n}t}\\ A_{n} & =\frac{-16}{\left ( n\pi \right ) ^{3}}\left ( n\pi \cos \left ( \frac{n\pi }{2}\right ) -2\sin \left ( \frac{n\pi }{2}\right ) \right ) \\ B_{n} & =\frac{-16}{\left ( n\pi \right ) ^{4}}\left ( 6n\pi \cos \left ( \frac{n\pi }{2}\right ) +\left ( -12+n^{2}\pi ^{2}\right ) \sin \left ( \frac{n\pi }{2}\right ) \right ) \\ \sqrt{\lambda _{n}} & =\frac{n\pi }{2}\qquad n=1,2,3,\cdots \end{align*}

Animation of the above

The source code for Manipulate

The second (short) method is as follows

Solve $$u_{t}=u_{xx}$$ with boundary conditions $$u\left ( a,t\right ) =0,u\left ( b,t\right ) =0$$ and initial conditions $$u\left ( x,0\right ) =f\left ( x\right )$$ where $$a=-1$$ and $$b=1$$. But will use $$a,b$$ in the solution as it is more clear.

Solution

Let $$\xi =x+a$$ where $$x=-a\cdots b$$ Therefore $$\xi =0\cdots \left ( b-a\right )$$. This also implies mapping the initial temperature to $$f\left ( x+a\right )$$. Now the ODE is solved\begin{align*} u_{t} & =u_{\xi \xi }\\ u\left ( 0,t\right ) & =0\\ u\left ( L,t\right ) & =0 \end{align*}

Where $$L=\left ( b-a\right )$$. This length do not change. It is $$\left ( b-a\right )$$ in both $$x$$ and $$\xi$$, since it is a length and not a coordinate. The solution to the above is known to be\begin{align*} u\left ( \xi ,t\right ) & =\sum _{n=1}^{\infty }c_{n}e^{-\lambda _{n}t}\sin \left ( \sqrt{\lambda _{n}}\xi \right ) \\ c_{n} & =\frac{2}{L}\int _{0}^{L}f\left ( \xi \right ) \sin \left ( \sqrt{\lambda _{n}}\xi \right ) d\xi \\ \lambda _{n} & =\left ( \frac{n\pi }{L}\right ) ^{2}\qquad n=1,2,3,\cdots \end{align*}

The solution is now translated back to $$x$$ by replacing $$\xi$$ by $$x+a$$\begin{align*} u\left ( x,t\right ) & =\sum _{n=1}^{\infty }c_{n}e^{-\lambda _{n}t}\sin \left ( \sqrt{\lambda _{n}}\left ( x+a\right ) \right ) \\ c_{n} & =\frac{2}{L}\int _{-a}^{b}f\left ( x+a\right ) \sin \left ( \sqrt{\lambda _{n}}\left ( x+a\right ) \right ) dx \end{align*}

That is all. In summary, to solve for $$x=-\left \vert a\right \vert \cdots \left \vert b\right \vert$$, solve the problem for $$0\cdots L$$ and at the end, replace $$x$$ in the solution by replaced by $$\left ( x+\left \vert a\right \vert \right )$$