What is the problem in taking the derivative of a function with respect to a non-monotonic function? I was going through the accepted solution here Danger Zone for Aircraft. And this is what caught my attention:

So let's continue with the reasoning. We need to find the furthest possible distance x that can be reached at height h. The only variable is θ. This means that if we take the derivative of x with respect to θ, the stationary point (zero slope) corresponds to the furthest distance (you have to check that it's not the closest distance, of course). This is what the answer in the book did. But (and this is the clever bit), recognizing that tanθ is a monotonic function over the range of values of interest <−π/2,π/2]>, one can equally decide to take a derivative with respect to tanθ. Usually this would be done formally by substitution of variables; the book's solution takes a short cut.

To actually see the consequences of differentiating with respect to a non-monotonic function I took two examples. Consider $y=tan^2(x)+7tan(x)-8$. On taking its derivative with respect to $tan(x)$ and setting it equal to 0 we get $tan(x) = -3.5$ the solutions of which are the points of minima of y. Now consider $y=sin^2(x)+7sin(x)-8$. Now taking its derivative with respect to $sin(x)$ and setting it equal to 0 we get $sin(x) = -3.5$ which is impossible and basically means that there are no stationary points, but in fact there are infinitely many.
So the solution says that its alright to take the derivative with respect to a monotonic function, and even the examples agree with that. But why? What is the reason that taking the derivative with respect to a non-monotonic function leads to erroneous answers? And are there any other conditions that the function (with respect to which we want to differentiate another function) must satisfy?
 A: The problem has to do with the chain rule.
Suppose what you want to do is maximize or minimize a quantity $f(x)$, which is a function of $x$, over some interval containing $x$, but you are able to express this function in the form $f(x) = g(h(x)).$
So you have
$$ f'(x) = g'(h(x)) h'(x) $$
and you want to find the zeros of $f'(x).$ If $h$ is function with a strictly positive (or strictly negative) derivative, then you can conclude that $f'(x)$ is zero only when $g'(h(x))$ is zero.
So if you look for the zeros of $g'(y)$, set $y = h(x)$ in each case, and evaluate $x$, you will find all the $x$ values at which $f'(x) = 0.$
Of course while you are doing this you have to throw out any $y$ that cannot be expressed in the form $y = h(x)$ for some $x$ in the appropriate interval.
Now let's consider what happens if $h'(x)$ is not strictly positive or strictly negative. That is, what happens if $h'(x)$ is sometimes zero.
If there are values of $x$ in the appropriate interval such that $h'(x) = 0$, then it will happen that $f'(x) = 0$ at the same values of $x$, although it will not generally be true that $g'(h(x)) = 0$ at the same time.
In short, if you look only for cases where $g'(h(x)) = 0$,
you are liable to miss the cases where $f'(x) = 0$ because $h'(x) = 0.$
That is exactly what happens with the function $f(x) = \sin^2(x)+7\sin(x)-8.$
You have $g(y) = y^2 + 7y - 8$ and $h(x) = \sin(x)$,
so $g'(y) = 2y + 7$ and
$$ f'(x) = g'(h(x)) h'(x) = (2\sin(x) + 7) \cos(x). $$
Now it should be clear that $f'(x) = 0$ only if $2\sin(x) + 7 = 0$ (which cannot happen) or if $\cos(x) = 0,$ which happens at $x = \frac\pi2 + n\pi$ for any integer $n$. And indeed the zeros of $f'(x)$ (and the local minima and maxima of $f(x)$, which turn out to be global minima and maxima in the case of this particular function) occur at $x = \frac\pi2 + n\pi$ for any integer $n$.
In particular we get maxima at $x = \frac\pi2 + 2k\pi$ for any integer $k$.
So that's how you missed infinitely many stationary points while taking the derivative of $\sin^2(x)+7\sin(x)-8$ with respect to $\sin(x).$
All the points you missed were points at which
$\frac{\mathrm d}{\mathrm dx}\sin(x) = 0.$
Now what about a function $h(x)$ that is monotonic, let's say monotonically increasing, but where sometimes $h'(x) = 0$?
An example is the function $h(x) = x^3.$
I'll leave this as an exercise. Ask yourself this: what do you need to know about what $g(y)$ is doing for $y = h(x)$ at and around the values of $x$ where $h'(x) = 0$?
