What are Numerical Methods?
& "G:\My Drive\Teaching\framework\convert.bat" "G:\My Drive\Teaching\teaching-notes\heat-transfer\numerical_methods.md"
This is a continuous function, i.e. its value and gradient can be calculated for any value of \(x\). To solve using the forward finite difference method, a fixed number of discrete equations need to be created, i.e. the values and gradients will only be calculated for these specific values of \(x\).
The points where the blocks meet are called nodes. In a numerical method, information is only known at the nodes. Nothing is known about what happens in between. In this example, the nodes will exist at (and only at) \(x=2, 3, 4, 5, 6, 7\).
In a numerical method, information is only known at the nodes. Nothing is known about what happens in between.
The term "discriisation"
Memory of computer - can't have infitie
Finite Difference
To determine the gradient of a function \(f(x)\) at a given value of \(x\), differentiation is used. One of the most common ways of explaining differentiation is to consider the change in the value of \(f(x)\) over a small distance \(\Delta x\). The gradient is approximated by dividing the change in \(f(x)\) by \(\Delta x\). As the size of \(\Delta x\) approaches zero, the approximation error reduces and the gradient approaches the true value.
\[ \frac{\mathrm{d}f}{\mathrm{d}x}= \lim_{\Delta x \to 0}\frac{f(x+\Delta x)-f(x)}{\Delta x} \]
In the forward finite difference method, \(\Delta x\) is not reduced to zero and remains at a finite size. It is accepted that there will be an error between the true gradient \((\frac{\mathrm{d}f}{\mathrm{d}x})\) and the approximation \(\frac{\Delta f}{\Delta x}\).
\[ \frac{\mathrm{d}f}{\mathrm{d}x} = \frac{\Delta f}{\Delta x} + \underbrace{\varepsilon}_{\text{error}} \]
\[ \frac{\Delta f}{\Delta x}=\frac{f(x+\Delta x)-f(x)}{\Delta x} \qquad ; \qquad \varepsilon = O(\Delta x) \]
The error will have a "first-order" relationship to the size of the finite difference \(\Delta x\). As the size of the distance \(\Delta x\) reduces, the approximation of the gradient approaches the true value.
Example: Forward Finite difference of a simple function
Let's apply the forward finite difference method to the simple function \(f(x)=x^2\). This is a useful function to start with because we already know the answer. Differentiating gives \(\frac{\mathrm{d}f}{\mathrm{d}x}=2x\). To solve this differential equation numerically and determine the value of \(f(x)\) at the specific values of \(x\) where nodes exist, we need to create one discrete equation at each node.
In this example, the section of the \(x\) axis between \(x=2\) and \(x=7\) is split into blocks of width \(\Delta x = 1\). In this example, the nodes will exist at (and only at) \(x=2, 3, 4, 5, 6, 7\).
To start the forward finite difference process, the first value (at \(x=2\)) is needed. For this node, we will use the analytically calculated value of \(f(x=2)\) as \(x^2=2^2=4\).
To determine the value at \(x=3\), we will apply the forward finite difference method. We know the value of \(f(x=2)\), the rate at which \(f\) is increasing with respect to \(x\) at \(x=2\), given by \(\frac{\mathrm{d}f}{\mathrm{d}x}\), and the distance \(\Delta x\) between \(x=2\) and \(x=3\).
\[ \left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x=2}=2x=2\times 2=4 \]
\[ f(x=3) \approx f(x=2)+\left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x=2}\Delta x = 4 + 4\times 1 = 8 \]
Of course, we know that \(3^2=9\), so after one step the approximation is out by 1. This error comes from using the gradient at \(x=2\) across the whole step, when the true gradient increases along it.
The process is repeated to find the value of \(f(x=4)\). We calculate the gradient at \(x=3\) and use it, with our approximate value \(f(x=3) \approx 8\), to step forward to \(x=4\).
\[ \left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x=3}=2x=2\times 3=6 \]
\[ f(x=4) \approx f(x=3)+\left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x=3}\Delta x = 8 + 6\times 1 = 14 \]
However, \(4^2=16\), so the approximation is now out by 2. The errors from each step accumulate as we march along the nodes: an error of 1 has been introduced in this step, and added to the error of 1 carried over from the previous step.
The exact same process can be applied to all subsequent steps where \(x=5\), \(6\) and \(7\).
| \(x\) | Gradient used, \(\left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x-\Delta x}\) | \(f(x-\Delta x)+\left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x-\Delta x}\Delta x\) | \(f(x)\) | Error, \(x^2 - f(x)\) |
|---|---|---|---|---|
| 2 | — | — | 4 (calculated value) | 0 |
| 3 | \(2\times 2=4\) | \(4 + 4\times 1\) | 8 | 1 |
| 4 | \(2\times 3=6\) | \(8 + 6\times 1\) | 14 | 2 |
| 5 | \(2\times 4=8\) | \(14 + 8\times 1\) | 22 | 3 |
| 6 | \(2\times 5=10\) | \(22 + 10\times 1\) | 32 | 4 |
| 7 | \(2\times 6=12\) | \(32 + 12\times 1\) | 44 | 5 |
Errors due to discretising a continuous equation are an unavoidable artefact of numerical methods. In general, errors cannot be eliminated, but they can be reduced. In this simple case, reducing the distance between nodes produces a result with smaller errors. In the example below, the distance between \(x=2\) and \(x=3\) is split into 10 steps of \(\Delta x = 0.1\), giving 11 nodes from \(x=2.0\) to \(x=3.0\).
| \(x\) | Gradient used, \(\left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x-\Delta x}\) | \(f(x-\Delta x)+\left.\frac{\mathrm{d}f}{\mathrm{d}x}\right\rvert_{x-\Delta x}\Delta x\) | \(f(x)\) | Error, \(x^2 - f(x)\) |
|---|---|---|---|---|
| 2.0 | — | — | 4.00 (calculated value) | 0.00 |
| 2.1 | \(2\times 2.0=4.0\) | \(4.00 + 4.0\times 0.1\) | 4.40 | 0.01 |
| 2.2 | \(2\times 2.1=4.2\) | \(4.40 + 4.2\times 0.1\) | 4.82 | 0.02 |
| 2.3 | \(2\times 2.2=4.4\) | \(4.82 + 4.4\times 0.1\) | 5.26 | 0.03 |
| 2.4 | \(2\times 2.3=4.6\) | \(5.26 + 4.6\times 0.1\) | 5.72 | 0.04 |
| 2.5 | \(2\times 2.4=4.8\) | \(5.72 + 4.8\times 0.1\) | 6.20 | 0.05 |
| 2.6 | \(2\times 2.5=5.0\) | \(6.20 + 5.0\times 0.1\) | 6.70 | 0.06 |
| 2.7 | \(2\times 2.6=5.2\) | \(6.70 + 5.2\times 0.1\) | 7.22 | 0.07 |
| 2.8 | \(2\times 2.7=5.4\) | \(7.22 + 5.4\times 0.1\) | 7.76 | 0.08 |
| 2.9 | \(2\times 2.8=5.6\) | \(7.76 + 5.6\times 0.1\) | 8.32 | 0.09 |
| 3.0 | \(2\times 2.9=5.8\) | \(8.32 + 5.8\times 0.1\) | 8.90 | 0.10 |
At \(x=3\) the error is now 0.10, compared with 1 when \(\Delta x = 1\). Making \(\Delta x\) ten times smaller has made the error ten times smaller, at the cost of ten times as many steps.
Finite difference form of the 1D, steady conduction equation (no heat generation) with regular node spacing
For 1D conduction through a material with constant properties and a constant cross-sectional area, the conduction equation is:
\[ \left[\frac{\partial^2 T}{\partial x^2}\right] + \frac{\dot e_{\mathrm{gen}}}{k} = \frac{1}{\alpha}\frac{\partial T}{\partial t} \]
Here, \(k\) is the thermal conductivity, \(\alpha\) is the thermal diffusivity and \(\dot e_{\mathrm{gen}}\) is the heat generation rate per unit volume.
To demonstrate the principle, we will start with steady conduction without heat generation. The time derivative and heat generation term are both zero, so the equation reduces to:
\[ \frac{\mathrm{d}^2 T}{\mathrm{d} x^2} = 0 \]
We will later extend this approach to more general cases by relaxing these assumptions.
The continuous conduction equation needs to be discretised into a series of algebraic equations using the finite difference approach. Consider a node \(m\), with temperature \(T_m\). The node to the left is \(m-1\), with temperature \(T_{m-1}\), and the node to the right is \(m+1\), with temperature \(T_{m+1}\). The nodes are equally spaced, a distance \(\Delta x\) apart. We will form an equation relating these three temperatures.
The second derivative, \(\frac{\mathrm{d}^2 T}{\mathrm{d} x^2}\), is the rate of change of the temperature gradient, \(\frac{\mathrm{d} T}{\mathrm{d} x}\). At node \(m\), we approximate it by taking the difference between the gradients at the two half-points and dividing by the distance \(\Delta x\) between them:
\[ \left.\frac{\mathrm{d}^2 T}{\mathrm{d} x^2}\right\rvert_m \approx \frac{\left.\frac{\mathrm{d} T}{\mathrm{d} x}\right\rvert_{m+\frac{1}{2}}-\left.\frac{\mathrm{d} T}{\mathrm{d} x}\right\rvert_{m-\frac{1}{2}}}{\Delta x} \]
We then approximate each half-point gradient using the temperatures at its two neighbouring nodes. Substituting these approximations into the conduction equation gives the finite difference equation:
\[ \frac{ \frac{T_{m+1}-T_m}{\Delta x} -\frac{T_m-T_{m-1}}{\Delta x} } {\Delta x}=0 \]
Combining the terms gives:
\[ \frac{T_{m+1}-2T_m+T_{m-1}}{\Delta x^2}=0 \]
Multiplying both sides by \(\Delta x^2\) gives:
\[ T_{m+1}-2T_m+T_{m-1}=0 \]
The approximation is in replacing the derivatives with finite differences. Once this replacement is made, the resulting algebraic equation is set equal to zero.
To solve for the temperature along a length \(L\), a grid of \(M\) equally spaced nodes is placed along it, including one at each end. These nodes are numbered from \(0\) to \(M-1\), so the distance between neighbouring nodes is \(\Delta x = L/(M-1)\). The equation above applies at every interior node, from \(m=1\) to \(m=M-2\). The two end nodes, \(m=0\) and \(m=M-1\), each have only one neighbour, so they need boundary conditions instead.
This equation applies at each interior node, \(m=1,\ldots,M-2\). It means that each interior temperature is the average of its two neighbouring temperatures. Boundary conditions provide the equations for the two end nodes, allowing the temperatures throughout the length to be determined.
Finite difference form of the 1D, steady conduction equation (inc heat generation) with regular node spacing
The finite difference method can be applied to the general conduction equation:
\[ \left[\frac{\partial^2 T}{\partial x^2}\right] + \frac{\dot e_{\mathrm{gen}}}{k} = \frac{1}{\alpha}\frac{\partial T}{\partial t} \]
To demonstrate the principle, the 1D, steady form of the conduction equation will be used.
\[ \frac{\mathrm{d}^2 T}{\mathrm{d} x^2} + \frac{\dot e_{\mathrm{gen}}}{k} = 0 \]
The continuous form of the conduction equation needs to be discretised into a series of algebraic equations using the finite difference approach, with one equation applied at each node. Consider a 1D length \(L\) split into \(M\) equally spaced nodes. These nodes are numbered from \(0\) to \(M-1\) (for example, six nodes are numbered \(m=0\) to \(m=5\)), so the distance between neighbouring nodes is \(\Delta x = L/(M-1)\). At a given node \(m\), the node to its left is \(m-1\) and the node to its right is \(m+1\). The aim is to calculate the temperature \(T_m\) at every node.
As the \(\frac{\mathrm{d}^2 T}{\mathrm{d} x^2}\) term is the second derivative, it is treated as \(\frac{\mathrm{d}}{\mathrm{d} x}\left(\frac{\mathrm{d} T}{\mathrm{d} x}\right)\). The rate of change of the derivative of the function becomes
\[ \frac{\left.\frac{\mathrm{d} T}{\mathrm{d} x}\right\rvert_{m+\frac{1}{2}}-\left.\frac{\mathrm{d} T}{\mathrm{d} x}\right\rvert_{m-\frac{1}{2}}}{\Delta x}+ \frac{\dot e_{\mathrm{gen}}}{k}\approx 0 \]
We can then apply the finite difference approach to the \(\frac{\mathrm{d} T}{\mathrm{d} x}\) terms, to get...
\[ \frac{ \frac{T_{m+1}-T_{m}}{\Delta x} -\frac{T_{m}-T_{m-1}}{\Delta x} } {\Delta x}+ \frac{\dot e_{\mathrm{gen}}}{k}\approx 0 \]
\[ \frac{ {T_{m+1}-2T_{m}+T_{m-1}} } {\Delta x^2}+ \frac{\dot e_{\mathrm{gen}}}{k}\approx 0 \]
For a constant conductivity \(k\) and node spacing \(\Delta x\), it is possible to multiply both sides of the equation by \(\Delta x^2\) to obtain the form
\[ \left[T_{m+1}-2T_{m}+T_{m-1}\right]+ \frac{\dot e_{\mathrm{gen}}\Delta x^2}{k}\approx 0 \]
DOnw to here****************
Finite diffence of 1D, steady conduction equation with variable node spacing
As the \(\frac{\mathrm{d}^2 T}{\mathrm{d} x^2}\) term is the second derivative, it is treated as \(\frac{\mathrm{d}}{\mathrm{d} x}\left(\frac{\mathrm{d} T}{\mathrm{d} x}\right)\). The rate of change of the derivative of the fucntion becomes
\[ \frac{\frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m+\frac{1}{2}}-\frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m-\frac{1}{2}}}{\frac{1}{2}\Delta x_m^++\frac{1}{2}\Delta x_m^-}+ \frac{\dot e_{\mathrm{gen}}}{k}=0 \]
This is to say that the rate of change of \(\frac{\mathrm{d} T}{\mathrm{d} x}\) with respect to \(x\) is the differnce between \(\frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m+\frac{1}{2}}\) and \(\frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m-\frac{1}{2}}\) over distance \({\frac{1}{2}\Delta x_m^++\frac{1}{2}\Delta x_m^-}\)
The \(\frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m+\frac{1}{2}}\) and \(\frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m+\frac{1}{2}}\) terms can be replaced with thier finite differnce.
\[ \frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m+\frac{1}{2}}=\frac{T_{m+1}-T_{m}}{\Delta x_m^+} \qquad ; \qquad \frac{\mathrm{d} T}{\mathrm{d} x}\rvert_{m-\frac{1}{2}}=\frac{T_{m}-T_{m-1}}{\Delta x_m^-} \]
\[ \left.\frac{\mathrm{d}^2T}{\mathrm{d}x^2}\right|_m \approx \frac{T_{m+1} - 2T_m + T_{m-1}}{\Delta x^2} \]