\documentclass{article}
\usepackage{linalg}
\usepackage{amsmath, amsfonts}
\usepackage{graphicx}
\title{Math 2270-2 Spring 2012\\ Computer Lab 4\\ Fourier Series}
\date{}
\begin{document}
\maketitle
This lab was written by Professor Chris Cashen in his Postdoc years at Utah. Some minor display, maple code display, and font size changes were made.
It remains exactly the same as Chris Cashen wrote.

 {\bf Submit your project} on the due date in class as a worksheet print. {\bf Submit study group} efforts as one worksheet print with multiple names.

\bigskip

See Section 8.5 of the text for problem background.

\section{Introduction}
Let $\bar{v}=<v_1,v_2,v_3>$ be a vector in $\Rr^3$. The vectors
$\vec{imath}=<1,0,0>$, $\vec{jmath}=<0,1,0>$ and $\vec{k}=<0,0,1>$ are an
orthonormal basis for $\Rr^3$, and $\vec{v}$ can be expressed
as a linear combination of these vectors as
\[\vec{v}=v_1\vec{imath}+v_2\vec{jmath}+v_3\vec{k}\]

Similarly, for any orthonormal basis $\vec{a}_1$, $\vec{a}_2$,
$\vec{a}_3$ we can write $\vec{v}$ as a linear combination
\[\vec{v}=(\vec{v}\cdot\vec{a}_1)\vec{a}_1+(\vec{v}\cdot\vec{a}_2)\vec{a}_2+(\vec{v}\cdot\vec{a}_3)\vec{a}_3\]

More generally still, we can do this for any orthogonal basis using
projection formulae. If $\vec{a}_1$, $\vec{a}_2$,
$\vec{a}_3$ form a basis of orthogonal vectors, we can write $\vec{v}$ as a linear combination
\[\vec{v}=\left(\frac{\vec{v}\cdot\vec{a}_1}
{\vec{a}_1\cdot\vec{a}_1}\right)\vec{a}_1+
\left (\frac{\vec{v}\cdot\vec{a}_2}{\vec{a}_2\cdot\vec{a}_2}\right)
\vec{a}_2+\left (\frac{\vec{v}\cdot\vec{a}_3}
{\vec{a}_3\cdot\vec{a}_3}\right)\vec{a}_3\]

In this lab we will be interested in rewriting vectors in terms of an
orthogonal basis, but the vector space will consist of
$2\pi$--periodic functions. You can verify that this is a
vector space: it is closed under linear combinations and the zero
function serves as the zero vector. The first order of business will
be to find a basis. It turns out that this vector space is infinite dimensional.

\section{An Orthogonal Basis}
We first define an inner product of vectors to take the place of the
usual dot product of $\Rr^n$.
If $f$ and $g$ are two functions in the vector space, define their
inner product to be:
\[(f,g)=\int_0^{2\pi}f(x)g(x)dx\]

Now we claim that a basis for the vector space is given by the
following infinite list of functions:
$$\begin{array}{l}
 \cos(mx)\qquad\text{for }m=0,1,2,\\dots\\
 \sin(nx)\qquad\text{for }n=1,2,\d\ots
\end{array}$$

First, these ``vectors'' are all $2\pi$--periodic functions, so they
are all in the vector space. They are all non-zero functions (which is
why we have not included $\sin(0x)$).

They are linearly independent - in fact, they form
an orthogonal set. To verify this we check that if you take the inner
product of any two of them you get 0. You may remember these formulae
from a table of integrals from Calculus II.

\begin{gather*}
  \int_0^{2\pi}\cos(mx)\cos(nx)dx=0\qquad\text{for }m\neq n\\
 \int_0^{2\pi}\sin(mx)\sin(nx)dx=0\qquad\text{for }m\neq n\\
 \int_0^{2\pi}\cos(mx)\sin(nx)dx=0\\
\end{gather*}

They are, however, not unit length.
\begin{gather*}
  \int_0^{2\pi}\cos(0x)\cos(0x)dx=\int_0^{2\pi}1\cdot 1 dx=2\pi\\
 \int_0^{2\pi}\cos(mx)\cos(mx)dx=\pi \qquad\text{for } m\neq 0\\
 \int_0^{2\pi}\sin(nx)\sin(nx)dx=\pi\qquad\text{for } n\neq 0
\end{gather*}

Verify these six formulae in Maple. To get the integrals to evaluate
we must make assumptions on the parameters $m$ and $n$.

\begin{gather*}
 assume(\, m>0,\, m::integer,\, n>0,\, n::integer,\, m!=n);\\
int(cos(m*x)*sin(n*x), x=0..2Pi);
\end{gather*}

The fact that they are a spanning set for a vector space of
$2\pi$--periodic functions is
Fourier's Theorem. There are some technicalities here that we will
ignore. The complications are due to the fact that the vector space is
infinite dimensional, so we will express a function as an infinite series
of functions, and we must worry about conditions under which such a
series converges. It is true that for ``well-behaved'' $2\pi$--periodic
functions the series will converge.

\section{Expressing a Vector in Terms of the Basis}
Given a ``nice'' $2\pi$--periodic function $f(x)$, we want to express
it in terms of the basis of sine and cosine functions. For each
$m=0,1,\dots$ and $n=1,2,\dots$ we need to find the $\cos(mx)$ and
$\sin(nx)$ parts of $f$. We do this with the usual projection formula.

For any $m=0,1,\dots$, the $\cos(mx)$ part of $f$ is $a_m\cos(mx)$
where
\[a_m=\frac{(f,\cos(mx))}{(\cos(mx),\cos(mx))}=
\begin{cases}
\frac{1}{2\pi}  \int_0^{2\pi}f\cos(mx)dx \qquad\text{if }m=0\\
\frac{1}{\pi}  \int_0^{2\pi}f\cos(mx)dx \qquad\text{if }m>0
\end{cases}\]

Similarly, for $n=1,2,\dots$, the $\sin(nx)$ part of $f$ is
$b_n\sin(nx)$ where
\[b_n=\frac{(f,\sin(nx))}{(\sin(nx),\sin(nx))}=\frac{1}{\pi}  \int_0^{2\pi}f\sin(nx)dx\]

Then the function $f$ can be written as a sum of its
parts as
\[f(x)\approx a_0\cos(0x)+a_1\cos(1x)+b_1\sin(1x)+a_2\cos(2x)+b_2\sin(2x)+\dots\]


Here is an example. Let $f(x)$ be the $2\pi$--periodic function whose
value is $\pi$ between 0 and $\pi$ and $-\pi$ from $\pi$ to $2\pi$. In Maple,
define this piecewise so that it is correct on the interval from
$-2\pi$ to $2\pi$ and plot it:
\begin{gather*}
  f:= x-> piecewise(x<=-Pi , \, Pi,\, x<0,\, -Pi,\, x<=Pi,\,
  Pi,\,-Pi);\\plot(f(x), x=-2Pi..2Pi, discont=false, thickness=3);
\end{gather*}

The graph of $f$ is described as a ``square wave''.

The sine parts we find by computing
\[b_n=\frac{1}{\pi}\int_0^{2\pi}f(x)\sin(nx)dx\]

The fact that $f$ is an odd function implies that all the cosine parts
are 0. Verify this.

So, we claim that \[f(x)\approx \frac{4}{1}\sin(1x)+\frac{4}{3}\sin(3x)+\frac{4}{5}\sin(5x)+\dots\]
You may remember from Calculus II that the Taylor Series expresses a
function as a series of powers of $x$, and that the series can be
approximated by taking a Taylor Polynomial consisting of finitely many
terms. We have the same idea here: we have a Fourier Series converging
to a function representing $f$, and we can stop after finitely many
terms to get a Fourier Sum approximating $f$. The more terms we
include the better the approximation will be. Try plotting the first four
Fourier Sums on the same axes with $f$:

\begin{gather*}
plot([ f(x),\,4\sin(x)\,], x=-2Pi..2Pi, discont=false,
color=[red,blue], thickness=[3,1]);\\
plot([ f(x),\,4\left(\sin(x)+\frac{1}{3}\sin(3x)\right)\,], x=-2Pi..2Pi, discont=false,
color=[red,blue], thickness=[3,1]);\\
etc\dots
\end{gather*}

Let's parse this Maple command: the first argument of the ``plot''
function is a list, in square brackets, of 2 functions to plot,
separated by commas. The next argument says to display the plot on
$x$-values from $-2\pi$ to $2\pi$ (the $y$--values will be determined
automatically). The remaining arguments specify
options. ``discont=false'' says to draw the graphs as if they were
continuous, even though $f$, for instance, is not
continuous. ``color''  and ``thickness'' define these attributes for
the graphs of the corresponding functions. The first function from the
list will be graphed with color red and thickness 3, etc.

\section{Energy Spectrum}
The $k$--th harmonic of $f$ is the frequency $\frac{k}{2\pi}$ part of
the Fourier Series: $a_k\cos(kx)+b_k\sin(kx)$.

The \emph{energy of the $k$-th harmonic} is $a_k^2+b_k^2$. There is an
Energy Theorem that says that the energy of a function is equal to the
sum of the energies of the harmonics.
Since the Fourier Series converges, these energies must limit to 0 as
$k $gets large. It is often useful to know which frequencies have
relatively large energies.

A trivial example is the function $\cos(2x)$. The Fourier Series for
this function is $\cos(2x)$ (the function is already a member of the
basis, there is no need to rewrite it). Thus, the function is equal to
its second harmonic, and all of the energy is concentrated in the
second harmonic.

For the square wave example in the previous section, the even
harmonics are all 0, and harmonic $2k+1$ has energy $\frac{16}{n^2}$,
so the energies decay quadratically. We can plot these energies using
the ``ColumnGraph'' command from the Statistics package:

\[with(Statistics): \qquad ColumnGraph([0,
\frac{16}{1},0,\frac{16}{9},0,\frac{16}{25}, 0, \frac{16}{49}], width = 0.07, distance = .93)  \]

Visually, this graph shows that most of the energy of the square wave is contained in the
first harmonic.



\end{document} 
