Why does making the 'wrong' $u$-substitution still work in this example? I was evaluating the integral
$$
\int 2x \cos(x^2)\,dx
$$
and realised that it could be written in the form
$$
\int f'(g(x))g'(x)\,dx
$$
and so substitution could be used to help evaluate it. Setting $u$ equal to $x^2$ (the inner function $g(x)$), we have
$$
du=2x\,dx
$$
and so
$$
\int 2x \cos(x^2)\,dx=\int \cos(u)\,du=\sin(u)+C=\sin(x^2)+C
$$
However, it appears that making the wrong substitution $u=2x$ still works, even though $u$ is not equal to the inner function $g(x)$:
$$
u=2x \implies du=2\,dx \\
\int 2x \cos(x^2)\,dx=\int\frac{u}{2}\cos\left(\frac{u^2}{4}\right)
$$
Admittedly, solving this is ugly. In fact, it requires two more substitutions, and so it was clearly a bad idea to set $u=2x$. However, the final answer did indeed come out as $\sin (x^2)+C$. My question is about why this works at all. If I understand correctly, substitution is only a valid method when $u=g(x)$, as this justifies the treating of $du$ and $dx$ as manipulable terms. We know that
$$
\int f'(u)\,du=f(u)+C=f(g(x))+C
$$
Therefore,
$$
\int f'(u)\,du = \int f'(g(x))g'(x)\,dx=\int f(g(x))+C
$$
meaning that $g'(x)\,dx$ can be 'replaced' by $du$. If $u\neq g(x)$, then this method seems to fall apart. Despite this, setting $u=2x$ actually did end up working. Was this just a coincidence?
 A: Note that in the link you provided, the calculator sets $v=u^2=4x^2$ and then $w=v/4=x^2$, i.e. effectively it has substituted for $x^2$ only albeit in a more circuitous manner.
A: Integration by substitution is most straightforwardly useful when you can pick $f,g$ such that the integrand is $f'(g(x)) g'(x)$. In this case, because of the chain rule for derivatives, you can just apply the FTC immediately and you're done.
However, it is possible that you can write the integral as, say, $h(g(x)) g'(x)$, but you don't immediately know how to integrate $h$ (and thus don't really know what $f$ should be). In this case you can still apply integration by substitution. This reduces the problem to integrating $h$. But there is no guarantee that this is easier than the original problem.
Thus the goal of integration by substitution basically boils down to choosing $h,g$ such that the integrand is $h(g(x)) g'(x)$ and $h$ is easier to integrate than the original integrand. It is still technically correct, just not useful, if $h$ is just as hard or harder to integrate than the original integrand.
As kind of an artificial example of my point, consider
$$\int \sin(x) e^{\sin(x)} \cos(x) \, dx.$$
Here we recognize the integrand is a function depending only on $\sin(x)$ times the derivative of $\sin(x)$, so we do integration by substitution and get to
$$\int u e^u \, du.$$
You might not immediately know what the antiderivative of $h(u)=ue^u$ is. But you still have an equality between the first integral and the second, so you can work from here, instead of having to work directly on the original problem.
A: I think I've managed to answer my own question. Actually, writing $u=2x$ does allow the integral to be written in the form $\int f'(g(x))g'(x)$, albeit in a much uglier way than before. Here is what we must set $f$ and $g$ to for the substitution to be valid:
\begin{align}
\text{Let }
f(x)&=\sin\left(\frac{x^2}{4}\right) \\
\implies f'(x)&= \cos\left(\frac{x^2}{4}\right)\frac{x}{2}\\
\text{Let }
g(x)&=2x \\
\implies g'(x)&=2 \\
\int f'(g(x))g'(x)dx&=f(g(x))=\cos(x^2)2x
\end{align}
So, often $u$ does equal $g(x)$ if we so choose, meaning that $g'(x)dx$ can be substituted by $du$. As Ian points out, it is not always clear what the antiderivative of $f'(x)$, but as along as we know that it can be integrated, there is no problem.
