% This is lecture no. 23 of MH1101/: Calculus II.

\chapter{Taylor \& Maclaurin Series}%
\label{cha:taylor_&_maclaurin_series}

The Taylor \& Maclaurin Series are general methods for finding power series representations of functions.

\section{Taylor \& Maclaurin Series}%
\label{sec:taylor_series}

Suppose that $f(x)$ has a power series expansion centred at $x=a$ that is valid for all $x$ in an interval $(a-R,a+R)$ with $R>0$.
If such a power series expansion did exist, it would have the following form:
\[
	f(x) = c_0 + c_1(x-a) + c_2(x-a)^2 + c_3(x-a)^3 +\dotsb
\]
where $\lvert x-a\rvert<R$.
Then, our goal is simply to determine the coefficients $c_0,c_1,c_2,\dotsc$
\begin{itemize}
	\item To find $c_0$:
		Substitute $x=a$ into the power series, getting:
		\[
			c_0=f(a)
		.\]
	\item To find $c_1$:
		Differentiate the power series term by term:
		\[
			f^{\prime}(x) = c_1+2c_2(x-a) + 3c_3(x-a)^2 + 4c_4(x-a)^3 + \dotsb
		\]
		Then, substitute $x=a$ into the above to give:
		\[
			c_1=f^{\prime}(a)
		.\]
	\item To find $c_2$:
		Differentiate the power series term by term another time:
		\[
			f^{(2)}(x) = 2c_2 + 3\cdot 2c_3(x-a) + 4\cdot 3c_4(x-a)^2 + \dotsb
		\]
		Substitute $x=a$ into the above getting:
		\[
			c_2 = \frac{1}{2}f^{(2)}(a)= \frac{1}{2!}f^{(2)}(a)
		.\]
	\item To find $c_3$:
		Differentiate the power series term by term another time:
		\[
			f^{(3)}(x) = 3\cdot 2c_2 + 4\cdot 3\cdot 2c_3(x-a) + 5\cdot 4\cdot 3c_4(x-a)^2 + \dotsb
		\]
		Substitute $x=a$ into the above getting:
		\[
			c_3 = \frac{1}{3\cdot 2}f^{(3)}(a)= \frac{1}{3!}f^{(3)}(a)
		.\]
\end{itemize}
\bigbreak
Clearly, there is a general pattern.
Solving the equation for the $n$-th coefficient, we get:
\[
	c_n = \frac{f^{(n)}(a)}{n!}
.\]
\begin{thrm}
	Uniqueness of the Power Series Expansion.
	If $f$ has a power series expansion at $x=a$, that is, if:
	\[
		f(x) = \sum_{n=0}^{\infty} c_n(x-a)^{n},\ \lvert x-a\rvert<R,\ R>0
	,\]
	then its coefficients are given by the formula:
	\[
		c_n = \frac{f^{(n)}(a)}{n!}
	.\]
	This power series is called the \textbf{Taylor Series of $f(x)$ centred at $x=a$}.
	In the special case where $a=0$, the Taylor series is also called the \textbf{Maclaurin Series}.
\end{thrm}
\begin{exmp}
	Find the Maclaurin series of the function $f(x) = e^{x}$ and its radius of convergence.
	\begin{sol}
		Note that $f^{(n)}(x)=e^{x}$ for all $n$.
		Therefore:
		\[
			f^{(n)}(0) = 1,\ \forall n \geqslant 0
		.\]
		Therefore, the Maclaurin series of $f$ is:
		\[
			\sum_{n=0}^{\infty} \frac{f^{(n)}(0)}{n!}x^{n} = \sum_{n=0}^{\infty} \frac{x^{n}}{n!} = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \dotsb
		\]
		To find the radius of convergence, we let $a_n = \ds{\frac{x^{n}}{n!}}$.
		Then:
		\[
			\lim_{n \to \infty}\left\lvert \frac{a_{n+1}}{a_n}\right\rvert =\lim_{n \to \infty} \left\lvert \frac{x^{n+1}}{(n+1)!}\cdot \frac{n!}{x^{n}}\right\rvert = \lim_{n \to \infty}\frac{\lvert x\rvert}{n+1} = 0 < 1
		.\]
		So, by the ratio test, the series converges for all $x$, and the radius of convergence is $R=\infty$.
		\begin{note}
			This example only tells us that \textbf{if} there exists a power series for $e^{x}$, then it is equal to the Maclaurin series as calculated.
		\end{note}
	\end{sol}
\end{exmp}
\section{Remainder \& Error Bounds of Power Series}%
\label{sec:remainder_&_error_bounds_of_power_series}

\subsection{Taylor's Theorem \& The Remainder Polynomial}%
\label{sub:taylor_s_theorem_&_the_remainder_plnma}

To show that a function has a power series representation, we need to study the convergence of Taylor series.
Let us consider the $n$-th degree Taylor polynomial of $f$ at $a$:
\begin{align*}
	T_n(x) & = \sum_{i=0}^{n} \frac{f^{(i)}(a)}{i!}(x-a)^{i}\\
	       & = f(a) + \frac{f^{\prime}(a)}{1!}(x-a) + \frac{f^{(2)}(a)}{2!}(x-a)^2 + \dotsb + \frac{f^{(n)}(a)}{n!}(x-a)^{n}.
\end{align*}
Note that the $n$-th degree Taylor polynomial is just the sum of the first $(n+1)$ terms in the corresponding Taylor series.
Therefore, $f(x)$ can be represented by its Taylor series if:
\[
	f(x) = \lim_{n \to \infty}T_n(x)
.\]
We denote the $n$-th remainder of $f$ at $a$ by:
\[
	R_n(x) = f(x) - T_n(x)
.\]
It follows that:
\[
	f(x) = T_n(x) + R_n(x)
.\]
So if we can show that the remainder vanishes, that is:
\[
	\lim_{n \to \infty}R_n(x)=0\implies \lim_{n \to \infty}T_n(x) = f(x)
.\]
We have therefore proved the following theorem.
\begin{thrm}
	Remainder Polynomial tends to 0.
	If:
	\[
		\lim_{n \to \infty}R_n(x) = 0,\ \forall \lvert x-a\rvert<R
	,\]
	then $f$ is equal to its Taylor series on the interval $\lvert x-a\rvert<R$.
\end{thrm}
Next, we study $R_n(x)$, known as Taylor's Theorem.
\begin{thrm}
	Taylor's Theorem.
	Assume that $f^{(n+1)}(x)$ exists and is continuous.
	Then:
	\[
		R_n(x) = \frac{1}{n!}\int_{a}^{x} (x-u)^{n}f^{(n+1)}(u) \ud u
	.\]
\end{thrm}
\begin{proof}
	Taylor's Theorem.
	Set:
	\[
		I_n = \frac{1}{n!}\int_{a}^{x} (x-u)^{n}f^{(n+1)}(u) \ud u
	.\]
	For $n=0$, we have by definition that:
	\[
		R_0(x)=f(x)-f(a)
	.\]
	On the other hand,
	\[
		I_0(x)=\frac{1}{0!}\int_{a}^{x} (x-u)^{0}f^{(0+1)}(u) \ud u = \int_{a}^{x} f^{\prime}(x)(u) \ud u = f(x)-f(a)
	,\]
	by the fundamental theorem of calculus.
	Hence, we have proved that:
	\[
		R_0(x)=I_0(x)
	.\]
	To prove the formula for $n>0$, we apply integration by parts to $I_n(x)$ with:
	\[
		U = \frac{(x-u)^{n}}{n!},\ V^{\prime}=f^{(n+1)}(u)
	.\]
	Then:
	\[
		U^{\prime}=-\frac{(x-u)^{n-1}}{(n-1)!},\ V = f^{(n)}(u)
	.\]
	So we have:
	\begin{align*}
		I_n(x) & = \int_{a}^{x} UV^{\prime} \ud u\\
		       & = [UV]^{x}_{a} - \int_{a}^{x} U^{\prime}V \ud u\\
		       & = \left[\frac{1}{n!}(x-u)^{n}f^{(n)}(u)\right]^{x}_{a} - \int_{a}^{x} -\frac{(x-u)^{n-1}}{(n-1)!}f^{(n)}(u) \ud u\\
		       & = -\frac{1}{n!}(x-a)^{n}f^{(n)}(a) + I_{n-1}(x).
	\end{align*}
	The result can be rewritten as:
	\[
		I_{n-1}(x) = \frac{f^{(n)}(a)}{n!}(x-a)^{n}+I_n(x)
	.\]
	Now, apply the recurrence relation $n$ times:
	\begin{align*}
		f(x) & = f(a) + I_0(x)\\
		     & = f(a) + \frac{f^{\prime}(a)}{1!}(x-a) + I_1(x)\\
		     & = f(a) + \frac{f^{\prime}(a)}{1!}(x-a) + \frac{f^{(2)}(a)}{2!}(x-a)^2+I_2(x)\\
		     &\ \,\vdots\\
		     & = f(a) + \frac{f^{\prime}(a)}{1!}(x-a) + \dotsb + \frac{f^{(n)}(a)}{n!}(x-a)^{n}+I_n(x)\\
		     & = T_n(x) + I_n(x).
	\end{align*}
	This shows that $I_n(x)=f(x)-T_n(x)$ which, by definition must be equal to $R_n(x)$.
\end{proof}

\subsection{Error Bound for Taylor Series}%
\label{sub:error_bound_for_taylor_series}

\begin{thrm}
	Error Bound for Taylor Series.
	Let $M$ be a number such that $\lvert f^{(n+1)}(x)\rvert\leqslant M,\ \forall \lvert x-a\rvert\leqslant d$.
	Then:
	\[
		\lvert R_n(x)\rvert\leqslant M \frac{\lvert x-a\rvert^{n+1}}{(n+1)!},\quad \lvert x-a\rvert\leqslant d
	.\]
\end{thrm}
\begin{proof}
	Error Bound for Taylor Series.
	Assume that $a\leqslant x\leqslant a+d$.
	Then, since $\lvert f^{(n+1)}(u)\rvert\leqslant M$ for all $a\leqslant u\leqslant x$ we have:
	\begin{align*}
		\lvert R_n(x)\rvert & = \left\lvert \frac{1}{n!}\int_{a}^{x} (x-u)^{n}f^{(n+1)}(u) \ud u\right\rvert\\
				    & \leqslant \frac{1}{n!}\int_{a}^{x} \lvert (x-u)^{n}f^{(n+1)}(u)\rvert \ud u\\
		\intertext{And since $a\leqslant u\leqslant x$, we ignore the absolute value sign:}
				    & \leqslant \frac{M}{n!}\int_{a}^{x} (x-u)^{n} \ud u\\
				    & = \frac{M}{n!}\left[\frac{-(x-u)^{n+1}}{n+1}\right]^{x}_{a}\\
				    & = M \frac{\lvert x-a\rvert^{n+1}}{(n+1)!}.
	\end{align*}
\end{proof}
\begin{exmp}
	Prove that $f(x)=e^{x}$ is equal to its Maclaurin Series.
	\begin{sol}
		It is enough to show that the remainder polynomial $R_n(x)$ converges to 0 for all $x$.
		First, pick an arbitrary positive number $d$.
		Note that:
		 \[
			 \lvert f^{(n+1)}(x)\rvert=e^{x}\leqslant e^{d},\ \forall \lvert x\rvert\leqslant d
		.\]
		Now apply the error bound for Taylor series with $a=0$ and $M=e^{d}$ to get:
		\[
			\lvert R_n(x)\rvert\leqslant \frac{e^{d}}{(n+1)!}\lvert x\rvert^{n+1},\ \forall \lvert x\rvert\leqslant d
		.\]
		Taking the limit of the right hand side we get:
		\begin{align*}
			\lim_{n \to \infty} \frac{e^{d}}{(n+1)!}\lvert x\rvert^{n+1} & = e^{d}\lim_{n \to \infty}\frac{\lvert x\rvert^{n+1}}{(n+1)!}\\
										     & = 0.
		\end{align*}
		It follows from the squeeze theorem that:
		\[
			\lim_{n \to \infty}\lvert R_n(x)\rvert=0
		,\]
		and so:
		\[
			\lim_{n \to \infty}R_n(x) = 0,\ \forall \lvert x\rvert\leqslant d
		.\]
		Since $d$ is arbitrary, the convergence is valid for all $x$.
	\end{sol}
\end{exmp}
\begin{exmp}
	Find the Maclaurin series for $\sin x$ and prove that it represents $\sin x,\ \forall x\in\mathbb{R}$.
	\begin{sol}
		First, we find the Maclaurin series.
		This is done by finding the form of the $n$-th term.
		We arrange the prerequisite computations for the first few terms of the sequence:
		\[
		\begin{aligned}
			f(x) & = \sin x\\
			f^{\prime}(x) & = \cos x\\
			f^{(2)}(x) & = -\sin x\\
			f^{(3)}(x) & = -\cos x\\
			f^{(4)}(x) & = \sin x
		\end{aligned}
		\qquad
		\begin{aligned}
			f(0) & = 0\\
			f^{\prime}(0) & = 1\\
			f^{(2)}(0) & = 0\\
			f^{(3)}(0) & = -1\\
			f^{(4)}(0) & = 0\\
		\end{aligned}
		\]
		Clearly, we see that the derivatives repeat in a cycle of four, therefore, we can write the Maclaurin series as follows:
		\begin{align*}
			T_n(x) & = f(0) + \frac{f^{\prime}(0)}{1!}x + \frac{f^{(2)}(0)}{2!}x^2 + \frac{f^{(3)}(0)}{3!}x^3 + \dotsb\\
			       & = x - \frac{x^3}{3!} + \frac{x^{5}}{5!} - \frac{x^{7}}{7!} + \dotsb\\
			       & = \sum_{n=0}^{\infty} (-1)^{n}\frac{x^{2n+1}}{(2n+1)!}.
		\end{align*}
		Next, we must show that $R_n(x)$ approaches 0 as $n\to\infty$.
		Since $f^{(n+1)}(x)=\pm \sin x$ or $\pm\cos x$, we have:
		\[
			\lvert f^{(n+1)}(x)\rvert\leqslant 1,\ \forall x
		.\]
		Apply the error bound formula with $M=1$, to deduce that:
		\[
			\lvert R_n(x)\rvert\leqslant \frac{M}{(n+1)!}\lvert x^{n+1}\rvert= \frac{\lvert x\rvert^{n+1}}{(n+1!)}
		.\]
		It can be shown that:
		\[
			\lim_{n \to \infty}\frac{\lvert x\rvert^{n+1}}{(n+1)!}=0
		,\]
		so by the squeeze theorem, $\ds{\lim_{n \to \infty}\lvert R_n(x)\rvert=0}$ and therefore, $\ds{\lim_{n \to \infty}R_n(x)=0}$, $\forall x$ as required.
	\end{sol}
\end{exmp}
\begin{exmp}
	Find the Maclaurin series for $\cos x$ and prove that it represents $\cos x$, $\forall x\in\mathbb{R}$.
	\begin{sol}
		We could proceed  directly as in the preceding example, but it is easier to differentiate the Maclaurin series for $\sin x$:
		\begin{align*}
			\cos x & = \frac{\upd}{\upd x}[\sin x]\\
			       & = \frac{\upd}{\upd x}\left[x - \frac{x^3}{3!} + \frac{x^{5}}{5!} - \frac{x^{7}}{7!} + \dotsb\right]\\
			       & = 1 - \frac{3x^2}{3!} + \frac{5x^{4}}{5!} - \frac{7x^{6}}{7!} + \dotsb\\
			       & = 1 - \frac{x^2}{2!} + \frac{x^{4}}{4!} - \frac{x^{6}}{6!} + \dotsb
		\end{align*}
		Since the Maclaurin series for $\sin x$ converges for all $x$, Theorem \ref{tbt} tells us that the differentiated series for $\cos x$ also converges for all $x$.
		Thus:
		\begin{align*}
			\cos x & = 1 - \frac{x^2}{2!} + \frac{x^{4}}{4!} - \frac{x^{6}}{6!} + \dotsb\\
			       & = \sum_{n=0}^{\infty} (-1)^{n}\frac{x^{2n}}{(2n)!}
		\end{align*}
	\end{sol}
\end{exmp}
