Finite differences of function composition

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.

Federico

-
Hmm, why don't you just evaluate a derivative of the form $dy/dx$ by the central difference $(y_{i+1}-y_{i-1})/(2h)$? –  user1551 Aug 26 '11 at 16:02
@user1551: may I use the central difference for the outer differential and then a forward and a backward difference for the inner differentials? –  Federico Aug 26 '11 at 17:24

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.

-
thanks for you clear answer. What happens if I try to extend this result to a two dimensional diffusion problem? I will have at least $A_{i+1/2, j}$, $A_{i-1/2, j}$, $A_{i, j+1/2}$, $A_{i, j-1/2}$; is this going to work fine, or I'm forcing the model too much? –  Federico Aug 29 '11 at 0:22
That will work fine (as far as I can see). –  Jitse Niesen Aug 29 '11 at 0:43
If A is linear and u is quadratic, both our answers are exact. Since the data does not have half-grid points, we need to use $A_{i+1/2}=\frac{1}{2}(A_{i+1}+A_i)$ and $A_{i-1/2}=\frac{1}{2}(A_i+A_{i-1})$, and we get that\begin{align}A_{i+1/2}[u_{i+1}-u_i]-A_{i-1/2}[u_i-u_{i-1}]&=\frac{1}{4}(A_‌​{i+1}-A_{i-1})(u_{i+1}-u_{i-1}) + A_i(u_{i+1}-2u_i+u_{i-1})\\&+\frac{1}{4}(A_{i+1}-2A_i+A_{i-1})(u_{i+1}-2u_i+u_{i-‌​1})\end{align} The difference is the discrete analog of $\frac{1}{4}A_i''u_i''$. –  robjohn Aug 31 '11 at 0:51
If we write $A$ in Taylor series\begin{align}A_{i+1}&=A_i+A_i'+\frac{1}{2}A_i''\\A_{i-1}&=A_i-A_i'+\frac{‌​1}{2}A_i''\end{align} and similarly for $u_{i+1}$ and $u_{i-1}$, we get that $$A_{i+1/2}[u_{i+1}-u_i]-A_{i-1/2}[u_i-u_{i-1}]=A_i'u_i'+A_iu_i''+\frac{1}{4}A_i‌​''u_i''$$Whereas, $$\frac{1}{4}(A_{i+1}-A_{i-1})(u_{i+1}-u_{i-1}) + A_i(u_{i+1}-2u_i+u_{i-1})=A_i'u_i'+A_iu_i''$$ So it turns out that the formula I cited is exact if both $A$ and $u$ are quadratic. Extending this analysis further yields fewer higher order error terms as well. –  robjohn Aug 31 '11 at 0:51

$$\frac{\mathrm{d}}{\mathrm{d}x}\left[A\frac{\mathrm{d}u}{\mathrm{d}x}\right]=\frac{\mathrm{d}A}{\mathrm{d}x}\frac{\mathrm{d}u}{\mathrm{d}x}+A\frac{\mathrm{d}^2u}{\mathrm{d}x^2}$$ So you might try using $\frac{1}{4}(A_{i+1}-A_{i-1})(u_{i+1}-u_{i-1}) + A_i(u_{i+1}-2u_i+u_{i-1})$

-
I have a similar question to the one stated above. Please see if you can help find a solution math.stackexchange.com/questions/491097/… –  Gavin Wiggins Sep 11 '13 at 23:29