I'm trying to express the following in finite differences:
$$\frac{d}{dx}\left[ A(x)\frac{d\, u(x)}{dx} \right].$$
Let $h$ be the step size and $x_{i-1} = x_i - h$ and $x_{i+ 1} = x_i + h$
If I take centered differences evaluated in $x_i$, I get:
$\begin{align*}\left\{\frac{d}{dx}\left[ A(x)\frac{d\, u(x)}{dx}\right]\right\}_i &= \frac{\left[A(x)\frac{d\, u(x)}{dx}\right]_{i+1/2} - \left[A(x)\frac{d\, u(x)}{dx}\right]_{i-1/2}}{h} \\ &= \frac{A_{i+1/2}\left[\frac{u_{i+1}-u_{i}}{h}\right] - A_{i-1/2}\left[\frac{u_{i}-u_{i-1}}{h}\right]}{h} \end{align*}$
So, if I use centered differences I would have to have values for $A$ at $i + \frac 12$ and $A$ at $i - \frac 12$; however those nodes don't exist (in my stencil I only have $i \pm$ integer nodes); is that correct? If I use forward or backward differences I need A values at $i$, $i + 1$, $i + 2$ and at $i$, $i -1$, $i -2$ respectively.
Am I on the correct path?
I would really appreciate any hint.
Thanks in advance,
Federico
While the approach of robjohn is certainly possible, it is often better to take the approach suggested by the original poster: $$ \left\{\frac{d}{dx}\left[ A(x)\frac{d\, u(x)}{dx}\right]\right\}_i = \frac{A_{i+1/2}\left[\frac{u_{i+1}-u_{i}}{h}\right] - A_{i-1/2}\left[\frac{u_{i}-u_{i-1}}{h}\right]}{h} $$ As he noted, $A$ is evaluated on half grid point. In many cases, that's not much of a problem. For instance, if you start from the diffusion equation with variable diffusivity, $$ \frac{\partial u}{\partial t} = \frac{\partial}{\partial x} \left( A(x) \frac{\partial u}{\partial x} \right), $$ with $A(x)$ given, then it does not matter whether you sample $A$ on grid points or half grid points. It is quite okay to use a different grid for $A$ and $u$ (that's sometimes called a staggered grid).
In other cases, you can approximate the values at half grid points by the average: $A_{i+1/2} \approx \frac12 ( A_{i+1} + A_i )$. That gives you almost the same method as the one derived by robjohn.