# nth order differentiation challenge

#### MarkFL

Staff member
Let:

$$\displaystyle f(x)=x\sin(x)$$

Derive a formula for:

$$\displaystyle f^{(n)}(x)$$

Using this, infer a formula for:

$$\displaystyle \frac{d^n}{dx^n}\left(x\cos(x) \right)$$

edit: I wanted to make sure it is clearly understood that:

$$\displaystyle f^{(n)}(x)\equiv\frac{d^n}{dx^n}\left(f(x) \right)$$

#### Ackbach

##### Indicium Physicus
Staff member
Using the Leibnitz rule for the $n$-th derivative of a product, which in general is
$$\frac{d^{n}}{dx^{n}}[f(x)\,g(x)]= \sum_{j=0}^{n} \left[{n \choose j} \frac{d^{j}f(x)}{dx^{j}} \frac{d^{n-j}g(x)}{dx^{n-j}}\right],$$
we have that
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= \sum_{j=0}^{n} \left[{n \choose j} \frac{d^{j}x}{dx^{j}} \frac{d^{n-j} \sin(x)}{dx^{n-j}} \right].$$
Now, there are only two non-zero terms: $j=0$ and $j=1$. Hence, the sum collapses down to those two terms:
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= {n \choose 0} x \frac{d^{n} \sin(x)}{dx^{n}}+{n \choose 1} \frac{d^{n-1} \sin(x)}{dx^{n-1}}.$$
Note that
$${n \choose 0}=1,\quad \text{and} \quad {n \choose 1}=n,$$
so we further simplify as
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= x \frac{d^{n} \sin(x)}{dx^{n}}+n \frac{d^{n-1} \sin(x)}{dx^{n-1}}.$$
To finish, we can use the fact that
$$\frac{d^{n}}{dx^{n}} \sin(x)= \sin \left( \frac{n \pi}{2}+x \right),$$
to write the derivative as
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= x \sin \left( \frac{n \pi}{2}+x \right)+n \sin \left( \frac{(n-1) \pi}{2}+x \right).$$

The same formula happens to work for $\cos$, so simply replace $\sin$ with $\cos$ to get the equivalent formula for $(d^{n}/dx^{n})(x \cos(x))$.

#### MarkFL

Staff member
Using the Leibnitz rule for the $n$-th derivative of a product, which in general is
$$\frac{d^{n}}{dx^{n}}[f(x)\,g(x)]= \sum_{j=0}^{n} \left[{n \choose j} \frac{d^{j}f(x)}{dx^{j}} \frac{d^{n-j}g(x)}{dx^{n-j}}\right],$$
we have that
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= \sum_{j=0}^{n} \left[{n \choose j} \frac{d^{j}x}{dx^{j}} \frac{d^{n-j} \sin(x)}{dx^{n-j}} \right].$$
Now, there are only two non-zero terms: $j=0$ and $j=1$. Hence, the sum collapses down to those two terms:
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= {n \choose 0} x \frac{d^{n} \sin(x)}{dx^{n}}+{n \choose 1} \frac{d^{n-1} \sin(x)}{dx^{n-1}}.$$
Note that
$${n \choose 0}=1,\quad \text{and} \quad {n \choose 1}=n,$$
so we further simplify as
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= x \frac{d^{n} \sin(x)}{dx^{n}}+n \frac{d^{n-1} \sin(x)}{dx^{n-1}}.$$
To finish, we can use the fact that
$$\frac{d^{n}}{dx^{n}} \sin(x)= \sin \left( \frac{n \pi}{2}+x \right),$$
to write the derivative as
$$\frac{d^{n}}{dx^{n}}[x \, \sin(x)]= x \sin \left( \frac{n \pi}{2}+x \right)+n \sin \left( \frac{(n-1) \pi}{2}+x \right).$$

The same formula happens to work for $\cos$, so simply replace $\sin$ with $\cos$ to get the equivalent formula for $(d^{n}/dx^{n})(x \cos(x))$.
Well done, Adrian. I recall with fondness "discovering" and proving by induction the formula you used up front when fiddling with reduction of order techniques in my ODE course.

I will give others time before posting my own solution. #### Fernando Revilla

##### Well-known member
MHB Math Helper
Another way:

Consider $h:\mathbf{R}\to \mathbf{C},\;\; h(x)=x\cos x+ix\sin x=xe^{ix}$. Then, $$\frac{d^n}{dx^n}(x\cos x)=\text{Re }\left(h^{(n)}(x)\right)\;,\quad \frac{d^n}{dx^n}(x\sin x)=\text{Im }\left(h^{(n)}(x)\right)$$

#### Ackbach

##### Indicium Physicus
Staff member
Well done, Adrian. I recall with fondness "discovering" and proving by induction the formula you used up front when fiddling with reduction of order techniques in my ODE course.

I will give others time before posting my own solution. And I remember taking a graduate-level physics course where I was the only mathematical physics guy there, and being asked to put a proof of this formula on the board. The prof asked me to include every step, including the formula for the entry in Pascal's Triangle as the sum of the two numbers directly above it. (The prof seemed to think that the rest of the students couldn't get this on their own - I suppose maybe because he had seen their homework!) I remember thinking to myself that this really wasn't that big a deal. The other students should have been able to do it. Indeed, their physics abilities were far beyond my own. But in the words of the prof, "They don't know how to differentiate!"

My consolation is that all theoretical physicists these days are mathematicians - they have to be, because the math people have gotten so far behind the physics people, that whenever the physicists look around for a needed tool, it's no longer there like it was, say, even 70 years ago.

#### anemone

##### MHB POTW Director
Staff member
My approach:

We're given $$\displaystyle f(x)=x\sin(x)$$

I first find the first few nth derivative of the function of $$\displaystyle f(x)=x\sin(x)$$ to see if there is any pattern to be observed...

$$\displaystyle f^{(1)}(x)=x\cos x +\sin x$$
$$\displaystyle f^{(2)}(x)=-x\sin x +2\cos x$$
$$\displaystyle f^{(3)}(x)=-x\cos x -3\sin x$$
$$\displaystyle f^{(4)}(x)=x\sin x -4\cos x$$
$$\displaystyle f^{(5)}(x)=x\cos x +5\sin x$$
$$\displaystyle f^{(6)}(x)=-x\sin x +6\cos x$$...

At this point, it is easy to generate the general formula for $$\displaystyle f^{(n)}(x)$$ where

$$\displaystyle f^{(n)}(x)=x\sin (\frac {\pi n}{2}+x)-n\cos (\frac {\pi n}{2}+x)$$

and it can easily be shown to be true using induction method.

Let $$\displaystyle P(n)$$ be the statement $$\displaystyle f^{(n)}(x)=x\sin (\frac {\pi n}{2}+x)-n\cos (\frac {\pi n}{2}+x)$$.

$$\displaystyle P(1)$$ asserts that $$\displaystyle f^{(1)}(x)=x\sin (\frac {\pi }{2}+x)-\cos (\frac {\pi }{2}+x)=x\cos x +\sin x$$ which is clearly true.

Next, suppose $$\displaystyle P(n)$$ is true for $$\displaystyle n=k$$. We need to prove $$\displaystyle P(n)$$ is true for $$\displaystyle n=k+1$$.

For $$\displaystyle P(k+1)$$, we have

$$\displaystyle f^{(k+1)}(x)=\frac{d}{dx} \left(x\sin (\frac {\pi k}{2}+x)-k\cos (\frac {\pi k}{2}+x)\right)$$

$$\displaystyle \;\;\;\;\;\;\;\;\;\;\;\;\;\;=x\cos (\frac {\pi k}{2}+x)(1)+(1)(\sin (\frac {\pi k}{2}+x)-(k)(-\sin (\frac {\pi k}{2}+x))$$

$$\displaystyle \;\;\;\;\;\;\;\;\;\;\;\;\;\;=x\cos (\frac {\pi k}{2}+x)(1)+(1)(\sin (\frac {\pi k}{2}+x)+k\sin (\frac {\pi k}{2}+x)$$

thereby showing that indeed $$\displaystyle P(k+1)$$ holds and this finished our work.

Now, we have $$\displaystyle f(x)=x\cos(x)$$.

Notice that there is a phase shift (delay) of $$\displaystyle \frac{\pi}{2}$$rad from $$\displaystyle y=x\sin x$$ to $$\displaystyle y=x\cos x$$ and also the signs of the two terms are of the opposite have been shifted in the other direction by $$\displaystyle -\frac{\pi}{2}$$ rad.

Therefore, the general formula for $$\displaystyle f^{(n)}(x)$$ if $$\displaystyle f(x)=x\cos x$$ can be obtained from:

$$\displaystyle f^{(n)}(x)=-x\sin (\frac {\pi n}{2}+x-\frac{\pi}{2})+n\cos (\frac {\pi n}{2}+x-\frac{\pi}{2})$$

$$\displaystyle \;\;\;\;\;\;\;\;\;\;\;=x\cos (\frac {\pi n}{2}+x)+n\sin (\frac {\pi n}{2}+x)$$

#### MarkFL

Staff member
Good work, anemone!

One of my approaches is quite similar to that which you gave, the other being that given by Ackbach.

This is how I formulated this approach:

Given:

$$\displaystyle f(x)=x\sin(x)$$

we find:

$$\displaystyle f'(x)=x\cos(x)+\sin(x)$$

$$\displaystyle f''(x)=-x\sin(x)+2\cos(x)$$

$$\displaystyle f'''(x)=-x\cos(x)-3\sin(x)$$

At this point, we may state the hypothesis:

$$\displaystyle f^{(n)}(x)=x\sin\left(x+n\frac{\pi}{2} \right)-n\cos\left(x+n\frac{\pi}{2} \right)$$

Having already demonstrated the base case (and the next few), we may use as our inductive step, differentiation of $P_n$ with respect to $x$:

$$\displaystyle f^{(n+1)}(x)=x\cos\left(x+n\frac{\pi}{2} \right)+(n+1)\sin\left(x+n\frac{\pi}{2} \right)$$

Now, using the identities:

(1) $$\displaystyle \sin\left(\theta-\frac{\pi}{2} \right)=-\cos(\theta)$$

(2) $$\displaystyle \cos\left(\theta-\frac{\pi}{2} \right)=\sin(\theta)$$

we may write:

$$\displaystyle f^{(n+1)}(x)=x\sin\left(x+(n+1)\frac{\pi}{2} \right)-(n+1)\cos\left(x+(n+1)\frac{\pi}{2} \right)$$

We have derived $P_{n+1}$ from $P_n$, thereby completing the proof by induction.

Now, if we are given:

$$\displaystyle g(x)=x\cos(x)=x\sin\left(x+\frac{\pi}{2} \right)$$

Then from the above, we may infer:

$$\displaystyle g^{(n)}(x)=x\sin\left(x+(n+1)\frac{\pi}{2} \right)-n\cos\left(x+(n+1)\frac{\pi}{2} \right)$$

and using (1) and (2), we find:

$$\displaystyle g^{(n)}(x)=x\cos\left(x+n\frac{\pi}{2} \right)+n\sin\left(x+n\frac{\pi}{2} \right)$$

Another way to infer this second formula is to write:

$$\displaystyle h(x)=x\sin\left(x+\frac{\pi}{4} \right)$$

Thus, we know:

$$\displaystyle h^{(n)}(x)=x\sin\left(x+\frac{\pi}{4}+n\frac{\pi}{2} \right)-n\cos\left(x+\frac{\pi}{4}+n\frac{\pi}{2} \right)$$

Using the identities:

$$\displaystyle \sin\left(\theta+\frac{\pi}{4} \right)=\frac{1}{\sqrt{2}}\left(\sin(\theta)+\cos(\theta) \right)$$

$$\displaystyle \cos\left(\theta+\frac{\pi}{4} \right)=\frac{1}{\sqrt{2}}\left(\cos(\theta)-\sin(\theta) \right)$$

we may write:

$$\displaystyle h^{(n)}(x)=\frac{1}{\sqrt{2}}\left(x\left(\sin \left(x+n\frac{\pi}{2} \right)+\cos\left(x+n\frac{\pi}{2} \right) \right)-n \left(\cos\left(x+n\frac{\pi}{2} \right)-\sin \left(x+n\frac{\pi}{2} \right) \right) \right)$$

$$\displaystyle h^{(n)}(x)=\frac{1}{\sqrt{2}}\left(x\sin \left(x+n\frac{\pi}{2} \right)-n\cos\left(x+n\frac{\pi}{2} \right)+x\cos\left(x+n\frac{\pi}{2} \right)+n\sin \left(x+n\frac{\pi}{2} \right) \right)$$

$$\displaystyle h^{(n)}(x)=\frac{1}{\sqrt{2}}\left(\frac{d^n}{dx^n}\left(x\sin(x) \right)+x\cos\left(x+n\frac{\pi}{2} \right)+n\sin \left(x+n\frac{\pi}{2} \right) \right)$$

Now, since we may write:

$$\displaystyle h(x)=\frac{1}{\sqrt{2}}\left(x\sin(x)+x\cos(x) \right)$$

And by the linearity of differentiation, we know:

$$\displaystyle h^{(n)}(x)=\frac{1}{\sqrt{2}}\left(\frac{d^n}{dx^n}\left(x\sin(x) \right)+\frac{d^n}{dx^n}\left(x\cos(x) \right) \right)$$

Thus, we must have:

$$\displaystyle \frac{d^n}{dx^n}\left(x\cos(x) \right)=x\cos\left(x+n\frac{\pi}{2} \right)+n\sin \left(x+n\frac{\pi}{2} \right)$$