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.
By Taylor expansion, for $h_1 \neq h_2$ we have
$$a y(x+h_1) + by(x+h_2) =a(y(x)+y'(x)h_1 +y''(x)h_1^2/2+o(h_1^2)) +b(y(x)+y'(x)h_2 +y''(x)h_2^2/2+o(h_2^2)).$$
To make this approximate $y'(x)$, you want to eliminate the zeroth order terms and make the net coefficient on $y'(x)$ be $1$. This means
$$a+b=0 \\ ah_1 + b h_2=1.$$
The solution to this is $a=-\frac{1}{h_2-h_1},b=\frac{1}{h_2-h_1}$.
Unfortunately, the net second order term is now
$$\frac{1}{h_2-h_1}(y''(x)h_2^2-y''(x)h_1^2)$$
which does not vanish unless $h_1=-h_2$ (in which case you recover the familiar centered difference scheme).
You can do similar techniques to come up with other schemes for approximating derivatives on irregular grids.