\section{The Mean Value Theorem}\label{sec:MVT}
There are numerous applications of the
derivative through its \textbf{definition} as rate of change and as the
slope of the tangent line. In this section
we shall look at some deeper reasons why the derivative turns out to be so
useful. The simple answer is that \textbf{the derivative of a function tells
us a lot about the function}. More
important, ``hard'' questions about a
function can sometimes be answered by solving a relatively simple problem
about the derivative of the function.

$$\includegraphics[width=11cm]{images/mvt-intro-diagram-1}$$

The Mean Value Theore tells us that
there is an intimate connection between the net change of the value of any
``sufficiently nice'' function over an
interval and the possible values of its derivative on that interval. Because
of this connection, we can draw conclusions about the possible values of the
derivative based on information about the values of the function, and
conversely, we can draw conclusions about the values of the function based
on information about the values of its derivative.

$$\includegraphics[width=11cm]{images/mvt-intro-diagram-2}$$

Let us illustrate the idea through the following two interesting questions
involving derivatives:

\begin{enumerate}
	\item Suppose two different functions have the same derivative;
		  what can you say about the relationship between the two functions?
	\item Suppose you drive a car from toll booth on a toll road to
		  another toll booth at an average speed of 70 miles per hour.
		  What can be concluded about your actual speed during the trip?
		  In particular, did you exceed the 65 mile per hour speed limit?
\end{enumerate}

While these sound very different, it turns out that the two problems
are very closely related. We know that ``speed'' is really the
derivative by a different name; let's start by translating the second
question into something that may be easier to visualize. Suppose that
the function $f(t)$ gives the position of your car on the toll road at
time $t$. Your change in position between one toll booth and the next
is given by $\ds f(t_1)-f(t_0)$, assuming that at time $\ds t_0$ you were at
the first booth and at time $\ds t_1$ you arrived at the second
booth. Your average speed for the trip is
$\ds (f(t_1)-f(t_0))/(t_1-t_0)$. If we think about the graph of $f(t)$,
the average speed is the slope of the line that connects the two
points $\ds (t_0,f(t_0))$ and $\ds (t_1,f(t_1))$. Your speed at any particular time
$t$ between $\ds t_0$ and $\ds t_1$ is $f'(t)$, the slope of the curve. Now
question (2) becomes a question about slope. In particular, if the
slope between endpoints is 70, what can be said of the slopes at
points between the endpoints?

As a general rule, when faced with a new problem it is often a good idea to
examine one or more simplified versions of the problem, in the hope
that this will lead to an understanding of the original problem.
In this case, the problem in its ``slope'' form is somewhat easier to
simplify than the original, but equivalent, problem.

Here is a special instance of the problem. Suppose that
$\ds f(t_0)=f(t_1)$. Then the two endpoints have the same height and the
slope of the line connecting the endpoints is zero. What can we say
about the slope between the endpoints? It shouldn't take much
experimentation before you are convinced of the truth of this
statement: Somewhere between $\ds t_0$ and $\ds t_1$ the slope is exactly
zero, that is, somewhere between $\ds t_0$ and $\ds t_1$ the slope is equal to
the slope of the line between the endpoints. This suggests that
perhaps the same is true even if the endpoints are at different
heights, and again a bit of experimentation will probably convince you
that this is so. But we can do better than ``experimentation''---we
can prove that this is so.

We start with the simplified version:

\begin{theorem}{Rolle's Theorem}{rolle}
(Rolle's Theorem) Suppose that $f(x)$ has a derivative on the
interval $(a,b)$, is continuous on the interval $[a,b]$, and
$f(a)=f(b)$. Then at some value $c\in (a,b)$, $f'(c)=0$.
\end{theorem}

\begin{proof}
We know that $f(x)$ has a maximum and minimum value on $[a,b]$
(because it is continuous), and we
also know that the maximum and minimum must occur at an endpoint, at a
point at which the derivative is zero, or at a point where the
derivative is undefined. Since the derivative is never undefined, that
possibility is removed.

If the maximum or minimum occurs at a point $c$, other than an endpoint,
where $f'(c)=0$, then we have found the point we seek. Otherwise, the
maximum and minimum both occur at an endpoint, and since the endpoints
have the same height, the maximum and minimum are the same. This means
that $f(x)=f(a)=f(b)$ at every $x \in [a,b]$, so the function is a
horizontal line, and it has derivative zero everywhere in
$(a,b)$. Then we may choose any $c$ at all to get $f'(c)=0$.
\end{proof}

Rolle's Theorem is illustrated below for a function $f(x)$ where $f'(x)=0$ holds for two values of $x=c_1$ and $x=c_2$:
$$\includegraphics[width=4in]{images/rolle}$$

Perhaps remarkably, this special case is all we need to prove the more
general one as well.

\begin{theorem}{Mean Value Theorem}{mvt}
Suppose that $f(x)$ has a derivative on the
interval $(a,b)$ and is continuous on the interval $[a,b]$.
Then at some value $c\in (a,b)$, $\ds f'(c)={f(b)-f(a)\over b-a}$.
\end{theorem}

\begin{proof}
Let $\ds m={f(b)-f(a)\over b-a}$, and consider a new function
$g(x)=f(x) - m(x-a)-f(a)$. We know that $g(x)$ has a derivative
everywhere, since $g'(x)=f'(x)-m$. We can compute
$g(a)=f(a)- m(a-a)-f(a) =0$ and
\begin{eqnarray*}
g(b)=f(b)-m(b-a)-f(a)&=f(b)-{f(b)-f(a)\over b-a}(b-a)-f(a)\cr
&=f(b)-(f(b)-f(a))-f(a)=0.\cr
\end{eqnarray*}
So the height of $g(x)$ is the same at both endpoints. This means, by
Rolle's Theorem, that at some $c$, $g'(c)=0$. But we know that
$g'(c)=f'(c)-m$, so
$$0=f'(c)-m=f'(c)-{f(b)-f(a)\over b-a},$$
which turns into
$$f'(c)={f(b)-f(a)\over b-a},$$
exactly what we want.
\end{proof}

The Mean Value Theorem is illustrated below showing the existence of a point $x=c$ for a function $f(x)$ where the tangent line at $x=c$ (with slope $f'(c)$) is parallel to the secant line connecting $A(a,f(a))$ and $B(b,f(b))$ (with slope ${f(b)-f(a)\over b-a}$):
$$\includegraphics[width=2.0in]{images/mvt}$$

Returning to the original formulation of question (2), we see that if
$f(t)$ gives the position of your car at time $t$, then the Mean Value
Theorem says that at some time $c$, $f'(c)=70$, that is, at some time
you must have been traveling at exactly your average speed for the
trip, and that indeed you exceeded the speed limit.

Now let's return to question (1). Suppose, for example, that two
functions are known to have derivative equal to 5 everywhere,
$f'(x)=g'(x)=5$. It is easy to find such functions: $5x$, $5x+47$,
$5x-132$, etc. Are there other, more complicated, examples? No---the
only functions that work are the ``obvious'' ones, namely, $5x$ plus
some constant. How can we see that this is true?

Although ``5'' is a very simple derivative, let's look at an even
simpler one. Suppose that $f'(x)=g'(x)=0$. Again we can find examples:
$f(x)=0$, $f(x)=47$, $f(x)=-511$ all have $f'(x)=0$. Are there
non-constant functions $f$ with derivative 0? No, and here's why:
Suppose that $f(x)$ is not a constant function. This means that there
are two points on the function with different heights, say
$f(a)\not=f(b)$. The Mean Value Theorem tells us that at some point
$c$, $f'(c)=(f(b)-f(a))/(b-a)\not=0$. So any non-constant function
does not have a derivative that is zero everywhere; this is the same
as saying that the only functions with zero derivative are the
constant functions.

Let's go back to the slightly less easy example: suppose that
$f'(x)=g'(x)=5$. Then $(f(x)-g(x))' = f'(x)-g'(x) = 5 -5 =0$. So using
what we discovered in the previous paragraph, we know that
$f(x)-g(x)=k$, for some constant $k$. So any two functions with
derivative 5 must differ by a constant; since $5x$ is known to work,
the only other examples must look like $5x+k$.

Now we can extend this to more complicated functions, without any
extra work. Suppose that $f'(x)=g'(x)$. Then as before
$(f(x)-g(x))' = f'(x)-g'(x) =0$, so $f(x)-g(x)=k$. Again this means
that if we find just a single function $g(x)$ with a certain
derivative, then every other function with the same derivative must be
of the form $g(x)+k$.

\begin{example}{Given Derivative}{givenderivative}
Describe all functions that have derivative $5x-3$.
\end{example}

\begin{solution}
It's easy to find
one: $\ds g(x)=(5/2)x^2-3x$ has $g'(x)=5x-3$. The only other functions
with the same derivative are therefore of the form
$\ds f(x)=(5/2)x^2-3x+k$.

Alternately, though not obviously, you might have first noticed that
$\ds g(x)=(5/2)x^2-3x+47$ has $g'(x)=5x-3$. Then every other function
with the same derivative must have the form $\ds f(x)=(5/2)x^2-3x+47+k$.
This looks different, but it really isn't. The functions of the form
$\ds f(x)=(5/2)x^2-3x+k$ are exactly the same as the ones of the form
$\ds f(x)=(5/2)x^2-3x+47+k$. For example, $\ds (5/2)x^2-3x+10$ is the same as
$\ds (5/2)x^2-3x+47+(-37)$, and the first is of the first form while
the second has the second form.
\end{solution}

This is worth calling a theorem:

\begin{theorem}{Functions with the Same Derivative}{FunctionsSameDerivative}
If $f'(x)=g'(x)$ for every $x\in (a,b)$, then for some constant
$k$, $f(x)=g(x)+k$ on the interval $(a,b)$.
\end{theorem}

\begin{example}{Same Derivative}{SameDerivative}
Describe all functions with derivative $\ds \sin x + e^x$. One such
function is $\ds -\cos x+e^x$, so all such functions have the form
$-\cos x+e^x+k$.
\end{example}

Theorem~\ref{thm:FunctionsSameDerivative} and the above example illustrate what the Mean
Value Theorem allows us to say about $f(x)$ when we have
perfect information about $f^{\prime}(x)$. Specifically, $f(x)$
is determined up to a constant. Our next example
illustrates almost the opposite extreme situation, one where we have much
less information about $f^{\prime}(x)$ beyond the fact that $f^{\prime}(x)$
exists. Specifically, assuming that we know an
upper bound on the values of $f^{\prime}(x)$, what can we say
about the values of $f(x)$?

\begin{example}{Conclusion Regarding Function Value Based on Derivative Information}{FuncValDerivativeInfo}
Suppose that $f$ is a differentiable function such that $f^{\prime }\left(
x\right) \leq 2$ for all $x.$ What is the largest possible value of $f\left(
7\right) $ if $f\left( 3\right) =5?$
\end{example}
\begin{solution}
We are interested in the values of $f\left( x\right) $ at $x=3$
and $x=7.$ It makes sense to focus our attention on the interval between 3
and 7. It is given that $f\left( x\right) $ is differentiable for all $x.$
So, $f\left( x\right) $ is also continuous at all $x.$ In particular, $%
f\left( x\right) $ is continuous on the interval $\left[ 3,7\right] $ and
differentiable on the interval $\left( 3,7\right) .$ By the Mean Value
Theorem, we know that there is some $c$ in $\left( 3,7\right) $ such that
\begin{equation*}
f^{\prime }\left( c\right) =\frac{f\left( 7\right) -f\left( 3\right) }{7-3}.
\end{equation*}%
Simplifying and using the given information $f\left( 3\right) =5$, we get
\begin{equation*}
f^{\prime }\left( c\right) =\frac{f\left( 7\right) -5}{4},
\end{equation*}%
or, after re-arranging the terms,%
\begin{equation*}
f\left( 7\right) =4f^{\prime }\left( c\right) +5.
\end{equation*}%
We do not know the exact value of $c,$ but we do know that $f^{\prime
}\left( x\right) \leq 2$ for all $x.$ This implies that $f^{\prime }\left(
c\right) \leq 2.$ Therefore,%
\begin{equation*}
f\left( 7\right) \leq 4\cdot 2+5=13.
\end{equation*}%
That is, the value of $f\left( 7\right) $ cannot exceed 13. To convince
ourselves that 13 (as opposed to some smaller number) is the largest
possible value of $f\left( 7\right) ,$ we still need to show that it is
possible for the value of $f\left( 7\right) $ to reach 13. If we review our
proof, we notice that the inequality will be an equality if $f^{\prime
}\left( c\right) =2.$ One way to guarantee this without knowing anything
about $c$ is to require $f^{\prime }\left( x\right) =2$ for all $x.$ This
means that $f\left( x\right) =2x+k$ for some constant $k.$ From the
condition $f\left( 3\right) =5,$ we see that $k=-1.$ We can easily verify
that indeed $f\left( x\right) =2x-1$ meets all our requirements and $f\left(
7\right) =13$.
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for Section \ref{sec:MVT}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
 Let $\ds f(x) = x^2$.
Find a value $c\in (-1,2)$ so that $f'(c)$ equals the slope between
the endpoints of $f(x)$ on $[-1,2]$.
\begin{sol}
 $c=1/2$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Verify that $f(x) = x/(x+2)$ satisfies the hypotheses of the
 Mean Value Theorem on the interval $[1,4]$ and then find all of the
 values, $c$, that satisfy the conclusion of the theorem.
\begin{sol}
 $\ds c=\sqrt{18}-2$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Verify that $f(x) = 3x/(x+7)$ satisfies the hypotheses of the
 Mean Value Theorem on the interval $[-2 , 6]$ and then find all of the
 values, $c$, that satisfy the conclusion of the theorem.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Let $f(x) = \tan x $. Show that $f(\pi ) = f(2\pi)=0$ but
there is no number $c\in (\pi,2\pi)$ such that $f'(c) =0$. Why does
this not contradict Rolle's theorem?
\end{ex}

%%%%%%%%%%
\begin{ex}
 Let $\ds f(x) = (x-3)^{-2}$.  Show that there is no value
$c\in (1,4)$ such that $f'(c) = (f(4)-f(1))/(4-1)$.  Why is
this not a contradiction of the Mean Value Theorem?
\end{ex}

%%%%%%%%%%
\begin{ex}
 Describe all functions with derivative $\ds x^2+47x-5$.
\begin{sol}
 $\ds x^3/3+47x^2/2-5x+k$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Describe all functions with derivative $\ds {1\over 1+x^2}$.
\begin{sol}
 $\arctan x + k$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Describe all functions with derivative $\ds x^3-{1\over x}$.
\begin{sol}
 $\ds x^4/4 -\ln x +k$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Describe all functions with derivative $\sin(2x)$.
\begin{sol}
 $-\cos(2x)/2 +k$
\end{sol}
\end{ex}

\begin{ex}
Find $f\left( x\right) $ if $f^{\prime }\left( x\right) =e^{-x}$ and $%
f\left( 0\right) =2$.
\end{ex}

\begin{ex}
Suppose that $f$ is a differentiable function such that $f^{\prime }\left(
x\right) \geq -3$ for all $x.$ What is the smallest possible value of $%
f\left( 4\right) $ if $f\left( -1\right) =2$?
\end{ex}

%%%%%%%%%%
\begin{ex}
 Show that the equation $\ds 6x^4 -7x+1 =0$ does not have more
than two distinct real roots.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Let $f$ be differentiable on $\R$. Suppose that $f'(x) \neq
0$ for every $x$. Prove that $f$ has at most one real root.
\end{ex}

%%%%%%%%%%
\begin{ex}
 Prove that for all real $x$ and $y$
$|\cos x -\cos y | \leq |x-y|$.
State and prove an analogous result involving sine.
\end{ex}

%%%%%%%%%%
\begin{ex}
Show that $\ds \sqrt{1+x} \le 1 +(x/2)$ if $-1<x<1$.
\end{ex}

%%%%%%%%%%
\begin{ex}
Suppose that $f(a)=g(a)$
and that $f^{\prime}(x)\leq g^{\prime}(x)$ for
all $x\geq a$.
\begin{enumerate}
	\item	Prove that $f(x)\leq g(x)$ for all $x\geq a$.
	\item	Use part (a) to prove that $e^x\geq 1+x$ for all $x\geq	0$.
	\item	Use parts (a) and (b) to prove that $e^x\geq 1+x+\dfrac{x^2}{2}$ for all $x\geq 0$.
	\item	Can you generalize these results?
\end{enumerate}
\end{ex}

\end{enumialphparenastyle}


% ***********************************************************************************
% Pure LaTeX part to be inserted in a document (be careful of depencies of packages & commands)
% Prepared by XXX and YYY under the supervision of Arnaud de La Fortelle
% Fall 2017
% 1D heat diffusion subsection of the simulation part
% ***********************************************************************************

\subgroup{1}{Lin Yang and Bradley Cage}

\paragraph{Model presentation}

\begin{figure}[htb]
	\centering
	\includegraphics[width=8cm]{Figures/Heat1D_model.png}
	\caption{The heated rod system, with the temperature at any point given by $T(x,t)$ }
	\label{Heat1D_model.fig}
\end{figure}

Our system is comprised of a rod fixed to a heated wall kept at constant temperature. The length of the rod is arbitrarily taken as 1m. The initial temperature of the rod is in equilibrium with the ambient temperature at 25$^{\circ} $C and the temperature of the wall is 200$^{\circ} $C.  We represent temperature at a point on the rod at some time with the function $T(x,t)$. Note that the rod is thin with respect to its length, thus is modeled as a 1D system, that is, temperature is solely a function of rod position and time. The goal of this simulation is to show the variations in temperature of various points on the rod over time.

We take our initial state as:
\begin{itemize}
	\item $T(0,t)=200 ^{\circ}$C
    \item $T(x,t)=25 ^{\circ}$C, $x\ne 0$
\end{itemize}

\noindent The partial differential equation of the 1D heat propagation:
\begin{equation}
 \frac{\partial T}{\partial t} = \alpha \frac{\partial^2 T}{\partial x^2}
\end{equation}
\noindent We explicitly state that $u(\cdot)$ is a function of $x$ and $t$
\begin{equation}
 \frac{\partial} {\partial t}T(x,t) = \alpha \frac{\partial^2}{\partial x^2}T(x,t)
\end{equation}
\noindent We use a finite difference approximation to get compute the derivatives in space and time
\begin{equation}
\frac{T_{i}^{N+1}-T_{i}^{N}}{\Delta t} =\alpha\frac{T_{i+1}^{N}-2T_{i}^{N}+T_{i-1}^{N}}{\Delta x^2}
\end{equation}

\noindent Rearranging we reach the final form we need for our Forward Euler approximation. Note that the quantity $\alpha\frac{\Delta t}{\Delta x^2}$ is Fourier's number, with $\alpha$ being the thermal diffusivity of a material. In the following simulations, we arbitrarily choose Aluminium with $\alpha = 9.7e-5$.

\begin{equation}
T_{i}^{N+1}=T_{i}^{N}+\alpha\frac{\Delta t}{\Delta x^2}\big[ {T_{i+1}^{N}-2T_{i}^{N}+T_{i-1}^{N}}\big]
\end{equation}

\paragraph{Implementation}
We provide an implementation in Matlab.

\begin{lstlisting}[language=Matlab, caption=Forward Euler and Plotting in MATLAB]

  % Values arbitrarily chosen. It's a useful exercise to vary these and look at the results
  T_final = 300;
  N_t = 2000;
  X_final = 1;
  N_x = 100;

  % Calculate time and x steps based on sampling size and # of samples
  T = linspace(0, T_final, N_t+1);
  X = linspace(0, X_final, N_x+1);
  dt = T(2) - T(1);  % Calculate delta t
  dx = X(2) - X(1);  % Calculate delta x

  alpha = 9.7e-5; % Thermal diffusivity of Aluminium in m^2/s
  Fo = (alpha*dt)/(dx^2); % Fouriers number = diffusive transport rate/storage rate

  % Define your initial condition here. This could be some function IC(x),
  %  however for simplicity's sake we take a rod with a uniform temperature
  %  and in contact with a hot plate at one end
  wall_temp = 200;
  init_temp = 25;

  % Initialize the N state and the N-1 state
  u_old = zeros(1, N_x+1);
  u_old(:) = init_temp;
  u_old(1) = wall_temp;
  u_cur = u_old;
  u_plot = u_old;

  for t = 1:N_t
     for i = 2:N_x
     	 % Forward Euler solution to heat equation
         u_cur(i) = Fo*(u_old(i+1) - 2*u_old(i) + u_old(i-1)) + u_old(i);
     end
     u_cur(1) = wall_temp; % Set the left boundary to be our high of 200
     u_old(:) = u_cur; % We move to the next time step, reset N-1 state
     u_plot = [u_plot; u_cur];
  end

  [X_plot,T_plot] = meshgrid(X,T); % Create 2D meshgrid to create surface plot

  surf(X_plot,T_plot,u_plot,'EdgeColor','none') % Create surface plot
  c = colorbar; % Attach colour bar and create scale
  c.Label.String = 'Temperature [C]';

  xlim([0 1]) % Add axis limits
  xlabel('Distance along rod [m]'); % Add descriptive axis labels
  ylabel('Time [s]');
  zlabel('Temperature [C]')

\end{lstlisting}

 \paragraph{Results}
\noindent We can create plots of the rods temperature as a function of time and position. This gives us insight into how the system evolves as we maintain our constant temperature.
\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{Figures/Heat1D_1m.png}
	\caption{Temperature variation for the whole rod over 300 seconds}
	\label{Heat1D_1m.fig}
\end{figure}

\begin{figure}[htb]

	\centering
	\includegraphics[width=9cm]{Figures/Heat1D_0_3m.png}
	\caption{Temperature variation for the rod segment with x varies from 0 to 0.3m over 300 seconds}
	\label{Heat1D_0_3m.fig}
\end{figure}
\clearpage
\begin{figure}[!htb]
	\centering
	\includegraphics[width=9cm]{Figures/Heat1DTop.png}
	\caption{Top view of the temperature variation for the whole rod over 300 seconds}
	\label{fig:Heat1D_top}
\end{figure}

\paragraph{Interpretation}
The results we obtain are consistent with our model and the physical characteristics. We can see that from the visualization the temperature varies in a logarithmic manner, which is
consitent with the fact that the Forward Euler scheme approximates the exact mathematical solution to the 2nd order PDE, which takes the form of an exponential. The exact rate at which heat moves through the rod is
dependent on the thermal conductivity of the material, which makes up part of the exponent (i.e. $e^{-m}$), thus governing the rate.
We can see pathlines of the heat front evolve over time, especially when looking
at the system from above. Given enough time, the rod will reach a steady state, which in this example would have the rod rest at a completely uniform temperature, since we have not incorporated any form of heat losses or sinks in the system.

 \paragraph{Conclusion}
Here we have learned that in examining systems such as the heated rod, we can safely analyze it as a 1D system. We were able to discretize the partial differential equations and
apply a forward Euler simluation to yield phyiscally relevant and meaningful simulations. Through the Matlab visualization, we gain a better understanding of how heat is moving
through the rod and the system. From the resources and analysis provided, it is trivial to create other initial/boundary conditions and repeat the simulations to garner a deeper
insight into the heat behaviour. This is left as an exercise to the reader.


Our final goal is to define complex computational structures through the use of our inference system. The most complex computational structures known to us are programming languages, and as such we will set the mark there: we intend to define the syntax and semantics of a (small) programming language through a series of inference rules. Before we can dive into that, we need the ability to represent some basic algorithms which will prove of fundamental usefulness to defining a programming language. After all, our programming language will have memory of some sort, and as such we need to represent memory somehow.

The first, and perhaps most basic data structure that we can define is the \textit{list}. A list is used to represent a sequence of values which may contain any number of elements: from no elements (the special \textit{empty list}) to hundreds of thousands of elements.

\subsection{Lists of integers}
For our example, we will limit ourselves to lists of integers. It will be possible to extend this definition thanks to the use of inheritance (a list of \texttt{Expr}) or with generic datatypes (which will be explored in a later section), but for now we shall keep things simple.

A list is defined very much like Peano numbers or binary digits. We have a ``termination symbol'' \texttt{nil}, which defines an empty list of integers:

\begin{lstlisting}
Data [] "nil" [] Priority 0 Type ListInt
\end{lstlisting}

Given a list of integers (commonly known as \textit{tail}), we can add an integer to it (commonly known as \textit{head}) and obtain a new list of integers. Head and tail are separated by a symbol such as:

\begin{lstlisting}
Data [<<int>>] ";" [ListInt] Priority 1000 Type ListInt
\end{lstlisting}

Notice that our language assumes the existance of \textit{predefined data types}, which can be accessed with the special brackets \texttt{<<} and \texttt{>>}.

Thanks to this definition, we can now build a few example lists:

\begin{itemize}
\item \texttt{nil}
\item \texttt{1;nil}
\item \texttt{0;(1;(2;(3;nil)))}
\item ...
\end{itemize}

\subsection{List querying}
We will now define a series of list operations that allow us to process a list and extract various properties from it.

\paragraph{Length of a list}
Among the most fundamental operations that we can perform on lists, we have the determination of the length of a list. We define the \texttt{length} symbol that takes a parameter of type list and returns an integer\footnote{Remember that \texttt{<<} and \texttt{>>} identify built-in types, so \texttt{<<int>>} literally means a machine-integer.} as result (the length of the list):

\begin{lstlisting}
Func "length" [ListInt] Priority 100 Type Expr => <<int>>
\end{lstlisting}

Determining the length of the empty list is simple, as we can immediately answer that it has a length of zero:

\begin{lstlisting}
----------------
length nil => 0
\end{lstlisting}

If the input list is not empty, then we compute the length of its tail (\texttt{xs}) and then return this length plus one as the total length of the whole list:

\begin{lstlisting}
length xs => y
--------------------------
length x;xs => <<1 + y>>
\end{lstlisting}

Consider now execution of program \texttt{length 1;2;3;nil}. We start with the initial proposition, which is clearly no instance of \texttt{length nil}:

\begin{lstlisting}
----------------------
length 1;2;3;nil => ?
\end{lstlisting}

The proposition above instances a recursive premise on the tail of the list:

\begin{lstlisting}
length 2;3;nil => y1
-------------------------------
length 1;2;3;nil => <<1 + y1>>
\end{lstlisting}

Proposition \texttt{length 2;3;nil} is, once again, clearly no instance of \texttt{length nil}, and we proceed for a few steps\footnote{Which, with a smile and some handwaving, allows us to skip a couple of boring paragraphs of the book, which you smart reader feel no need for.} until we get to the following derivation tree:

\begin{lstlisting}
length nil => y3
y2 := <<1 + y3>>
------------------------
length 3;nil => y2
y1 := <<1 + y2>>
------------------------
length 2;3;nil => y1
-------------------------------
length 1;2;3;nil => <<1 + y1>>
\end{lstlisting}

The last premise, \texttt{length nil}, is quite obviously an instance of \texttt{length nil}, therefore we can directly jump to the result that \texttt{y3 := 0}:

\begin{lstlisting}
y3 := 0
-----------------
length nil => y3
y2 := <<1 + y3>>
------------------------
length 3;nil => y2
y1 := <<1 + y2>>
------------------------
length 2;3;nil => y1
-------------------------------
length 1;2;3;nil => <<1 + y1>>
\end{lstlisting}

We can now fold back the results, just like a stack unwinding in a series of recursive calls. The first unwinding step yields:

\begin{lstlisting}
length nil => 0
y2 := <<1 + 0>>
------------------------
length 3;nil => y2
y1 := <<1 + y2>>
------------------------
length 2;3;nil => y1
-------------------------------
length 1;2;3;nil => <<1 + y1>>
\end{lstlisting}

We then proceed with the second unwinding step:

\begin{lstlisting}
------------------------
length 3;nil => 1
y1 := <<1 + 1>>
------------------------
length 2;3;nil => y1
-------------------------------
length 1;2;3;nil => <<1 + y1>>
\end{lstlisting}

And so on, until we reach the final expected answer which was \texttt{length 1;2;3;nil => 3}.

\paragraph{Searching}
A slightly more complex operation that we can perform is searching. Search determines whether or not a list \texttt{contains} an element. We define the \texttt{contains} function that takes a list and an integer as parameters:

\begin{lstlisting}
Func "contains" [ListInt <<int>>] Priority 100 Type Expr => <<int>>
\end{lstlisting}

The simplest test we perform is on an empty list. An empty list \texttt{nil} never contains a value \texttt{k}, independently of its value. The \texttt{contains} function in this case will thus return \texttt{no}\footnote{Assume we have defined symbols \texttt{yes} and \texttt{no}, but of course as a sane alternative boolean values can be used.} directly:

\begin{lstlisting}
---------------------
nil contains k => no
\end{lstlisting}

If the input list is not empty, then it means that it is not \texttt{nil}, and therefore it is made up of an element \texttt{x} followed by the rest of the list \texttt{xs}: \texttt{x;xs}. In this case, we check to see whether or not the head of the list is equal to the search element (the single premise \texttt{x == k}). If the premise is positively discharged (it is true) then we can return \texttt{yes} as the final result:

\begin{lstlisting}
x == k
-----------------------
x;xs contains k => yes
\end{lstlisting}

If the input list is not empty, but the first element is not equal to the searched key, then we need to keep searching. The result of the overall search will therefore be the result of searching in the rest of the list \texttt{xs} with the premise \texttt{xs contains k => res}:

\begin{lstlisting}
x != k
xs contains k => res
-----------------------
x;xs contains k => res
\end{lstlisting}

\paragraph{Transforming}
Consider now the problem of transforming a list by incrementing all of its elements by \texttt{k}. This example is interesting because it shows how a new list can be rebuilt and returned, instead of a single value like we did in the previous examples.

We begin by deifning the \texttt{plus} function which takes as input a list and an integer:

\begin{lstlisting}
Func [] "plus" [ListInt <<int>>] Priority 100 Type Expr => ListInt
\end{lstlisting}

The transformation of an empty list remains an empty list, as there are no elements to transform:

\begin{lstlisting}
-------------------
plus nil k => nil
\end{lstlisting}

The transform of the non-empty list proceeds first with the transformation of the rest of the list into \texttt{xs'}, then it computes \texttt{x+k} (the transformed head) into \texttt{x'}, and finally it returns the list \texttt{x';xs'} as the whole transformed list:

\begin{lstlisting}
plus xs k => xs'
<<x+k>> => x'
----------------------
plus x;xs k => x';xs'
\end{lstlisting}

Many useful functions on lists take the general form above: an immediate (often trivial) answer for the case of an empty list, and a recursive step which somehow combines premises on the first element with premises on the remaining elements. In a sense, lists are the embodiement of the simplest principles of recursion.

\paragraph{Sorting}
Let us now consider a slightly more complex algorithm, which allows us to test the algorithmic expressive power of the language. In particular, let us consider a powerful and fast sorting algorithm, the well-known \textit{merge-sort}. Merge sort works by, recursively, dividing the list of elements into two sublists, sorting them separately, and then merging the two sorted lists into a single, final sorted list.

We need two auxiliary symbol definitions: one is the comma, which will be used to represent a pair of lists, and the other is the \texttt{mergeSort} function itself:

\begin{lstlisting}
Data [] [ListInt] "," [ListInt] Priority 900 Type ListIntPair
Func [] "mergeSort" [ListInt] Priority 100 Type Expr => ListInt
\end{lstlisting}

The definition of \texttt{mergeSort} then becomes quite simple. If the list is empty, or contains only one element, then no sorting needs to take place and we can immediately return the sorted list as an answer:

\begin{lstlisting}
---------------------
mergeSort nil => nil

-------------------------
mergeSort x;nil => x;nil
\end{lstlisting}

If the list contains at least two elements, then:
\begin{inparaenum}[\itshape i\upshape)]
\item we split it into two sub-lists \texttt{l} and \texttt{r};
\item we sort \texttt{l} into \texttt{l'} with a recursive call;
\item we sort \texttt{r} into \texttt{r'} with a recursive call;
\item we merge \texttt{l'} and \texttt{r'}, therefore obtaining the final sorted list.
\end{inparaenum}

\begin{lstlisting}
split x;y;xs => l,r
mergeSort l => l'
mergeSort r => r'
merge l' r' => res
------------------------
mergeSort x;y;xs => res
\end{lstlisting}

Splitting a list is done with function \texttt{split}:

\begin{lstlisting}
Func [] "split" [ListInt] Priority 100 Type Expr => ListIntPair
\end{lstlisting}

If the list to split has less than two elements, then splitting is trivial:

\begin{lstlisting}
---------------------
split nil => nil,nil

-------------------------
split x;nil => x;nil,nil
\end{lstlisting}

If the list to split has at least two elements \texttt{x} and \texttt{y}, then:
\begin{inparaenum}[\itshape i\upshape)]
\item we split the remaining list recursively, obtaining partial splits \texttt{l} and \texttt{r};
\item list \texttt{x;l} is the first item of the result;
\item list \texttt{y;r} is the second item of the result.
\end{inparaenum}

\begin{lstlisting}
split xs => l,r
----------------------------
split x;y;xs => (x;l),(y;r)
\end{lstlisting}

Let us consider a short example of how split works on a list such as:

\begin{lstlisting}
-------------------------
split 0;1;2;3;4;nil => ?
\end{lstlisting}

Since the list contains at least two elements, we apply the recursive rule for \texttt{x=0}, \texttt{y=1}, and \texttt{xs=2;3;4;nil}:

\begin{lstlisting}
split 2;3;4;nil => l,r
-----------------------------------
split 0;1;2;3;4;nil => (0;l),(1;r)
\end{lstlisting}

We apply the same rule again because \texttt{2;3;4;nil} has at least two elements, thus:

\begin{lstlisting}
split 4;nil => l',r'
l := 2;l'
r := 3;r'
-----------------------
split 2;3;4;nil => l,r
-----------------------------------
split 0;1;2;3;4;nil => (0;l),(1;r)
\end{lstlisting}

We end up with list \texttt{4;nil}, which has only one element. This requires us to apply one of the trivial rules above:

\begin{lstlisting}
l' := 4;nil
r' := nil
--------------------
split 4;nil => l',r'
l := 2;l'
r := 3;r'
-----------------------
split 2;3;4;nil => l,r
-----------------------------------
split 0;1;2;3;4;nil => (0;l),(1;r)
\end{lstlisting}

We can now unwind the stack of intermediate results. The first unwind step yields:

\begin{lstlisting}
l := 2;4;nil
r := 3;nil
-----------------------
split 2;3;4;nil => l,r
-----------------------------------
split 0;1;2;3;4;nil => (0;l),(1;r)
\end{lstlisting}

The second and last unwind step returns the final result:

\begin{lstlisting}
---------------------------------------------
split 0;1;2;3;4;nil => (0;2;4;nil),(1;3;nil)
\end{lstlisting}

The role of \texttt{split} is thus to split the list in two, but instead of taking the first half and then the second half of the list, we \textit{interleave} the elements: elements in odd positions go in the first sublist, elements in even positions go in the second sublist.

Merging two lists requires a bit of care, because we take as input two sorted lists and we must give as output a single list which contains all elements of the two lists to merge \textit{in sorted order}. Merging is done with function \texttt{merge}:

\begin{lstlisting}
Func [] "merge" [ListInt ListInt] Priority 100 Type Expr => ListInt
\end{lstlisting}

If any of the input lists to merge is empty, then the answer is trivial to find and return:

\begin{lstlisting}
---------------------
merge nil nil => nil

-----------------------
merge x;xs nil => x;xs

-----------------------
merge nil y;ys => y;ys
\end{lstlisting}

If the first list begins with an element that is smaller than the first element of the second list, then the resulting list will need to start with that element (and vice-versa):

\begin{lstlisting}
x <= y
merge xs y;ys => res
-------------------------
merge x;xs y;ys => x;res

x > y
merge x;xs ys => res
-----------------------
merge x;xs y;ys => y;res
\end{lstlisting}

Consider now the merging of two partial sorted lists \texttt{0;2;nil} and \texttt{1;3;nil} (the lists do not need to be of the same length, nor do the elements need to interleave so strictly):

\begin{lstlisting}
---------------------------
merge 0;2;nil 1;3;nil => ?
\end{lstlisting}

The first element of the first list is smaller than the first element of the second list, thus it will become the first element of the result:

\begin{lstlisting}
merge 2;nil 1;3;nil => res
-------------------------------
merge 0;2;nil 1;3;nil => 0;res
\end{lstlisting}

The first element of the second list is smaller than the first element of the first list, thus it will be selected as the first element of the (intermediate) result:

\begin{lstlisting}
--------------------------
merge 2;nil 3;nil => res'
res := 1;res'
---------------------------
merge 2;nil 1;3;nil => res
-------------------------------
merge 0;2;nil 1;3;nil => 0;res
\end{lstlisting}

We now select \texttt{2} as the new head of the intermediate result:

\begin{lstlisting}
merge nil 3;nil => res''
res' := 2;res''
--------------------------
merge 2;nil 3;nil => res'
res := 1;res'
---------------------------
merge 2;nil 1;3;nil => res
-------------------------------
merge 0;2;nil 1;3;nil => 0;res
\end{lstlisting}

Finally, since the first list is empty, we directly return the second:

\begin{lstlisting}
res'' := 3;nil
-------------------------
merge nil 3;nil => res''
res' := 2;res''
--------------------------
merge 2;nil 3;nil => res'
res := 1;res'
---------------------------
merge 2;nil 1;3;nil => res
-------------------------------
merge 0;2;nil 1;3;nil => 0;res
\end{lstlisting}

Since we have no more intermediate results to process, we can now begin the unwinding. First of all \texttt{res''} is replaced with \texttt{3;nil}:

\begin{lstlisting}
res' := 2;3;nil
--------------------------
merge 2;nil 3;nil => res'
res := 1;res'
---------------------------
merge 2;nil 1;3;nil => res
-------------------------------
merge 0;2;nil 1;3;nil => 0;res
\end{lstlisting}

Then we replace \texttt{res'} with its value of \texttt{2;3;nil}:

\begin{lstlisting}
res := 1;2;3;nil
---------------------------
merge 2;nil 1;3;nil => res
-------------------------------
merge 0;2;nil 1;3;nil => 0;res
\end{lstlisting}

Finally we replace \texttt{res}, therefore obtaining the final answer:

\begin{lstlisting}
merge 0;2;nil 1;3;nil => 0;1;2;3;nil
\end{lstlisting}

At this point we can better discuss what \texttt{mergeSort} does, without doing the full derivation which would take too long. Suppose we wished to sort sequence \texttt{5;4;3;2;1;nil}:

\begin{lstlisting}
split 5;4;3;2;1;nil => l,r
mergeSort l => l'
mergeSort r => r'
merge l' r' => res
------------------------
mergeSort 5;4;3;2;1;nil => res
\end{lstlisting}

Without going (again) into the details of \texttt{split}, we can just assume it will work as expected and obtain:

\begin{lstlisting}
split 5;4;3;2;1;nil => (5;3;1;nil),(4;2;nil)
mergeSort 5;3;1;nil => l'
mergeSort 4;2;nil => r'
merge l' r' => res
------------------------
mergeSort 5;4;3;2;1;nil => res
\end{lstlisting}

At this point we make a strong assumption, which we can safely make because of the principle of induction, that is we assume that \texttt{mergeSort 5;3;1;nil} and \texttt{mergeSort 4;2;nil} will both return a sorted list. The next step is therefore:

\begin{lstlisting}
mergeSort 5;3;1;nil => 1;3;5;nil
mergeSort 4;2;nil => 2;4;nil
merge 1;3;5;nil 2;4;nil => res
-------------------------------
mergeSort 5;4;3;2;1;nil => res
\end{lstlisting}

Without going (again) into the details of \texttt{merge}, we can just assume it will work as expected and obtain:

\begin{lstlisting}
merge 1;3;5;nil 2;4;nil => 1;2;3;4;5;nil
-----------------------------------------
mergeSort 5;4;3;2;1;nil => 1;2;3;4;5;nil
\end{lstlisting}

which is precisely the expected result.

\paragraph{Conclusions}
What we have seen so far extends (with more or less translation work depending on the circumstances) to all known algorithms on lists. It is possible to write algorithms such as insertion sort, various kinds of search, and so on with minimal adjustments. In the coming sections we will dig yet deeper into recursive data structures such as binary trees.


\chapter{Fourier Series}
\section{Series Expansion of Periodic Functions}
The basic fact about Fourier series is as follows.\\
If $f(x)$ is a periodic function of $x$,with a period $L$ . The \textbf{Fundamental interval} is taken to be $(a,b)$, so that $L=(b-a)$\\
Then, $f(x)=f(x+L)=f(x+nL)$ Where $n$ is any integer.\\If the function $f(x)$ also satisfies a set of conditions known as \textbf{Drichlet's  Conditions} given as below\\

\begin{enumerate}
	\item It is single-valued in the interval $(a, b)$
	\item It is bounded in the interval $(a, b)$
	\item It has at most a finite number of maxima and minima in the interval $(a, b)$
	\item It has only a finite number of discontinuity in the interval $(a, b)$
\end{enumerate}
\begin{theorem}\label{geneq}
	Then such a function can be expressed by a sum of a set  sine and cosine functions (or complex exponentials). This series is known as \textbf{Fourier Series Expansion}\index{Fourier Series}.\\
	\begin{equation}
	\begin{array}{c}
	f(x)=\frac{a_{0}}{2}+\sum_{n=1}^{\infty} a_{n} \cos \frac{2n \pi x}{L}+\sum_{n=1}^{\infty} b_{n} \sin \frac{2n \pi x}{L}
	\end{array}
	\end{equation}

	$a_{0}, a_{n}, b_{n}$ are constants, known as Fourier co-efficients.
	\begin{equation}
	a_{0}=\frac{2}{b-a} \int_{a}^{b} f(x) d x
	\end{equation}
	\begin{equation}
	a_{n}=\frac{2}{b-a} \int_{a}^{b} f(x) \cdot \cos \left(\frac{2 n \pi x}{b-a}\right) d x
	\end{equation}
	\begin{equation}
	b_{n}=\frac{2}{b-a} \int_{a}^{b} f(x) \cdot \sin \left(\frac{2 n \pi x}{b-a}\right) d x
	\end{equation}
\end{theorem}
The Fourier expansion of a periodic function $f(x)$ consists of the following term
\begin{enumerate}
	\item $a_{0}: a$ constant term or $D .$ C. term
	\item $a_{1} \cos \frac{\pi x}{L}$ and $b_{1} \sin \frac{\pi x}{L}:$ Fundamental frequency term i.e term having lowest frequency
	\item $a_{2} \cos \frac{2 \pi x}{L}, a_{3} \cos \frac{3 \pi x}{L} \ldots \ldots ., b_{2} \sin \frac{2 \pi x}{L}, b_{3} \sin \frac{3 \pi x}{L} \ldots \ldots .$ Overtones i.e terms having
	frequencies integer multiple of fundamental frequency term.
\end{enumerate}

\subsection{Simplified formulas when $b-a=2\pi$}

We have established the Euler formulas for a function having period $L$ and principal interval $(a,b)$.The formulas connected with fourier series expansion look a bit simpler if one choose the fundamental interval to be $(-\pi,\pi)$ or $(0,2\pi)$, ie Period $L=2\pi$.\\For the case $b-a=2\pi$, the Euler formulas become

\begin{theorem}\label{pareq}

\begin{equation}
a_{0}=\frac{1}{\pi} \int_{a}^{b} f(x) d x
\end{equation}
\begin{equation}
a_{0n}=\frac{1}{\pi} \int_{a}^{b} f(x) \cdot \cos n x d x
\end{equation}
\begin{equation}
b_{n}=\frac{1}{\pi} \int_{a}^{b} f(x) \cdot \sin n x d x
\end{equation}
So that the Fourier expansion becomes
\begin{equation}
f(x)=\frac{a_{0}}{2}+\sum_{n=1}^{\infty} a_{n} \cdot \cos n x+\sum_{n=1}^{\infty} b_{n} \sin n x
\end{equation}
\end{theorem}

\begin{exercise}
	 Find the Fourier series representing $$f(x)=x, \quad 0<x<2 \pi$$
\end{exercise}
\begin{answer}
Here the interval is $(0,2\pi)$ So L=$2\pi$.From theorem \ref{pareq} we have
\begin{equation}
\label{eq1}
f(x)=\frac{a_{0}}{2}+\sum_{n=1}^{\infty} a_{n} \cdot \cos n x+\sum_{n=1}^{\infty} b_{n} \sin n x
\end{equation}
$$h
\text { Hence } \begin{aligned}
a_{0} &=\frac{1}{\pi} \int_{0}^{2 \pi} f(x) d x=\frac{1}{\pi} \int_{0}^{2 \pi} x d x=\frac{1}{\pi}\left[\frac{x^{2}}{2}\right]_{0}^{2 \pi}=2 \pi \\
a_{n} &=\frac{1}{\pi} \int_{0}^{2 \pi} f(x) \cos n x d x=\frac{1}{\pi} \int_{0}^{2 \pi} x \cos n x d x \\
&=\frac{1}{\pi}\left[x \frac{\sin n x}{n}-1 \cdot\left(-\frac{\cos n x}{n^{2}}\right)\right]_{0}^{2 \pi}=\frac{1}{\pi}\left[\frac{\cos 2 n \pi}{n^{2}}-\frac{1}{n^{2}}\right]=\frac{1}{n^{2} \pi}(1-1)=0 \\
b_{n} &=\frac{1}{\pi} \int_{0}^{2 \pi} f(x) \sin n x d x=\frac{1}{\pi} \int_{0}^{2 \pi} x \sin n x d x \\
&=\frac{1}{\pi}\left[x\left(-\frac{\cos n x}{n}\right)-1 \cdot\left(\frac{-\sin n x}{n^{2}}\right)\right]_{0}^{2 \pi}=\frac{1}{\pi}\left[\frac{-2 \pi \cos 2 n \pi}{n}\right]=-\frac{2}{n}
\end{aligned}
$$
Substituting the values of $a_{0}, a_{1}, a_{2} \ldots, b_{1}, b_{2} \ldots$ in Equation \ref{eq1}, we get
$$
x=\pi-2\left[\sin x+\frac{1}{2} \sin 2 x+\frac{1}{3} \sin 3 x+\ldots\right]
$$
\end{answer}
\begin{exercise}
Given that $f(x)=x+x^{2}$ for $-\pi<x<\pi,$ find the Fourier expression of $f(x)$
\end{exercise}

\begin{answer}
	\begin{equation}
\label{eq2}
	\text { Let } x+x^{2}=\frac{a_{0}}{2}+a_{1} \cos x+a_{2} \cos 2 x+\ldots+b_{1} \sin x+b_{2} \sin 2 x+\ldots
	\end{equation}
	$\begin{aligned} a_{0} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) d x=\frac{1}{\pi} \int_{-\pi}^{\pi}\left(x+x^{2}\right) d x \\ &=\frac{1}{\pi}\left[\frac{x^{2}}{2}+\frac{x^{3}}{3}\right]_{-\pi}^{\pi}=\frac{1}{\pi}\left[\frac{\pi^{2}}{2}+\frac{\pi^{3}}{3}-\frac{\pi^{2}}{2}+\frac{\pi^{3}}{3}\right]=\frac{2 \pi^{2}}{3} \\ a_{n} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \cos n x d x=\int_{-\pi}^{\pi}\left(x+x^{2}\right) \cos n x d x \end{aligned}$\\
	$\begin{aligned} &=\frac{1}{\pi}\left[\left(x+x^{2}\right) \frac{\sin n x}{n}-(2 x+1) \frac{(-\cos n x)}{n^{2}}+(2)\left(-\frac{\sin n x}{n^{3}}\right)\right]_{-\pi}^{\pi} \\ &=\frac{1}{\pi}\left[(2 \pi+1) \frac{\cos n \pi}{n^{2}}-(-2 \pi+1) \frac{\cos (-n \pi)}{n^{2}}\right]=\frac{1}{\pi}\left[4 \pi \frac{\cos n \pi}{n^{2}}\right]=\frac{4(-1)^{n}}{n^{2}} \\ b_{n} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \sin n x d x=\frac{1}{\pi} \int_{-\pi}^{\pi}\left(x+x^{2}\right) \sin n x d x \\ &=\frac{1}{\pi}\left[\left(x+x^{2}\right)\left(-\frac{\cos n x}{n}\right)-(2 x+1)\left(\frac{-\sin n x}{n^{2}}\right)+2 \frac{\cos n x}{n^{3}}\right]_{-\pi}^{\pi} \\ &=\frac{1}{\pi}\left[-\left(\pi+\pi^{2}\right) \frac{\cos n \pi}{n}+2 \frac{\cos n \pi}{n^{3}}+\left(-\pi+\pi^{2}\right) \frac{\cos n \pi}{n}-2 \frac{\cos n \pi}{n^{3}}\right] \\ &=\frac{1}{\pi}\left[-\frac{2 \pi}{n} \cos n \pi\right]=-\frac{2}{n}(-1)^{n} \end{aligned}$\\
	Substituting the values of $a_{0}, a_{n}, b_{n},$ in Equation \ref{eq2}, we get
	\begin{equation}
	\begin{array}{r}
	x+x^{2}=\frac{\pi^{2}}{3}+4\left[-\cos x+\frac{1}{2^{2}} \cos 2 x-\frac{1}{3^{2}} \cos 3 x+\ldots\right]
	-2\left[-\sin x+\frac{1}{2} \sin 2 x-\frac{1}{3} \sin 3 x+\ldots\right]
	\end{array}
	\end{equation}
\end{answer}
\section{Even and Odd Functions}
\subsection{Even Function}
A function $f(x)$ is said to be \textbf{even} (or \textbf{symmetric}) function if, $f(-x)=f(x)$ The graph of such a function is symmetrical with respect to $y$ -axis . Here $y$ -axis is a mirror for the reflection of the curve.
\begin{example}
	$\cos x,\sec x ,x^2$ etc

\end{example}
The area under such a curve from $-\pi$ to $\pi$ is double the area from 0 to $\pi$, ie\\
$$\quad \int_{-\pi}^{\pi} f(x) d x=2 \int_{0}^{\pi} f(x) d x$$
\subsection{Odd Function}
A function $f(x)$ is called \textbf{odd} (or \textbf{skew symmetric}) function if
$$
f(-x)=-f(x)
$$ Odd functions will be symmetric about origin.\\
Here the area under the curve from $-\pi$ to $\pi$ is zero.
$$
\int_{-\pi}^{\pi} f(x) d x=0
$$

\begin{note}
	\[
	\boxed{
		\!\begin{aligned}
		&Even \times Even= Odd \times Odd = Even\\
		&Odd \times Even= Even \times Odd = Odd
		\end{aligned}
	}
	\]
\end{note}
\section{Fourier Expansion of Even and Odd Functions}
\subsection{Even Function}
$$
a_{0}=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) d x=\frac{2}{\pi} \int_{0}^{\pi} f(x) d x
$$
$$
a_{n}=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \cos n x d x=\frac{2}{\pi} \int_{0}^{\pi} f(x) \cos n x d x
$$
As $f(x)$ and $\cos n x$ are both even functions, therefore, the product of $f(x) . \cos n x$ is also an even function.
$$
b_{n}=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \sin n x d x=0
$$
\textbf{So if $f(x)$ is an even function then Fourier coefficient $b_n$ will be zero in the expansion. }\\
The series of the even function will contain only cosine and DC terms.
\begin{exercise}
	Find the Fourier series expansion of the periodic function of period $2 \pi$
	$$
	f(x)=x^{2},-\pi \leq x \leq \pi
	$$
\end{exercise}
\begin{answer}
	This is an even function. $\therefore b_{n}=0$
	$$
	\begin{aligned}
	b_{n} &=0 \quad[f(-x)=f(x)] \\
	a_{0} &=\frac{2}{\pi} \int_{0}^{\pi} f(x) d x=\frac{2}{\pi} \int_{0}^{\pi} x^{2} d x=\frac{2}{\pi}\left[\frac{x^{3}}{3}\right]_{0}^{\pi}=\frac{2 \pi^{2}}{3} \\
	a_{n} &=\frac{2}{\pi} \int_{0}^{\pi} f(x) \cos n x d x=\frac{2}{\pi} \int_{0}^{\pi} x^{2} \cos n x d x \\
	&=\frac{2}{\pi}\left[x^{2}\left(\frac{\sin n x}{n}\right)-(2 x)\left(-\frac{\cos n x}{n^{2}}\right)+(2)\left(-\frac{\sin n x}{n^{3}}\right)\right]_{0}^{\pi} \\
	=& \frac{2}{\pi}\left[\frac{\pi^{2} \sin n \pi}{n}+\frac{2 \pi \cos n \pi}{n^{2}}-\frac{2 \sin n \pi}{n^{3}}\right]=\frac{4(-1)^{n}}{n^{2}}
	\end{aligned}
	$$
	$\begin{aligned} \text { Fourier series is } & f(x)=\frac{a_{0}}{2}+a_{1} \cos x+a_{2} \cos 2 x+a_{3} \cos 3 x+\ldots+a_{n} \cos n x+\ldots \\ x^{2} &=\frac{\pi^{2}}{3}-4\left[\frac{\cos x}{1^{2}}-\frac{\cos 2 x}{2^{2}}+\frac{\cos 3 x}{3^{2}}-\frac{\cos 4 x}{4^{2}}+\ldots\right] \end{aligned}$
\end{answer}
\subsection{Odd Function}
$$\begin{aligned}
	&a_{0}=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) d x=0\\
	&a_{n}=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \cos n x d x=0
\end{aligned}$$
(Since $f(x)$ is an odd function and $cos(x)$ is an even function their product will be odd and the integral vanishes)
$$\begin{aligned}
b_{n}&=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \sin n x d x\\
\therefore b_{n}&=\frac{2}{\pi} \int_{0}^{\pi} f(x) \sin n x d x
\end{aligned}$$
\textbf{So if $f(x)$ is an odd function then Fourier coefficient $a_0$ and $a_n$ will be zero in the expansion. }\\
The series of the odd function will contain only sine  terms.

\begin{exercise}
	Obtain a Fourier expression for
	$$
	f(x)=x^{3} \quad \text { for }-\pi<x<\pi
	$$
\end{exercise}
\begin{answer}
	$f(x)=x^{3}$ is an odd function.
	$$
	\begin{aligned}
	\therefore \quad a_{0} & =0 \text { and } a_{n}=0 \\
	\text{ Use tabular integration }\\
	b_{n} & =\frac{2}{\pi} \int_{0}^{\pi} f(x) \sin n x d x=\frac{2}{\pi} \int_{0}^{\pi} x^{3} \sin n x d x \\
	& =\frac{2}{\pi}\left[x^{3}\left(-\frac{\cos n x}{n}\right)-3 x^{2}\left(-\frac{\sin n x}{n^{2}}\right)+6 x\left(\frac{\cos n x}{n^{3}}\right)-6\left(\frac{\sin n x}{n^{4}}\right)\right]_{0}^{\pi} \\
	& =\frac{2}{\pi}\left[-\frac{\pi^{3} \cos n \pi}{n}+\frac{6 \pi \cos n \pi}{n^{3}}\right]=2 \cdot(-1)^{n}\left[-\frac{\pi^{2}}{n}+\frac{6}{n^{3}}\right] \\
 \text{ We have }f(x)&=b_{1} \sin x+b_{2} \sin 2 x+b_{3} \sin 3 x+\ldots \ldots \\
	 x^{3}&=2\left[-\left(-\frac{\pi^{2}}{1}+\frac{6}{1^{3}}\right) \sin x+\left(-\frac{\pi^{2}}{2}+\frac{6}{2^{3}}\right) \sin 2 x-\left(-\frac{\pi^{2}}{3}+\frac{6}{3^{3}}\right) \sin 3 x+\ldots\right]
	\end{aligned}
	$$
\end{answer}

\newpage
\begin{abox}
	Practise Set-1
\end{abox}
\begin{enumerate}[label=\color{ocre}\textbf{\arabic*.}]
	\item   The first few terms in the Laurent series for $\frac{1}{(z-1)(z-2)}$ in the region $1 \leq|z| \leq 2$ and around $z=1$ is
	{\exyear{NET/JRF(JUNE-2012)}}
	\begin{tasks}(1)
		\task[\textbf{A.}] $\frac{1}{2}\left[1+z+z^{2}+\ldots\right]\left[1+\frac{z}{2}+\frac{z^{2}}{4}+\frac{z^{3}}{8}+\ldots .\right]$
		\task[\textbf{B.}] $\frac{1}{1-z}-z-(1-z)^{2}+(1-z)^{3}+\ldots .$
		\task[\textbf{C.}] $\frac{1}{\mathrm{z}^{2}}\left[1+\frac{1}{\mathrm{z}}+\frac{1}{\mathrm{z}^{2}}+\ldots .\right]\left[1+\frac{2}{\mathrm{z}}+\frac{4}{\mathrm{z}^{2}}+\ldots . .\right]$
		\task[\textbf{D.}]  $2(z-1)+5(z-1)^{2}+7(z-1)^{3}+\ldots$
	\end{tasks}
	\item Consider a sinusoidal waveform of amplitude $1 V$ and frequency $f_{0}$. Starting from an arbitrary initial time, the waveform is sampled at intervals of $\frac{1}{2 f_{0}}$. If the corresponding Fourier spectrum peaks at a frequency $\bar{f}$ and an amplitude $\bar{A}$, them
	{\exyear{NET/JRF(JUNE-2012)}}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\bar{f}=2 f_{0}$ and $\bar{A}=1 V$
		\task[\textbf{B.}] $\bar{f}=2 f_{0}$ and $0 \leq \bar{A} \leq 1 V$
		\task[\textbf{C.}] $\bar{f}=0$ and $\bar{A}=1 V$
		\task[\textbf{D.}] $\bar{f}=\frac{f_{0}}{2}$ and $\bar{A}=\frac{1}{\sqrt{2}} V$
	\end{tasks}
	\item The Fourier transform of the derivative of the Dirac $\delta-$ function, namely $\delta^{\prime}(x)$, is proportional to
	{\exyear{NET/JRF(DEC-2013)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] 0
		\task[\textbf{B.}] 1
		\task[\textbf{C.}] $\sin k$
		\task[\textbf{D.}] $i k$
	\end{tasks}
	\item The Laplace transform of $6 t^{3}+3 \sin 4 t$ is
	{\exyear{NET/JRF(JUNE-2015)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{36}{s^{4}}+\frac{12}{s^{2}+16}$
		\task[\textbf{B.}] $\frac{36}{s^{4}}+\frac{12}{s^{2}-16}$
		\task[\textbf{C.}] $\frac{18}{s^{4}}+\frac{12}{s^{2}-16}$
		\task[\textbf{D.}] $\frac{36}{s^{3}}+\frac{12}{s^{2}+16}$
	\end{tasks}
	\item  The Fourier transform of $f(x)$ is $\tilde{f}(k)=\int_{-\infty}^{+\infty} d x e^{i k x} f(x)$.
	If $f(x)=\alpha \delta(x)+\beta \delta^{\prime}(x)+\gamma \delta^{\prime \prime}(x)$, where $\delta(x)$ is the Dirac delta-function (and prime denotes derivative), what is $\tilde{f}(k) ?$
	{\exyear{NET/JRF(DEC-2015)}}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\alpha+i \beta k+i \gamma k^{2}$
		\task[\textbf{B.}] $\alpha+\beta k-\gamma k^{2}$
		\task[\textbf{C.}]  $\alpha-i \beta k-\gamma k^{2}$
		\task[\textbf{D.}] $i \alpha+\beta k-i \gamma k^{2}$
	\end{tasks}
	\item  What is the Fourier transform $\int d x e^{i l x} f(x)$ of
	$$
	f(x)=\delta(x)+\sum_{n=1}^{\infty} \frac{d^{n}}{d x^{n}} \delta(x)
	$$
	where $\delta(x)$ is the Dirac delta-function?
	{\exyear{NET/JRF(JUNE-2016)}}
	\begin{tasks}(4)
		\task[\textbf{A.}]  $\frac{1}{1-i k}$
		\task[\textbf{B.}] $\frac{1}{1+i k}$
		\task[\textbf{C.}] $\frac{1}{k+i}$
		\task[\textbf{D.}] $\frac{1}{k-i}$
	\end{tasks}
	\item The Laplace transform of
	$$
	f(t)=\left\{\begin{array}{cc}
	\frac{t}{T}, & 0<t<T \\
	1 & t>T
	\end{array}\right.
	$$
	is
	{\exyear{NET/JRF(DEC-2016)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{-\left(1-e^{-s T}\right)}{s^{2} T}$
		\task[\textbf{B.}] $\frac{\left(1-e^{-s T}\right)}{s^{2} T}$
		\task[\textbf{C.}] $\frac{\left(1+e^{-s T}\right)}{s^{2} T}$
		\task[\textbf{D.}] $\frac{\left(1-e^{s T}\right)}{s^{2} T}$
	\end{tasks}
	\item The Fourier transform $\int_{-\infty}^{\infty} d x f(x) e^{i k x}$ of the function $f(x)=\frac{1}{x^{2}+2}$ is
	{\exyear{NET/JRF(DEC-2016)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\sqrt{2} \pi e^{-\sqrt{2}|| \mid}$
		\task[\textbf{B.}] $\sqrt{2} \pi e^{-\sqrt{2 k}}$
		\task[\textbf{C.}] $\frac{\pi}{\sqrt{2}} e^{-\sqrt{2 k}}$
		\task[\textbf{D.}] $\frac{\pi}{\sqrt{2}} e^{-\sqrt{2}|k|}$
	\end{tasks}
	\item Consider the differential equation $\frac{d y}{d t}+a y=e^{-b t}$ with the initial condition $y(0)=0$. Then the Laplace transform $Y(s)$ of the solution $y(t)$ is
	{\exyear{NET/JRF(DEC-2017)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{1}{(s+a)(s+b)}$
		\task[\textbf{B.}] $\frac{1}{b(s+a)}$
		\task[\textbf{C.}] $\frac{1}{a(s+b)}$
		\task[\textbf{D.}] $\frac{e^{-a}-e^{-b}}{b-a}$
	\end{tasks}
	\item  The Fourier transform $\int_{-\infty}^{\infty} d x f(x) e^{i k x}$ of the function $f(x)=e^{-|x|}$
	{\exyear{NET/JRF(JUNE-2018)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $-\frac{2}{1+k^{2}}$
		\task[\textbf{B.}] $-\frac{1}{2\left(1+k^{2}\right)}$
		\task[\textbf{C.}] $\frac{2}{1+k^{2}}$
		\task[\textbf{D.}] $\frac{2}{\left(2+k^{2}\right)}$
	\end{tasks}
	\item The function $f(t)$ is a periodic function of period $2 \pi$. In the range $(-\pi, \pi)$, it equals $e^{-t}$. If $f(t)=\sum_{-\infty}^{\infty} c_{n} e^{\text {int }}$ denotes its Fourier series expansion, the sum $\sum_{-\infty}^{\infty}\left|c_{n}\right|^{2}$ is
	{\exyear{NET/JRF(DEC-2019)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] 1
		\task[\textbf{B.}] $\frac{1}{2 \pi}$
		\task[\textbf{C.}] $\frac{1}{2 \pi} \cosh (2 \pi)$
		\task[\textbf{D.}]  $\frac{1}{2 \pi} \sinh (2 \pi)$
	\end{tasks}
	\end{enumerate}
 \colorlet{ocre1}{ocre!70!}
\colorlet{ocrel}{ocre!30!}
\setlength\arrayrulewidth{1pt}
\begin{table}[H]
	\centering
	\arrayrulecolor{ocre}
	\begin{tabular}{|p{1.5cm}|p{1.5cm}||p{1.5cm}|p{1.5cm}|}
		\hline
		\multicolumn{4}{|c|}{\textbf{Answer key}}\\\hline\hline
		\rowcolor{ocrel}Q.No.&Answer&Q.No.&Answer\\\hline
		1&\textbf{B} &2&\textbf{B}\\\hline
		3&\textbf{D} &4&\textbf{A} \\\hline
		5&\textbf{C} &6&\textbf{B} \\\hline
		7&\textbf{B}&8&\textbf{D}\\\hline
		9&\textbf{A}&10&\textbf{C}\\\hline
		11&\textbf{D} &&\textbf{}\\\hline

	\end{tabular}
\end{table}

\newpage
\begin{abox}
	Practise Set-2
\end{abox}
\begin{enumerate}[label=\color{ocre}\textbf{\arabic*.}]
\item If $f(x)=\left\{\begin{array}{ll}0 & \text { for } x<3, \\ x-3 & \text { for } x \geq 3\end{array}\right.$ then the Laplace transform of $f(x)$ is
{\exyear{GATE 2010}}
\begin{tasks}(4)
	\task[\textbf{A.}] $s^{-2} e^{3 s}$
	\task[\textbf{B.}] $s^{2} e^{3 s}$
	\task[\textbf{C.}] $s^{-2}$
	\task[\textbf{D.}] $s^{-2} e^{-3 s}$
\end{tasks}
\item The coefficient of $e^{i k x}$ in the Fourier expansion of $u(x)=A \sin ^{2}(\alpha x)$ for $k=-2 \alpha$ is
{\exyear{GATE 2017}}
\begin{tasks}(4)
	\task[\textbf{A.}] $\frac{A}{4}$
	\task[\textbf{B.}] $\frac{-A}{4}$
	\task[\textbf{C.}] $\frac{A}{2}$
	\task[\textbf{D.}] $\frac{-A}{2}$
\end{tasks}
\item Given the fundamental constants $\hbar$ (Planck's constant), $G$ (universal gravitation constant) and $c$ (speed of light), which of the following has dimension of length?
{\exyear{JEST 2014}}
 \begin{tasks}(2)
	\task[\textbf{A.}]$\sqrt{\frac{\hbar G}{c^{3}}}$
	\task[\textbf{B.}] $\sqrt{\frac{\hbar G}{c^{5}}}$
	\task[\textbf{C.}]$\frac{\hbar G}{c^{3}}$
	\task[\textbf{D.}] $\sqrt{\frac{\hbar c}{8 \pi G}}$
\end{tasks}
\item The Fourier transform of the function $\frac{1}{x^{4}+3 x^{2}+2}$ up to proportionality constant is
{\exyear{JEST 2017}}
 \begin{tasks}(2)
	\task[\textbf{A.}]$\sqrt{2} \exp \left(-k^{2}\right)-\exp \left(-2 k^{2}\right)$
	\task[\textbf{B.}]$\sqrt{2} \exp (-|k|)-\exp (-\sqrt{2}|k|)$
	\task[\textbf{C.}]$\sqrt{2} \exp (-\sqrt{|k|})-\exp (-\sqrt{2|k|})$
	\task[\textbf{D.}]  $\sqrt{2} \exp \left(-\sqrt{2} k^{2}\right)-\exp \left(-2 k^{2}\right)$
\end{tasks}
\item The function $f(x)=\cosh x$ which exists in the range $-\pi \leq x \leq \pi$ is periodically repeated between $x=(2 m-1) \pi$ and $(2 m+1) \pi$, where $m=-\infty$ to $\infty$. Using Fourier series, indicate the correct relation at $x=0$
{\exyear{JEST 2017}}
 \begin{tasks}(2)
	\task[\textbf{A.}] $\sum_{n=-\infty}^{\infty} \frac{(-1)^{n}}{1-n^{2}}=\frac{1}{2}\left(\frac{\pi}{\cosh \pi}-1\right)$
	\task[\textbf{B.}]$\sum_{n=-\infty}^{\infty} \frac{(-1)^{n}}{1-n^{2}}=2 \frac{\pi}{\cosh \pi}$
	\task[\textbf{C.}]$\sum_{n=-\infty}^{\infty} \frac{(-1)^{-n}}{1+n^{2}}=2 \frac{\pi}{\sinh \pi}$
	\task[\textbf{D.}] $\sum_{n=1}^{\infty} \frac{(-1)^{n}}{1+n^{2}}=\frac{1}{2}\left(\frac{\pi}{\sinh \pi}-1\right)$
\end{tasks}
\item The Laplace transform of $\frac{(\sin (a t)-a t \cos (a t))}{\left(2 a^{3}\right)}$ is
{\exyear{JEST 2018}}
 \begin{tasks}(2)
	\task[\textbf{A.}]$\frac{2 a s}{\left(s^{2}+a^{2}\right)^{2}}$
	\task[\textbf{B.}]$\frac{s^{2}-a^{2}}{\left(s^{2}+a^{2}\right)^{2}}$
	\task[\textbf{C.}]$\frac{1}{(s+a)^{2}}$
	\task[\textbf{D.}] $\frac{1}{\left(s^{2}+a^{2}\right)^{2}}$
\end{tasks}
\end{enumerate}
 \colorlet{ocre1}{ocre!70!}
\colorlet{ocrel}{ocre!30!}
\setlength\arrayrulewidth{1pt}
\begin{table}[H]
	\centering
	\arrayrulecolor{ocre}
	\begin{tabular}{|p{1.5cm}|p{1.5cm}||p{1.5cm}|p{1.5cm}|}
		\hline
		\multicolumn{4}{|c|}{\textbf{Answer key}}\\\hline\hline
		\rowcolor{ocrel}Q.No.&Answer&Q.No.&Answer\\\hline
		1&\textbf{D} &2&\textbf{B}\\\hline
		3&\textbf{A} &4&\textbf{B} \\\hline
		5&\textbf{D} &6&\textbf{D} \\\hline

	\end{tabular}
\end{table}
\newpage
\begin{abox}
	Practise Set-3
\end{abox}
\begin{enumerate}[label=\color{ocre}\textbf{\arabic*.}]
	\item  Find the fourier series to represent the function $f(x)$ given by
	$$
	f(x)=\left[\begin{array}{ll}
	-k & \text { for }-\pi<x<0 \\
	k & \text { for } 0<x<\pi
	\end{array}\right.
	$$
	hence show that $1-\frac{1}{3}+\frac{1}{5}+\frac{1}{7}+\ldots \ldots=\frac{\pi}{4}$
	\begin{answer}
		Given function is odd in nature, so $a_{0}=0$ and $a_{n}=0$

		\begin{align*}
		b_{n} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \sin n x d x=\frac{1}{\pi}\left[\int_{-\pi}^{0}-k \sin n x d x+\int_{0}^{\pi} k \sin n x d x\right] \\
		&=\frac{1}{\pi} k\left\lbrace \left[ \frac{\cos n x}{n}\right] _{-\pi}^{0}-\left[ \frac{\cos n x}{n}\right] _{0}^{\pi}\right\rbrace  \\
		&=\frac{1}{\pi} k\left\lbrace \frac{1}{n}-\frac{(-1)^{n}}{n}-\frac{(-1)^{n}}{n}+\frac{1}{n}\right\rbrace =\frac{1}{\pi} k\left[\frac{2}{n}-\frac{2(-1)^{n}}{n}\right\rbrace \\
		\text{If $n$ is even}\ b_{n}&=0\\
	\text{	If $\mathrm{n}$ is odd }\ b_{n}&=\frac{4 k}{n \pi}\\
	f(x)&=\frac{4 k}{\pi} \sin x+\frac{4 k}{3 \pi} \sin 3 x+\frac{4 k}{5 \pi} \sin 5 x+\cdots\\
	f(x)&=\frac{4 k}{\pi}\left[\sin x+\frac{1}{3} \sin 3 x+\frac{1}{5} \sin 5 x+\ldots\right]\\
	\text { At}\ x&=\frac{\pi}{2} \\
	k&=\frac{4 k}{\pi} \left[  \sin \frac{\pi}{2}+\frac{1}{3} \sin \frac{3 \pi}{2} +\frac{1}{5} \sin \frac{5 \pi}{2}\right]   \\
	1&=\frac{4}{\pi}\left[1+\frac{1}{3}(-1)+\frac{1}{5}(1)+\frac{1}{7}(-1)+\ldots \cdots\right]\\&=\frac{4}{\pi}\left[1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\ldots . .\right] \\ \frac{\pi}{4}&=1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\ldots \ldots
		\end{align*}

	\end{answer}

\item Find the fourier series of the function defined as
$$
f(x)=\left\{\begin{array}{ll}
 \left.  \right. \ x+\pi, & \text { for } 0 \leq x \leq \pi \\
-x-\pi, & \text { for }-\pi \leq x<0
\end{array} \text { and } f(x+2 \pi)=f(x)\right.
$$
\begin{answer}
	\begin{align*}
		a_{0} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) d x=\frac{1}{\pi} \int_{-\pi}^{0} f(x) d x+\frac{1}{\pi} \int_{0}^{\pi} f(x) d x \\
		&=\frac{1}{\pi} \int_{-\pi}^{0}(-x-\pi) d x+\frac{1}{\pi} \int_{0}^{\pi}(x+\pi) d x\\&=\frac{1}{\pi}\left(-\frac{x^{2}}{2}-\pi x\right)_{-\pi}^{0}+\frac{1}{\pi}\left(\frac{x^{2}}{2}+\pi x\right)_{0}^{\pi} \\
		&=\frac{1}{\pi}\left(\frac{\pi^{2}}{2}-\pi^{2}\right)+\frac{1}{\pi}\left(\frac{\pi^{2}}{2}+\pi^{2}\right)\\&=\pi\left(\frac{1}{2}-1\right)+\pi\left(\frac{1}{2}+1\right)\\&=\pi
	\end{align*}
	\begin{align*}
		a_{n} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \cos n x d x\\&=\frac{1}{\pi} \int_{-\pi}^{0} f(x) \cos n x d x+\frac{1}{\pi} \int_{0}^{\pi} f(x) \cos n x d x \\
		&=\frac{1}{\pi} \int_{-\pi}^{0}(-x-\pi) \cos n x d x+\frac{1}{\pi} \int_{0}^{\pi}(x+\pi) \cos n x d x \\
		&=\frac{1}{\pi}\left[(-x-\pi) \frac{\sin n x}{n}-(-1)\left\{-\frac{\cos n x}{n^{2}}\right\}\right]_{-\pi}^{0}+\frac{1}{\pi}\left[(x+\pi) \frac{\sin n x}{n}-(1)\left\{-\frac{\cos n x}{n^{2}}\right\}\right]_{0}^{\pi}\\
		&=\frac{1}{\pi}\left[-\frac{1}{n^{2}}+\frac{(-1)^{n}}{n^{2}}\right]+\frac{1}{\pi}\left[\frac{(-1)^{n}}{n^{2}}-\frac{1}{n^{2}}\right]=\frac{2}{n^{2} \pi}\left[(-1)^{n}-1\right]\\
		&=\frac{-4}{n^{2} \pi}\quad \text{if $n$ is odd}\\
		&=0 \quad \text{if $n$ is even}
	\end{align*}
	\begin{align*}
		b_{n} &=\frac{1}{\pi} \int_{-\pi}^{\pi} f(x) \sin n x d x=\frac{1}{\pi} \int_{-\pi}^{0} f^{\prime}(x) \sin n x d x+\frac{1}{\pi} \int_{0}^{\pi} f(x) \sin n x d x \\
		&=\frac{1}{\pi} \int_{-\pi}^{0}(-x-\pi) \sin n x d x+\frac{1}{\pi} \int_{0}^{\pi}(x+\pi) \sin n x d x \\
		&=\frac{1}{\pi}\left[(-x-\pi)\left(\frac{-\cos n x}{n}\right)-(-1)\left(-\frac{\sin n x}{n^{2}}\right)\right]_{-\pi}^{0}+\frac{1}{\pi}\left[(x+\pi)\left(-\frac{\cos n x}{n}\right)-(1)\left(-\frac{\sin n x}{n^{2}}\right)\right]_{0}^{\pi} \\
		&=\frac{1}{\pi}\left[\frac{\pi}{n}\right]+\frac{1}{\pi}\left[-\frac{2 \pi}{n}(-1)^{n}+\frac{\pi}{n}\right]=\frac{1}{n}\left[(1)-2(-1)^{n}\right]=\frac{2}{n}\left[1-(-1)^{n}\right]\\
		&=\frac{4}{n}\quad \text{if $n$ is odd}\\
		&=0 \quad \text{if $n$ is even}
	\end{align*}
	The fourier series expansion of the function is
	\begin{align*}
	f(x)&=\frac{a_{0}}{2}+a_{1} \cos x+a_{2} \cos 2 x+\ldots+b_{1} \sin x+b_{2} \sin 2 x+\ldots \\
	f(x)&=\frac{\pi}{2}-\frac{4}{\pi}\left(\frac{\cos x}{1^{2}}+\frac{\cos 3 x}{3^{2}}+\ldots . .\right)+4\left(\frac{\sin x}{1}+\frac{\sin 3 x}{3}+\ldots\right)
	\end{align*}

\end{answer}
\item  Find the Fourier sine series for the function
$$
f(x)=e^{a x} \quad \text { for }-\pi \leq x \leq \pi
$$
where $a$ is constant.
\begin{answer}
	\begin{align*}
		\int e^{a x} \sin b x d x&=\frac{e^{a x}}{a^{2}+b^{2}}[a \sin b x-b \cos b x] \\
		b_{n}&=\frac{2}{\pi} \int e^{a x} \sin n x d x \\
		&=\frac{2}{\pi}\left[\frac{e^{a x}}{a^{2}+n^{2}}(a \sin n x-n \cos n x)\right]_{0}^{\pi} \\
		&=\frac{2}{\pi}\left[\frac{e^{a \pi}}{a^{2}+n^{2}}(a \sin n \pi-n \cos n \pi)+\frac{n}{a^{2}+n^{2}}\right] \\
		&=\frac{2}{\pi}\left(\frac{n}{a^{2}+n^{2}}\right)\left[-(-1)^{n} e^{a \pi}+1\right]\\&=\frac{2 n}{\left(a^{2}+n^{2}\right) \pi}\left[1-(-1)^{n} e^{a \pi}\right] \\\\
		b_{1}&=\frac{2\left(1+e^{a \pi}\right)}{\left(a^{2}+1^{2}\right) \pi} \quad ; \quad b_{2}=\frac{2 \cdot 2 \cdot\left(1-e^{a \pi}\right)}{\left(a^{2}+2^{2}\right) \pi} \\\\
		e^{a x}&=\frac{2}{\pi}\left[\frac{1+e^{a \pi}}{a^{2}+1^{2}} \sin x+\frac{2\left(1-e^{a \pi}\right)}{a^{2}+2^{2}} \sin 2 x+\ldots .\right]
	\end{align*}
\end{answer}
\item Obtain the half range cosine series for $\mathrm{f}(\mathrm{x})=(\mathrm{x}-2)^{2}$ in the interval $(0,2)$.
\begin{answer}
	$f(x)=(x-2)^{2}$
	We know that the Fourier half range cosine series is
	$$
	f(x)=\frac{a_{0}}{2}+\sum_{n=1}^{\infty} a_{n} \cos \frac{n x \pi}{l}
	$$

	\begin{align*}
	\text { Where } \\
	a_{0}&=\frac{2}{l} \int_{0}^{l} f(x) d x \\
	a_{n}&=\frac{2}{l} \int_{0}^{l} f(x) \cos \frac{n \pi x}{l} d x
	\end{align*}

	Here $l=2$
	\begin{align*}
	a_{0}&=\frac{2}{2} \int_{0}^{2}(x-2)^{2} d x=\left[\frac{(x-2)^{3}}{3}\right]_{0}^{2}\\
	&=\left[0-\frac{(-2)^{3}}{3}\right]=\frac{8}{3} \\
	a_{0}&=\frac{8}{3}\\
	a_{n}&=\frac{2}{2} \int_{0}^{2}(x-2)^{2} \cos \frac{n \pi x}{2} d x \\
	a_{n}&=\left[(x-2)^{2}\left(\frac{\sin \frac{n \pi x}{2}}{\frac{n \pi}{2}}\right)-2(x-2)\left(\frac{-\cos \frac{n \pi x}{2}}{\frac{n^{2} \pi^{2}}{2^{2}}}\right)+2\left(\frac{-\sin \frac{n \pi x}{2}}{\frac{n^{3} \pi^{3}}{2^{3}}}\right)\right]_{0}^{2} \\
	a_{n}&=\left[0+0-2 \frac{8}{n^{3} \pi^{3}} \sin n \pi-0-2(-2) \frac{4}{n^{2} \pi^{2}}+0\right] \\
	a_{n}&=\frac{8}{n^{2} \pi^{2}}
	\end{align*}

	$$
	\begin{array}{l}
	f(x)=\frac{8}{2 x^3}+\sum_{n=1}^{\infty} \frac{8}{n^{2} \pi^{2}} \cos \frac{n x \pi}{2} \\\\
	f(x)=\frac{4}{3}+\frac{8}{\pi^{2}} \sum_{n=1}^{\infty} \frac{1}{n^{2}} \cos \frac{n x \pi}{2}
	\end{array}
	$$
\end{answer}
\end{enumerate}


\documentclass[12pt]{amsart}
\usepackage{amssymb,amsmath}
\usepackage{graphicx}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
\geometry{a4paper} % or letter or a5paper or ... etc
% \geometry{landscape} % rotated page geometry

\newcommand{\bigzero}{\mbox{\normalfont\Large\bfseries 0}}
\newcommand{\rvline}{\hspace*{-\arraycolsep}\vline\hspace*{-\arraycolsep}}

% See the ``Article customise'' template for come common customisations

\title{Visualizing Grover's Search Algorithm}
\author{James Weaver \& Paul Kassebaum}
\date{} % delete this line to display the current date

%%% BEGIN DOCUMENT
\begin{document}

\maketitle

\section{Oracle operators}

If we want an operator $U_x$ with the following behavior

\begin{equation}
	\begin{split}
		U_x |x\rangle & = - |x\rangle \\
		U_x |x_\perp\rangle & = |x_\perp\rangle
	\end{split}
\end{equation}

where all $|x_\perp\rangle$ are orthogonal to $|x\rangle$, then the operator takes the form

\begin{equation}
	U_x = I - 2 |x\rangle\langle x|
\end{equation}

Let's demonstrate this with a couple of examples.

\begin{equation}
	\begin{split}
		U_x |x\rangle & = (I - 2 |x\rangle\langle x| ) |x\rangle \\
		& = |x\rangle - 2 |x\rangle\langle x | x\rangle \\
		& = |x\rangle - 2 |x\rangle \\
		& = - |x\rangle
	\end{split}
\end{equation}

because $\langle x | x\rangle = 1$.

\begin{equation}
	\begin{split}
		U_x |x_\perp\rangle & = (I - 2 |x\rangle\langle x| ) |x_\perp\rangle \\
		& = |x_\perp\rangle - 2 |x\rangle\langle x | x_\perp\rangle \\
		& = |x_\perp\rangle - 2 \langle x | x_\perp\rangle  |x_\perp\rangle \\
		& = |x_\perp\rangle
	\end{split}
\end{equation}

because $\langle x | x_\perp\rangle = 0$.

Now let's look at the result of acting on an arbitrary state $|\alpha\rangle$, that is not completely orthogonal to $|x\rangle$.

\begin{equation}
	\begin{split}
		U_x |\alpha\rangle & = (I - 2 |x\rangle\langle x| ) |\alpha\rangle \\
		& = |\alpha\rangle - 2 |x\rangle\langle x | \alpha\rangle \\
		& = (1 - 2 \langle x | \alpha\rangle)  |\alpha\rangle \\
	\end{split}
\end{equation}

In words, the result is the original state vector minus twice the overlap or projection of $|\alpha\rangle$ on $|x\rangle$. We can draw this out geometrically.
\begin{figure}[h]
   \centering
   \includegraphics[width=0.5\textwidth]{./img/fig-00} % requires the graphicx package
   \caption{Geometric demonstration that the operator $U_x$ has the effect of reflecting $|\alpha\rangle$ across $|x_\perp\rangle$, the vector orthogonal to $|x\rangle$.}
   \label{fig:example}
\end{figure}

\section{Symmetries of a single qubit system}
A single qubit's state $|s\rangle$

\begin{equation}
	|s\rangle = (a+ib)|0\rangle + (c + id)|1\rangle
\end{equation}

written in terms of the 4 real numbers $a,b,c,d$ can be fully described by 3 real numbers, since the state must be normalized,

\begin{equation}
	\langle s | s\rangle = a^2 + b^2 + c^2 + d^2 = 1,
\end{equation}

which makes one of the real numbers dependent on the other three.

A single qubit's rotational symmetries can be described by the matrices

\begin{equation}
	\begin{pmatrix}
		a + i b & -(c - id) \\
		c + id & -(a + i b)
	\end{pmatrix}
\end{equation}

where $a,b,c,d$ are real numbers. The normalization condition on the state of the qubit requires that the determinant of this matrix be 1.

Another way to describe a single qubit's rotational symmetries is by the quaternion:

\begin{equation}
	a +bi + cj + dk
\end{equation}

where the real numbers $a,b,c,d$ of the matrix and quaternion are equal respectively. The determinant of the matrix is the norm of the corresponding quaternion. Since the matrix has determinant 1 as a consequence of the normalization condition of the qubit's state, the corresponding quaternion has norm 1.

\section{Symmetries of a two qubit system}

A system of two qubits can be described by $2\times3 = 6$ real numbers. The system transforms as the tensor product of two independent matrices that each describe a single qubit, as explained above.

\begin{equation}
	\begin{pmatrix}
  \begin{matrix}
  i a & -\bar{z} \\
  z & - i a
  \end{matrix}
  & \rvline & \bigzero \\
\hline
  \bigzero & \rvline &
  \begin{matrix}
  i b & -\bar{w} \\
  w & -i b
  \end{matrix}
\end{pmatrix}
\end{equation}

where $a,b$ are real numbers and $z,w$ are complex numbers. The numbers $a,z,b,w$ are made up of 6 independent real numbers. Each of the two block matrices can be related to independent quaternions.

The dynamics of a two qubit system can be thought of geometrically in 4D euclidean space where each of the two quaternions is acted upon independently by operators associated with each spin respectively.

\end{document}


\chapter{Luminosity Calculation}
\label{lumi}

The probability of n interactions per bunch crossing is ( because a collision is a random process, the distribution will follow Poisson statistics.
\begin{equation}
P = \frac{\mu^{N}}{N!} \times e^{-\mu}
\end{equation}
The probability of at least one bunch crossing is
\begin{equation}
P = 1 -  e^{-\mu}
\end{equation}
The average number of bunch crossings, $\mu$, is defined as
\begin{equation}
\mu = \frac{\mathcal{L} \times \sigma_{\rm{eff}}}{f_{\rm{beam}}}
\end{equation}
Thus, the luminosity can be written as
\begin{equation}
\mathcal{L} = - \frac{f_{\rm{beam}}}{\sigma_{\rm{eff}}} \rm{ln}(1-P(n>0))
\end{equation}
By recording the number of bunch crossings without an inelastic collision, the luminosity can be calculated.


\chapter{Introduction}
In this course we will be concerned with ordinary differential equations, ODEs.

\begin{defin}
An ordinary differential equation (ODE) is a differential equation containing one or more functions of \underline{\textbf{exactly one}} independent variable and its derivatives.
\end{defin}

ODEs relate the change of one variable to changes in another variable and can be used to model and understand a wide variety of phenomena, such as projectile motion, animal population interactions and the progression of chemical reactions.

In these notes we consider two important aspects in the theory of ordinary differential equations. Specifically, we seek to
\begin{enumerate}
\item develop methods of modelling physical phenomena;
\item understand the properties of the equations without explicitly solving them.
\end{enumerate}

Point 2 may seem counter-intuitive as we have a variety of techniques that enable us to solve ODEs in closed form. Further, even if an explicit solution is not available, we can use numerical simulations to illustrate the dynamics of the ODEs. However, direct solutions are not always possible and, even when they are, they may not always enable clear interpretations and understanding of the underlying system. Equally, our analytical techniques will give us confidence in the solutions produced by numerical software.

Critically, what we gain in analytical specificity, we lose in global accuracy. Namely, we are going to learn techniques that will allow us to rigorously examine small regions of the ODE space at the expense of losing knowledge of the global dynamics. However, by the end of the course we will be able to patch together multiple parts of the local analysis in order to give us an approximate understanding of the entire dynamical system.

\section{Preliminary definitions}
We will be considering the rate of change of a variable, $u$, with respect to another variable, $t$. This dependence will be denoted
\bb
u(t).
\ee
Here, $u$ is a scalar function (\ie one-dimensional), but more generally, we will be considering systems of variables
\bb
\bm{u}(t)=\l u_1(t),u_2(t),\dots,u_k(t)\r.
\ee
On the board we will usually write bold symbols with an underline\footnote{I was once told that we use underlines to illustrate bold variables because when typesetting a document an underline would tell the printer that that symbol needed to be bold. However, if this is true, how did the writer indicate that they wanted a symbol underlined?} as it is easier to see, thus, $\bm{u}=\underline{u}$.

The values of $u$ or $\bm{u}$ define quantities of interest. For example they could be an animal population density, a distance or a speed. Further, $t$ can be any variable which these quantities are dependent on. Generally, however, we will take $t$ to be time and we will be considering how these values temporally evolve.

In order to link the changes in these quantities we define a system of ODEs in the most general way possible,
\bb
\bm{F}\l t,\bm{u},\frac{\rd \bm{u}}{\rd t},\frac{\rd^2 \bm{u}}{\rd t^2},\dots,\frac{\rd^n \bm{u}}{\rd t^n}\r=0,
\ee
with initial condition given by
\bb
\bm{u}(0)=\bm{u}_0.
\ee
Note that the initial condition is kept general as we will usually be interested in how the dynamics of the system change for different starting points.

\begin{example}[frametitle=Bacteria population growth]
\label{Expo_growth}
\COL{In this case we are only considering one population, thus, $\bm{u}=u$ and we specify $u(t)$ to be the population of E.Coli at time $t$. Initially, the population is $u_0$ and resources are abundant, thus, each E.Coli is able to double itself at a rate $r$/s. Explicitly, the population grows at a rate proportional to the population already present, \ie
\bb
\frac{\rd u}{\rd t}=ru.
\ee
This equation can be trivially solved to give
\bb
u(t)=u_0\exp(rt),\label{Intro_exp}
\ee
see \fig{Exponential}.

Note instead of specifying the time at which a population takes an arbitrary value, we can consider the more general time scale of how long does it take the population to double? Namely, at what point, $t_2$, is $u(t_2)=2u_0$. Rearranging \eqn{Intro_exp} we derive that
\bb
t_2=\frac{1}{r}\log\l 2\r.
\ee

Critically, once a model is constructed and an answer is found, we must consider whether if it is a good model or not. Clearly this model has problems because it predicts the population will grow exponentially quickly, without bound. The key problematic assumption that we have made is that the resources (\eg space, nutrients, etc.) do not run out. Although this may be a fine assumption to begin with, eventually the bacteria will be limited by competition.}
%\begin{figure}[!!!h!!!tb]
{\centering
\includegraphics[width=\textwidth]{../Pictures/Bacteria.png}}
%\end{figure}
\end{example}

\begin{example}[frametitle=Bacteria and nutrient populations.]\label{Logi_growth}
\COL{Imagine a case similar to the one above, but we introduce a nutrient population, $v$. We assume that a bacterium can divide at a rate $r$ if and only if it can interact with enough nutrient. However, the nutrient is depleted at a rate $r$ as the bacteria interacts with it.

Here, the equation governing this system is going to be provided. However, later on we will learn how to write down and interpret interaction equations of the form
\bb
u+v \stackrel{r}{\longrightarrow} 2u.
\ee

The governing equations are
\begin{align}
\frac{\rd u}{\rd t}&=ruv,\quad u(0)=u_0,\label{Pop_u}\\
\frac{\rd v}{\rd t}&=-ruv,\quad v(0)=v_0.
\end{align}}
\COL{We notice that we can add the two equations and integrate to provide a conserved quantity,
\bb
u+v=c.\label{Conserved_c}
\ee
We can substitute \eqn{Conserved_c} into \eqn{Pop_u} to get
\bb
\frac{\rd u}{\rd t}=ru(c-u).\label{Log_1}
\ee
This is known as the logistic equation and we will see it many times throughout these notes, it is a simple example of competition between species for resources.}

\COL{Using partial fractions, we can directly solve \eqn{Log_1}. Specifically,
\begin{align}
\frac{\rd u}{\rd t}&=rcu\l 1-\frac{u}{c}\r,\nonumber\\
\Rightarrow\int_0^T\frac{\rd u}{u(1-u/c)}&=\int_0^Trc \rd t,\nonumber\\
\Rightarrow\int_0^T \frac{1}{u}+\frac{1/c}{1-u/c}  \rd u&=rcT,\nonumber\\
\Rightarrow\left[\ln(u)-\ln\l 1- \frac{u}{c} \r \right]^T_0&=rcT,\nonumber\\
\Rightarrow\ln\l\frac{u}{1- \frac{u}{c}} \r-\ln\l\frac{u_0}{1- \frac{u_0}{c}} \r&=rcT,\nonumber\\
\Rightarrow u(T)=\frac{c}{1+\frac{c-u_0}{u_0}\exp\l{-rcT}\r},
\end{align}
see \fig{Logistic}.

Comparing the models of bacteria growth, illustrated in \fig{Growth_examples}, we see that \eqn{Log_1} is a more realistic model for growth because there is a maximum population value which can be supported by the experiment. This maximum value is given by $c$ and is known as the carrying capacity. The parameter grouping $rc$ is also important as this control the time scale over which this maximum is obtained.}
\end{example}
\begin{figure}[!!!h!!!tb]
{\centering
\subfigure[\label{Exponential}]{\includegraphics[width=\ttp]{../Pictures/Exponential.png}}
\subfigure[\label{Logistic}]{\includegraphics[width=\ttp]{../Pictures/Logistic.png}}
\caption{(a) Exponential growth. Parameters are $r=u_0=0.1$. See example \ref{Expo_growth}. (b) Logistic growth. Parameters are $c=10$, $r=u_0=0.1$. See example \ref{Logi_growth}.}\label{Growth_examples}}
\end{figure}

\begin{example}[frametitle=Duffing's equations.]\label{Duffing_example}
For our last example, consider the Duffing oscillator. The equation is simply a toy example that can be used to examine complex phenomena in a simple equation. In terms of interpretation, you can think of the equation as modelling the displacement of a beam near two magnetics. Critically, the beam and magnets are being forced to oscillate with amplitude $\gamma$ and frequency $\omega$ \see{Duffing_beam}.
\bb
\underbrace{\frac{\rd^2 x}{\rd t^2}}_{\textrm{Acceleration}}+\underbrace{2\delta\frac{\rd x}{\rd t}}_{\textrm{Air resistance}}+\underbrace{\beta x+\alpha x^3}_{\textrm{Beam's restorative force}}=\underbrace{\gamma\cos(\omega t)}_{\textrm{Forcing term}}.\label{Duffing_eqn}
\ee

We are not going to try and analytically solve or analyse Duffing's equation. Instead, we illustrate the dynamics that the equation produces as the amplitude of oscillation, $\gamma$, increases. Specifically, as $\gamma$ is increased the system becomes chaotic \see{Duffing_equation}.
\end{example}
\begin{figure}[!!!h!!!tbp]
\centering
\subfigure[\label{Duffing_beam}]{\includegraphics[width=.3\textwidth]{../Pictures/Duffing_beam.png}}
\subfigure[\label{Duffing_equation}]{\includegraphics[width=\textwidth]{../Pictures/Duffing.png}}
\caption{\label{Duffing}(a) Schematic diagram of the system underlying Duffing's equation. (b) Three simulations of \eqn{Duffing_eqn} with increasing values of $\gamma$.}
\end{figure}

\begin{defin}
The \textbf{order} of a differential equation is the value of the highest derivative in the equation.
\end{defin}
Examples \ref{Logi_growth} and \ref{Expo_growth} are both first order equations, whilst example \ref{Duffing_example} is a second order equation. Generally (like polynomial equations of order) a differential equation of order $n$ will have $n$ linearly independent solutions.

\begin{defin}
A system of differential equations is \textbf{autonomous} if the system does not explicitly depend on the independent variable.
\end{defin}
When the variable is time, they are also called time-invariant systems, this simply means that we are assuming that the defined underlying laws of the system are identical to those for any point in the past, or future.

\begin{defin}
To save time we use a dot or prime mark to denote a derivative with respect to the argument, thus,
\bb
\dot{\bm{u}}(t)=\bm{u}'(t)=\frac{\rd \bm{u}}{\rd t}.
\ee
\end{defin}
Traditionally, dots are primarily used when the variable is time and primes are used otherwise. Note that higher orders derivatives are signified by the appropriate number of dots or primes. Namely, a second derivative would be denoted by two dots or primes, etc.

\begin{defin}
A \textbf{trajectory} is a solution, $u(t)$.
\end{defin}
The graphs in \figs{Growth_examples}{Duffing} illustrate single trajectories of their respective systems.

In this course we are going to occupy ourselves with systems of autonomous first order equations, of the form
\bb
\frac{\rd \bm{u}}{\rd t}=\dot{\bm{u}}=\bm{F}(\bm{u}).\label{ODE}
\ee
This may seem highly restrictive. However, systems of first order equations can have extremely complicated properties, such as oscillations and chaos, which we will try to understand.

\COL{Critically, equations of higher order can be written as a system of first order equations. For example, if
\bb
\bm{G}\l \bm{u},\frac{\rd \bm{u}}{\rd t},\frac{\rd^2 \bm{u}}{\rd t^2},\dots,\frac{\rd^n \bm{u}}{\rd t^n}\r=0
\ee
then we can define $n-1$ new equations of the form $\bm{v}_1=\rd \bm{u}/\rd t$ and $\bm{v}_i=\rd \bm{v}_{i-1}/\rd t=\rd^{i} \bm{u}/\rd t^{i}$ for $2\leq i \leq n-1$ to produce the first order system
\begin{align}
&\bm{G}\l \bm{u},\bm{v}_1,\dots,\bm{v}_{n-1},\frac{\rd \bm{v}_{n-1}}{\rd t}\r=0,\\
&\frac{\rd \bm{u}}{\rd t}=\bm{v}_1,\\
&\vdots\nonumber\\
&\frac{\rd \bm{v}_{n-2}}{\rd t}=\bm{v}_{n-1}.
\end{align}}
\begin{example}[frametitle=Duffing's equations without forcing.]\label{Duffing_example2}
\COL{Setting $\gamma=0$ in Duffing's equation and letting
\bb
v=\frac{\rd x}{\rd t}
\ee
then we are able to convert the single second order equation seen in \eqn{Duffing_equation} to two first}\COL{ order ODEs,
\begin{align}
\frac{\rd v}{\rd t}&=-2\delta v-(\beta x+\alpha x^3)\\
\frac{\rd x}{\rd t}&=v.
\end{align}
Note that $v$ is an apt variable name for the variable, because, as discussed in example \ref{Duffing_example}, $x$ can be thought of as position, making $v$ a velocity.}
\end{example}
\begin{thm}
A solution trajectory, $\bm{u}(t)$, of \eqn{ODE} cannot self-intersect \see{Trisectrix}.
\end{thm}
\begin{proof}
\COL{Suppose there is an intersection. Hence, there exist two points, $t_1$ and $t_2$, such that $\bm{u}(t_1)=\bm{u}(t_2)$ then we will also have that $\bm{F}(\bm{u}(t_1))=\bm{F}(\bm{u}(t_2))$. However, the curves intersect, thus, the curves must be travelling in different directions at $t_1$ and $t_2$ \see{Intersect}, meaning that the derivatives are different there, \ie $\dot{\bm{u}}(t_1)\neq\dot{\bm{u}}(t_2)$. But
\bb
\dot{\bm{u}}(t_1)=\bm{F}(\bm{u}(t_1))=\bm{F}(\bm{u}(t_2))=\dot{\bm{u}}(t_2),
\ee
which produces a contradiction. Hence the curves cannot intersect.}
\end{proof}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\ttp]{../Pictures/Trisectrix.png}
\caption{\label{Trisectrix} A solution of \eqn{ODE} cannot look like this.}
\end{figure}

\subsection{Existence and uniqueness}
With this being an applied mathematics course we are often very `fast and loose' with our rigour. However, it is good to know that theorems have been proven regarding the existence and unique of solution to \eqn{ODE}. Here we will quote the theorem in one dimension, but the theorem can be expanded to any number of variables.
\begin{thm}Existence-Uniqueness theorem.

Suppose the function $F(u)$ is differentiable and the derivative, $F'(u)$, is continuous for all values of $u$ then there will exist some constant $c>0$ such that
\bb
\dot{u}=F(u),\quad u(t_0)=u_0,
\ee
has a solution and it is guaranteed to exist and be unique in some finite time interval $|t-t_0|<c$.\label{Existence_Uniqueness}
\end{thm}
Note that:\COL{
\begin{itemize}
\item we will not consider the proof here. For those who are interested look up ``Picard's theorem''. Picard's theorem is actually weaker than the one specified above, but theorem \ref{Existence_Uniqueness} expresses the statement in the most useful form for us.
\item in many cases solutions will exist and be unique for all time, but, the theorem hardly ever provides an optimal value for $c$. However, the theorem is general enough to include cases where `blow up' occurs. Namely, blow up occurs when a solution tends to infinity in finite time.
\item without loss of generality we can always take $t_0=0$ (why?). \textbf{This is only true in autonomous systems}.
\item solution curves cannot intersect, otherwise there would be two different solutions going through the same point and there would not be uniqueness around the intersection \see{Intersect}.
\item the case for higher dimensional systems is effectively the same except we need the function
\bb
\bm{F}(\bm{u})=\left(
\begin{array}{c}
{F_1}(u_1,u_2,u_3,\dots,u_n)\\
{F_2}(u_1,u_2,u_3,\dots,u_n)\\
\vdots\\
{F_n}(u_1,u_2,u_3,\dots,u_n)
\end{array}
\right)
\ee to be continuous in all of its derivatives.
\end{itemize}}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\ttp]{../Pictures/Intersection.png}
\caption{\label{Intersect}Two different solution curves of a one-dimensional ODE cannot intersect.}
\end{figure}

\begin{defin}\label{Monotonic_def}
A differentiable function is \textbf{monotonic} if its derivative never changes sign. Moreover, the function is monotonically increasing (decreasing) if the derivative is positive (negative).
\end{defin}
\begin{defin}\label{Strict_monotonic_def}
A differentiable function is \textbf{strictly monotonic} if its derivative never changes sign and is never zero.
\end{defin}
See \fig{Monotonics} for examples of definitions \ref{Monotonic_def} and \ref{Strict_monotonic_def}.
\begin{figure}[!!!h!!!tb]
\centering
\subfigure[\label{Non_monotonic}]{\includegraphics[width=\tttp]{../Pictures/Non_monotonic.png}}
\subfigure[\label{Monotonic}]{\includegraphics[width=\tttp]{../Pictures/Monotonic.png}}
\subfigure[\label{Strictly_monotonic}]{\includegraphics[width=\tttp]{../Pictures/Strictly_monotonic.png}}
\caption{\label{Monotonics}(a)\COL{ A non-monotonic function.} (b) \COL{A monotonic function.} (c) \COL{A strictly monotonic function.}}
\end{figure}

\begin{cor}
Suppose $F(u)$ is a scalar function that is continuously differentiable. The solution, $u(t)$, of the one dimensional ODE,
\bb
\dot{u}=F(u),\quad u(t_0)=u_0,
\ee
cannot oscillate. Specifically, $u(t)$ must either be constant, or a monotonically increasing, or decreasing function.
\end{cor}
\begin{proof}
\COL{Suppose that $u^*(t)$ is non-monotonic. By definition its derivative changes sign. By continuity there is somewhere, $t_c$, such that $\rd u^*(t_c)/\rd t=0$.
Thus, $u^*$ is a solution of
\bb
\dot{u^*}=F(u^*),\quad u^*(t_c)=u^*_c.\label{Starred}
\ee
Let us construct the constant function $u\equiv u^*_c$. We note that, $F(u^*(t_c))=0$ and, thus,  $u$ is also a solution to \eqn{Starred}. However, this means that we have two different solutions to \eqn{Starred} violating theorem \ref{Existence_Uniqueness}. By contradiction $u^*(t)$ has to be monotonic.}
\end{proof}
This means that to have oscillatory phenomena in a system either we need more than one population, or the system has to be non-autonomous. See example \ref{Duffing_example} for a case where both of these factors are present and do indeed produce oscillations (and chaos).

\section{Taylor expansions}
This section is to remind you of the Taylor expansion technique. The Taylor expansion is one of the most powerful tools for an applied mathematician because very often we want to know what happens to a trajectory near some critical point. Although the kinetics maybe very non-linear and difficult to understand globally we can use the Taylor expansion to simplify the dynamics in a small region around the critical point in order to gain knowledge about the dynamics in this region.

\begin{thm}\label{Taylor}
Suppose $f(x)$ is infinitely differentiable at a point $a$ then the Taylor series of $f$ at $a$ is the power series
\bb
f(x)=\sum _{n=0}^{\infty }{\frac{f^n(a)}{n!}(x-a)^n},
\ee
which is explicitly
\bb
f(x)=f(a)+{\frac {f'(a)}{1!}}(x-a)+{\frac {f''(a)}{2!}}(x-a)^{2}+{\frac {f'''(a)}{3!}}(x-a)^{3}+\cdots,
\ee
where $n!$ denotes the factorial of $n$ and $f^{(n)}(a)$ denotes the $n^{th}$ derivative of $f$ evaluated at the point $a$. The derivative of order zero of $f$ is defined to be $f$ itself and $(x-a)^0$ and $0!$ are both defined to be 1.
\end{thm}
\begin{example}[frametitle=Taylor expansions.]
\begin{itemize}
\item $\exp(x)$ at $x=0$ \see{Taylor_approximations}.
\COL{
\bb
\exp(x)=1+x+{\frac {1}{2}}{x}^{2}+{\frac {1}{6}}{x}^{3}+O \left( {x}^{4}
 \right) .
 \ee}
\item $\cos(x)$ at $x=0$.
\COL{
\bb
\cos(x)=1-{\frac {1}{2}}{x}^{2}+O \left( {x}^{4} \right) .
\ee}
\item $1/\l 1+x\r$ at $x=0$
\COL{
\bb
\frac{1}{1+x}=1-x+{x}^{2}-{x}^{3}+O \left( {x}^{4} \right) .
\ee}
\item $\sin(x)$ at $x=\pi/2$.
\COL{
\bb
\sin(x)=1-{\frac{1}{2}} \left( x-{\frac {\pi}{2}} \right) ^{2}+O \left(
 \left( x-{\frac {\pi}{2}} \right) ^{4} \right)  .
\ee}
\end{itemize}
\end{example}
\begin{figure}[!!!h!!!tb]
\centering
\includegraphics[width=\textwidth]{../Pictures/Taylor_approximations.png}
\caption{\label{Taylor_approximations} Approximating the exponential function with different orders of Taylor series.}
\end{figure}

\COL{Although Theorem \ref{Taylor} is the most general form of Taylor's theorem we are frequently going to want to know what happens near a specific point. Namely, if $x$ is the point of interest, what does the function look like at $x+\epsilon$, where $\epsilon \ll 1$. Specifically, this simply comes down to redefining $x\coloneqq x+\epsilon$ and $a\coloneqq x$ in Theorem \ref{Taylor}, namely
\bb
f(x+\epsilon)=f \left( x \right) +\epsilon{\frac {\rm d}{{\rm d}x}}f \left( x \right)
+{\frac {{\epsilon}^{2}}{2}{\frac {{\rm d}^{2}}{{\rm d}{x}^{2}}}f \left( x
 \right) }+{\frac {{\epsilon}^{3}}{6}{\frac {{\rm d}^{3}}{{\rm d}{x}^{
3}}}f \left( x \right) }+O \left( {\epsilon}^{4}
 \right).
\ee}\COL{
Since $\epsilon\ll 1$ we can truncate this series to obtain a good estimate after only a first term in $\epsilon$,
\bb
f(x+\epsilon)\approx f \left( x \right) +\epsilon{\frac {\rm d}{{\rm d}x}}f \left( x \right).
\ee
This is known as linearisation. You are taking the (possibly complicated) function $f$ and rewriting it as a linear function in $\epsilon$.}

Courses in the third year will deal with what information you get in the case that you truncate at $\epsilon^2$, or higher. This is non-linear analysis.

\subsection{Multivariate Taylor expansion}
A similar theorem can be stated when the function $f$ has more than one argument.
\begin{defin}
If $f$ is a function of more than one variable it is called \textbf{multivariate}.
\end{defin}

\COL{Here we simply state the expansion to first order expansion that we will be concerned with throughout the course.
\bb
f(x+\epsilon_1,y+\epsilon_2)\approx f(x,y)+\epsilon_1 f_x+\epsilon_2 f_y,
\ee
where we observe that we have used a subscript $f_x$ to denote the partial derivative $\partial f/\partial x$ and similarly for $f_y$.}
\begin{defin}
For brevity we use subscripts to stand for partial derivatives,
\bb
f_{x_1x_2\dots x_n}=\frac{\partial^n f}{\partial x_1\partial x_2\dots\partial x_n}.
\ee
\end{defin}
\begin{example}[frametitle=Multivariate Taylor expansion.]
\begin{itemize}
\item $\sin(x+y)$ at $x=y=0$.
\COL{
\bb
\sin(x+y)\approx x+y.
\ee}
\item $\sin(x)\cos(y)$ at $x=y=0$.
\COL{
\bb
\sin(x)\cos(y)\approx x.
\ee}
\end{itemize}
\end{example}

\section{Polar coordinates}
Many phenomena that we will model will fall under the consideration of spatial movement, for example in Chapter \ref{How to model a system} and question sheet two we will be considering planetary movement. Critically, in many of these cases the objects tend to move in circular trajectories orbiting a single point. Thus, it is more natural to use polar coordinates $(r,\theta)$ to describe the motion, rather than Cartesian coordinates $(x,y)$ \see{Polars}. However, it may be easier to model the system in Cartesian coordinates. Thus, we need to know how to convert between one set and another.
\begin{figure}[h!!!tb]
\centering
\includegraphics[width=\ttp]{../Pictures/Polars.png}
\caption{\label{Polars} Cartesian and polar coordinates.}
\end{figure}

\fig{Polars} illustrates the fundamental relationships between the Cartesian and the polar coordinates, namely:
\begin{align}
x=r\cos(\theta),\\
y=r\sin(\theta).
\end{align}
Critically, these specify $x$ and $y$ singly as functions $(r,\theta)$. These, in turn, can be used to construct equations for $r$ and $\theta$ separately as functions of $(x,y)$, namely,
\bb
r^2=x^2+y^2,
\ee
and
\bb
\theta=\arctan\l\frac{y}{x}\r,\quad \textrm{or}\quad\theta=\arccos\l\frac{x}{\sqrt{x^2+y^2}}\r,\quad \textrm{or}\quad\theta=\arcsin\l\frac{y}{\sqrt{x^2+y^2}}\r.\label{theta_eqns}
\ee
where the appropriate function $\theta(x,y)$ is chosen depending on which ever is easiest to use.

\begin{example}[frametitle=Cartesian to polar conversion.]
\COL{Suppose
\begin{align}
\dot{x}&=f(x,y),\\
\dot{y}&=g(x,y)
\end{align}
how do we convert the system from Cartesian coordinates to polar coordinates?

First we use the condition that $r^2=x^2+y^2$. Taking derivatives we get
\bb
2r\dot{r}=2x\dot{x}+2y\dot{y}.
\ee
At which point we can exchange all Cartesian coordinates for their polar analogues, namely:
\bb
\dot{r}=\cos(\theta)f(r\cos(\theta),r\sin(\theta))+\sin(\theta)g(r\cos(\theta),r\sin(\theta)).
\ee
Next we need $\dot{\theta}$. Since there are multiple (equivalent) ways of representing $\theta$ there are multiple (equivalent) forms of the derivative, here only one will be presented. Other forms follow exactly the same procedure. We note that
\bb
\dot{x}=f(r\cos(\theta),r\sin(\theta))=\frac{\rd \l r\cos(\theta)\r}{\rd t}=\dot{r}\cos(\theta)-r\sin(\theta)\dot{\theta}.\label{xdot_to_r}
\ee
}\COL{Rearranging \eqn{xdot_to_r} gives
\begin{align}
\dot{\theta}&=\frac{\cos(\theta)^2f+\sin(\theta)\cos(\theta)g-f}{r\sin(\theta)},\\
&=\frac{-\sin(\theta)f+\sin(\theta)\cos(\theta)g}{r\sin(\theta)},\\
&=\frac{\cos(\theta)g-f\sin(\theta)}{r}.
\end{align}
where the arguments of $f$ and $g$ have been suppressed for brevity.}
\end{example}

In the above example we created $\dot{r}$ first and then used this to produce $\dot{\theta}$. In following example we show how to do the substitution all in one go.
\begin{example}[frametitle= A quicker conversion]
\COL{From
\begin{align}
x=r\cos(\theta),\\
y=r\sin(\theta).
\end{align}
we generate
\begin{align}
&\dot{x}=f(x,y)=\dot{r}\cos(\theta)-r\sin(\theta)\dot{\theta},\\
&\dot{y}=g(x,y)=\dot{r}\sin(\theta)+r\cos(\theta)\dot{\theta}.
\end{align}
This can be seen as a set of simultaneous equations and, thus, solved as a matrix problem
\bb
\colvec{2}{f}{g}=\begin{pmatrix}
\cos(\theta)& -r\sin(\theta) \\
\sin(\theta) & r\cos(\theta)
\end{pmatrix}
\colvec{2}{\dot{r}}{\dot{\theta}}.
\ee
The matrix can be inverted to produce
\bb
\frac{1}{r}\begin{pmatrix}
r\cos(\theta)& r\sin(\theta) \\
-\sin(\theta) & \cos(\theta)
\end{pmatrix}\colvec{2}{f}{g}=
\colvec{2}{\dot{r}}{\dot{\theta}},
\ee
which allows us to reproduce
\bb
\dot{r}=\cos(\theta)f+\sin(\theta)g.
\ee
\bb
\dot{\theta}=\frac{\cos(\theta)g-f\sin(\theta)}{r}.
\ee
}
\end{example}
Generally, nonlinear equations are not solvable however, we will see in the next example the polar coordinates can convert nonlinearities in $(x,r)$ to linearities in $(r,\theta)$
\begin{example}[frametitle= Solving ODEs in polar coordinates]
\COL{Consider the following system
\begin{align}
\dot{x}=y+x\l 1-x^2-y^2\r,\label{x_polar}\\
\dot{y}=-x+y\l 1-x^2-y^2\r\label{y_polar}.
\end{align}
Convert to polar coordinates
\begin{align}
\dot{r}&=\frac{xy+x^2\l 1-x^2-y^2\r-xy+y^2\l 1-x^2-y^2\r}{r},\\
&=\frac{(x^2+y^2)\l 1-x^2-y^2\r}{r},\\
&=r(1-r^2).
\end{align}
and
\bb
\dot{x}=y+x\l 1-x^2-y^2\r=\dot{r}\cos(\theta)-r\sin(\theta)\dot{\theta},
\ee
which implies
\begin{align}
\dot{\theta}&=\frac{\dot{r}\cos(\theta)-y-x\l 1-x^2-y^2\r}{r\sin(\theta)},\\
&=\frac{r(1-r^2)\cos(\theta)-r\sin(\theta)-r\cos(\theta)(1+r^2)}{r\sin(\theta)},\\
&=-1.
\end{align}
Assuming initial conditions $\theta(0)=0$ and $r(0)=r_0>0$ we can immediately integrate to get $\theta=-t$ and
\begin{align}
t&=\int^r_{r_0}\frac{1}{r'(1-r'^2)}\rd r',\\
&=\int^r_{r_0}\frac{1}{r'}+\frac{1/2}{1-r'}-\frac{1/2}{1+r'}\rd r',\\
&=\left[\frac{1}{r'}+\frac{1/2}{1-r'}-\frac{1/2}{1+r'}\right]^r_{r_0},\\
&=\left[\ln(r')-1/2\ln(1-r')-1/2\ln(1+r')\right]^r_{r_0},\\
&=\left[\ln\l \frac{r'}{\sqrt{1-r'^2}}\r\right]^r_{r_0},\\
&=\ln\l \frac{r}{\sqrt{1-r^2}}\frac{\sqrt{1-r_0^2}}{r_0}\r,
\end{align}

Rearranging this gives,
\bb
r(t)=\frac{r_0}{\sqrt{r_0^2+e^{-2t}\l 1-r_0^2\r}}.\label{r_lim}
\ee
From \eqn{r_lim} we can see that $r(t)\rightarrow 1$ as $t\rightarrow \infty$. Critically, we can reconstruct the Cartesian solution by considering the polar identities. Firstly, since $\theta=-t$ the solutions spiral at a constant rate. Note the spirals go clockwise as we normally take positive angles to be anticlockwise. Further, any trajectory must head towards the circle $r=1$.

The full dynamics are simulated in \fig{Polar_plot}
}
\end{example}
\begin{figure}[h!!!tb]
\centering
\includegraphics[width=\ttp]{../Pictures/Polar_plot.png}
\caption{\label{Polar_plot} Full dynamics of \eqns{x_polar}{y_polar}.}
\end{figure}
\section{Check list}
By the end of this chapter you should be able to:
\begin{todolist}
\item reproduce all the definitions;
\item state all theorems;
\item solve simple linear ODE systems;
\item prove trajectories of autonomous systems cannot cross themselves;
\item prove that an ODE of one variable cannot oscillate;
\item derive single variable Taylor series of any order;
\item derive multivariate Taylor series up to first order;
\item convert systems of ODE equations of Cartesian variables in to polar variables and back again.
\end{todolist}