I have a set of data, $x, y$ and $ z$, each with length n:

$x \rightarrow \{x_{1}...x_{n}\}$

$y \rightarrow \{y_{1}...y_{n}\}$

$z \rightarrow \{z_{1}...z_{n}\}$

$y$ and $z$ are parameterised by $x$:

$y = y(x)$

$z = z(x)$

Problem 1

I wish to find derivatives $\Large \frac{dy}{dx}$ and $\Large \frac{dz}{dx}$.

The 'second-order error' numerical scheme: $\Large \frac{dy}{dx} = \frac{y(x+dx)-y(x-dx)}{2dx} + \mathcal{O}({dx}^{2})$

woud usually work, but in my case, my 'x' data is not uniformly spaced (it is nonlinear) i.e. '$dx$' is not constant (hence my problem turns into a first order error one). The problem applies for the derivative of z.

Can anyone suggest a good method to find these derivatives? (for anyone interested in meteorology, $x$ are pressure coordinates in atmosphere, $y$ is the potential temperature, $z$ is the geopotential height).

One person suggested that I interpolate $x$, $y$ and $z$ onto a new $x$ whereby this new list of $x$ values are uniformly spaced. However, I also want to see how the error introduced here propagates to the end result.

Problem 2

I wish to find the derivative: $\Large \frac{dy}{dz}$. However, as my $z$ are not necessarily spaced evenly between each other, my numerical scheme is not second order. Hence, what scheme should I use and what is my resulting error, preferably as a function of $x$. I guess it's a similar problem to above.


2 Answers 2


This is not the best numerical scheme, however, it should work well with second order accuracy.

Problem 1: you can apply the following second order approximation \begin{align} \left( \frac{dy}{ dx} \right)_i &= \frac{ y_{i+1} (\Delta x_{i} )^2 - y_{i-1} ( \Delta x_{i+1} )^2 + y_{i} \left( (\Delta x_{i+1})^2 - (\Delta x_{i})^2 \right) }{ \Delta x_{i} \Delta x_{i+1} \left( \Delta x_{i} + \Delta x_{i+1} \right) } ~~+ \\ &~~~~ - \frac{\Delta x_{i+1} \Delta x_{i} }{ 6} \left( \frac{d^3 y }{ dx ^3} \right)_i + \textrm{Higher-Order-Terms} \\ &\simeq \frac{ y_{i+1} (\Delta x_{i} )^2 - y_{i-1} ( \Delta x_{i+1} )^2 + y_{i} \left( (\Delta x_{i+1})^2 - (\Delta x_{i})^2 \right) }{ \Delta x_{i} \Delta x_{i+1} \left( \Delta x_{i} + \Delta x_{i+1} \right) } \end{align} for $2 \le i \le n-1$, where $\Delta x_{i} = x_{i} - x_{i-1}$. For the end-points, i.e. $i=1$ and $i=n$, one can adopt the one-side approximations \begin{align} \left( \frac{dy}{ dx} \right)_1 &\simeq \frac{ - y_{3} ( x_2 -x_1 )^2 + y_{2} ( x_3 - x_1 )^2 - y_{1} \left( (x_3 -x_1)^2 - (x_2 - x_1 )^2 \right) }{ (x_2-x_1)(x_3-x_1)(x_3-x_2) }, \\ \left( \frac{dy}{ dx} \right)_n &\simeq \frac{ y_{n-2} ( x_n -x_{n-1} )^2 - y_{n-1} ( x_{n} - x_{n-2} )^2 + y_{n} \left( ( x_{n} - x_{n-2} )^2 - ( x_{n} - x_{n-1} )^2 \right) }{ ( x_n -x_{n-1} ) ( x_n -x_{n-2} ) ( x_{n-1} -x_{n-2} ) } , \end{align} which also have second order accuracy on any grid. For further reading, you can read the excellent book of Ferziger and Peric, Chapter 3.

Regarding Problem 2, you can apply the idea from Wolfgang Bangerth's post, i.e. $$ \left( \frac{ d y }{ d z } \right)_i = \frac{ \left( \frac{dy }{dx} \right)_i }{ \left( \frac{ dz}{ dx} \right)_i } $$ for any $i=1,2,..,n$.


For Problem 1: You could use the forward-looking one-sided finite difference scheme, $$ \frac{dy(x_k)}{dx} \approx \frac{y_{k+1}-y_k}{x_{k+1}-x_k}. $$ Of course, you could equally well have used the backward looking scheme: $$ \frac{dy(x_k)}{dx} \approx \frac{y_k-y_{k-1}}{x_k-x_{k-1}}. $$ To make it more accurate, you can also average them: $$ \frac{dy(x_k)}{dx} \approx \frac 12 \frac{y_{k+1}-y_k}{x_{k+1}-x_k} + \frac 12 \frac{y_k-y_{k-1}}{x_k-x_{k-1}}. $$

For Problem 2: Use the chain rule: $$ \frac{dz}{dy}=\frac{\frac{dz}{dx}}{\frac{dy}{dx}}. $$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.