How to know when one has license to define a new function to assist in completing a solution. - Example from Spivak Calculus - Ch 14 Using question question: 14-9 from Spivak's Calculus. The chapter revolves around the ideas of the Fundamental Thoerem of Calculus. I have a question about how do you know if/when you have license to "define" another function to assist in trying to create a solution ? The following screenshot is of the question I was attempting to solve:
$$ f(x) = \begin{cases} \cos\frac{1}{x},\ &x\ne 0\\0,&x=0 \end{cases} $$
Is $F(x)=\int_0^x f\,$ differentiable at $x=0$?
The solution is as follows:
If
$$g(x) = \begin{cases} x^2\sin\frac{1}{x},\ &x\ne 0\\0,&x=0 \end{cases}$$
then
$$g'(x) = \begin{cases} 2x\sin\frac{1}{x}-\cos\frac{1}{x},\ &x\ne 0\\0,&x=0 \end{cases}$$
So if we define
$$h(x) = \begin{cases} 2x\sin\frac{1}{x},\ &x\ne 0\\0,&x=0 \end{cases}$$
We have
$$f(x)=h(x)-g'(x)\quad\textrm{for all }x.$$
Hence
\begin{align}F(x) = & \, \int_0^x (h-g') \\
= & \, \left(\int_0^x h\right) -g, \end{align}
using the second Fundamental Theory of Calculus (and not merely the Corollary of the First Fundamental Theorem). Since $h$ is continuous we may then apply the First Fundamental Theorem to conclude that,
\begin{align}F(0) = & \, h(0)-g'(0) \\
= & \, 0. \end{align}
So let me take you through my thought process first before I get into specifics. After looking at the hint, seeing that the original question was asking about $f(x) = \cos(\frac{1}{x})$, I deduced that since  $\cos(\frac{1}{x})$ is not continuous at $0$, then the function $f(x)$ is not continuous at $0$, which would imply that FTC could not be applied and $F(x)$ is not differentiable.
Here is where my question comes up. As you can see in the solution, Spivak defined a new function $h(x)$ and used this to illustrate that $F(x)$ is indeed differentiable at $0$.
I understand the solution to this question in particular and the logic, but it is how/why/when can one "define" a new function and use it in this way and similar ways ? I ask because this sort of thing keeps on popping up when I'm working through exercises regardless of the math subject and it throws me off. I'm not sure there is a succinct answer to this, but perhaps some sort of guidance on how to look out for these things in the future ?
 A: I'm not sure I like Spivak's solution as it's a bit of rabbit from a hat. It seems to me there is intuition here that's being overlooked: We have
$$F(x) = \int_0^x\cos(1/t)\,dt.$$
Does $F'(0)$ exist? If it does it would appear that $F'(0)=0,$ simply because the crazy oscillation of $\cos(1/t)$ looks pretty evenly balanced between plus and minus.  So maybe
$$\frac{F(x)-F(0)}{x-0}= \frac{F(x)}{x}\to 0.$$
What I would do is make the change of variables $t=1/y.$ Why do this? For one thing it turns $\cos(1/t)$ into $\cos (y).$ Doing that, we get
$$\frac{F(x)}{x} = \frac{1}{x}\int_{1/x}^\infty \frac{\cos(y)}{y^2}\,dy.$$
Integrate by parts ($u=y^{-2}, dv = \cos(y)\,dy$) and everything works out. In fact we find $F(x)/x=O(x).$
A: Regarding the issue raised in the comments, yes, $f$ is integrable (in the Riemann/Darboux sense) on every compact interval in $\Bbb{R}$ (i.e for every $a<b$, $f$ is integrable on $[a,b]$). This is because $f$ is continuous everywhere except for $0$ (as the OP should take for granted until the next chapter which proves basic properties of trig functions), and $f$ is bounded (in fact, if $f:[a,b]\to \Bbb{R}$ is bounded and continuous except for a finite set of points, then it is still integrable). Therefore, the function $F(x) = \int_0^x f$ is well-defined.
Now, you say

I deduced that since  $\cos(\frac{1}{x})$ is not continuous at $0$, then the function $f(x)$ is not continuous at $0$

well, a small technical detail: $\cos(1/x)$ is not even defined at $0$ so it doesn't make sense to speak of it being continuous at $0$. What you probably should have said is that "since $\lim\limits_{x\to 0}\cos\left(\frac{1}{x}\right)$ does not exist..." Next, you continue

which would imply that FTC could not be applied and $F(x)$ is not differentiable.

Again, there's a mistake in your logic. You're right that since $f$ is not continuous at $0$, you cannot apply the FTC as stated in Spivak's text, simply because the hypotheses of the theorem have not been satisfies. BUT, this is no way implies that $F$ is not differentiable at $0$. All this says is that you have to be more cautious before making any conclusions.
Finally, you ask

but it is how/why/when can one "define" a new function and use it in this way and similar ways ?

To answer "how can one know to define such a function", the answer is just "practice and keep reading". Sometimes, you'll learn cool new tricks just by doing more math. As for "why" define a new function, it's because "it works". As for "when can one define...", the answer is you can define anything you want whenever you want (as long as you're doing things logically and not introducing any contradictions). The difficult part is in knowing whether such a definition is useful; this is something you only learn with practice.
At first glance, this solution of Spivak's does seem pretty random, but if you analyze it carefully, all he's done is take the guess-work out of integration by parts. For $x\neq 0$, we have (by multiplying and dividing by the derivative of $1/x$)
\begin{align}
\cos\left(\frac{1}{x}\right) &= \left(\dfrac{-1}{x^2}\right)\cos\left(\frac{1}{x}\right) \cdot (-x^2) \\
&= \dfrac{d}{dx}\left(\sin\left(\frac{1}{x}\right)\right) \cdot (-x^2) \\
&= \dfrac{d}{dx}\left(\sin\left(\frac{1}{x}\right) \cdot (-x^2)\right) - \sin\left(\frac{1}{x}\right)\cdot \dfrac{d}{dx}(-x^2) \tag{$*$} \\
&= 2x \cdot \sin\left(\frac{1}{x}\right) - \dfrac{d}{dx}\left(x^2 \sin\left(\frac{1}{x}\right)\right)
\end{align}
Step $(*)$ is exactly what integration by parts is based on (and trust me, this is one of the integration "tricks" which is pretty common... there are some integration tricks which are so much more "out of thin air"); namely the product rule in reverse.
So, really, all Spivak has done is a "clever" undoing of the chain and product rule; i.e "integration by parts" without really saying so (because he hasn't introduced this concept yet), and then he has concisely written up all these functions carefully, by defining them at $x=0$, and then he has invoked the theorems proven up to this point.
