# Fourier Series (Part 3)

Source: https://cca.twango.dev/AP-Calculus-BC/Take-Home-Quizzes/quiz-10

# Fourier Series (Part 3)

## Taylor vs. Fourier Series Approximations

We are now going to compare the Taylor approximations and Fourier approximations to the same function to see which is better for approximating values.

Consider the function $f(x)=e^{-x^2}$

1. Write down the first four terms and the general term for the MacLaurin power series of $f(x)$. What is the interval of convergence?

    $$
    \begin{align}
    f(0) &= 1 \\
    f'(0) &= 0 \\
    f''(0) &= -2 \\
    f'''(0) &= 0 \\
    f^{(4)}(0) &= 12 \\
    f^{(6)}(0) &= -120 \\
    \end{align}
    $$

    $$
    \begin{align}
    f(x) &= 1 - \frac{2x^2}{2!} + \frac{12x^4}{4!} - \frac{120x^6}{6!} + \cdots \\
    &= 1 - x^2 + \frac{x^4}{2} - \frac{x^6}{6} + \cdots \\
    e^{-x^2} &= \sum_{n=0}^{\infty} \frac{(-1)^n x^{2n}}{n!}
    \end{align}
    $$

    The interval of convergence is $(-\infty, \infty)$.

2. Let $T_n(x)$ be the $n$th degree Taylor series approximation for $f(x)$. Draw a graph of $f(x)$ and $T_2(x)$ on the same axes on the interval $[-\pi, \pi]$. How good of an approximation is $T_2(x)$ near zero? Near $\pi$?

    <iframe src="https://www.desmos.com/calculator/p1p5rfbc6q?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>

    As the values approach 0, the approximation becomes more accurate. However, as the values approach $\pi$, the approximation becomes less accurate.

3. Let $F_n(x)$ be the Fourier series that goes up to the $nth$ term/coefficient. For example, $F_2(x) = a_0 + a_1 \cos(x) + b_1 \sin(x) + a_2 \cos(2x) + b_2 \sin(2x)$. Using your previous work on Fourier series, find the coefficients for $F_2(x)$. Graph $F_2(x)$ and $f(x)$ on the same axes on the interval $[-\pi, \pi]$. How good of an approximation is $F_2(x)$ near zero? Near $\pi$?

    <iframe src="https://www.desmos.com/calculator/jpywezfdq9?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>

    As the values approach 0, the approximation yields an underestimation. However, as the values approach $\pi$, the approximation yields an overestimation. The approximation around $\pi$ is much better than the previous Taylor Series approximation.

Now we will define something called the least-squares error, which takes the form of $\int_a^b(g_1(x) - g_2(x))^2dx$. The argument in parentheses represents the difference between the actual function and its approximation. Think about why this would be called the "least-squares error."

1. Find the least squares error for $T_4(x)$, $T_6(x)$, and $T_8(x)$.

    $$
    \begin{align}
    T_4(x) &= 1 - x^2 + \frac{x^4}{2} \\
    T_6(x) &= 1 - x^2 + \frac{x^4}{2} - \frac{x^6}{6} \\
    T_8(x) &= 1 - x^2 + \frac{x^4}{2} - \frac{x^6}{6} + \frac{x^8}{24} \\
    \end{align}
    $$

    $$
    \begin{aligned}
    E_4 &= \int_{-\pi}^{\pi}(f(x)-T_4(x))^2dx &\approx 1001.030453 \\
    E_6 &= \int_{-\pi}^{\pi}(f(x)-T_6(x))^2dx &\approx 6416.169822 \\
    E_8 &= \int_{-\pi}^{\pi}(f(x)-T_8(x))^2dx &\approx 25822.818325 \\
    \end{aligned}
    $$

2. Explain why we would expect the least squares error to increase for $T_{2k}$ as $k$ increases (at least for some time)

    For these initial truncations, the endpoint region near $\pm\pi$ dominates the squared error. The alternating exponential terms $x^{2n}/n!$ initially grow when $x^2\approx\pi^2$, so adding a term can increase the error before enough terms are included. The full series converges uniformly on this fixed interval, and the error eventually tends to zero; higher degree does not generally mean a worse Taylor approximation.

3. Find the least squares error for $F_3(x)$, $F_4(x)$, and $F_5(x)$.

    $$
    \begin{aligned}
    a_0 = \frac1{2\pi}\int_{-\pi}^{\pi}e^{-x^2}dx &= 0.28209 \\
    a_1 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\cos(x)dx &= 0.43939 \\
    b_1 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\sin(x)dx &= 0 \\
    a_2 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\cos(2x)dx &= 0.20754 \\
    b_2 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\sin(2x)dx &= 0 \\
    a_3 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\cos(3x)dx &= 0.0594 \\
    b_3 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\sin(3x)dx &= 0 \\
    a_4 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\cos(4x)dx &= 0.0103 \\
    b_4 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\sin(4x)dx &= 0 \\
    a_5 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\cos(5x)dx &= 0.00109 \\
    b_5 = \frac1\pi\int_{-\pi}^{\pi}e^{-x^2}\sin(5x)dx &= 0 \\
    \end{aligned}
    $$

    $$
    \begin{align}
    F_3(x) &= 0.28209 + \cdots + 0.0594\cos(3x) \\
    F_4(x) &= 0.28209 + \cdots + 0.0594\cos(3x) + 0.0103\cos(4x) \\
    F_5(x) &= 0.28209 + \cdots + 0.0594\cos(3x) + 0.0103\cos(4x) + 0.00109\cos(5x) \\
    \end{align}
    $$

    $$
    \begin{aligned}
    E_3 &= \int_{-\pi}^{\pi}(f(x)-F_3(x))^2dx &= 0.0003389 \\
    E_4 &= \int_{-\pi}^{\pi}(f(x)-F_4(x))^2dx &= 0.00000376 \\
    E_5 &= \int_{-\pi}^{\pi}(f(x)-F_5(x))^2dx &= 1.416 \times 10^{-8} \\
    \end{aligned}
    $$

4. Explain why we would expect the least squares error to decrease for $F_k(x)$ as $k$ increases.

    The least squares error decreases for $F_k(x)$ as $k$ increases because the Fourier series approximation becomes more accurate as the number of terms in the series increases. This is because the Fourier series is accurate over the entire interval, and as the number of terms increases, the approximation becomes more accurate.


5. For what value of $k$ does the least squares error of $T_k(x)$ first become less than 0.001? Show your work.

    With $T_k$ defined as a **degree-$k$** polynomial,

    $$T_k(x)=\sum_{n=0}^{\lfloor k/2\rfloor}\frac{(-1)^nx^{2n}}{n!},\qquad E_k=\int_{-\pi}^{\pi}(e^{-x^2}-T_k(x))^2\,dx.$$

    The original Desmos calculation counts the largest summation index instead of polynomial degree:

    <iframe src="https://www.desmos.com/calculator/lxazgvn9xo?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>

    | Largest summation index $n$ | Polynomial degree $k$ | Recomputed squared error |
    |---|---|---|
    | 2 | 4 | 1001.030453 |
    | 3 | 6 | 6416.169822 |
    | 23 | 46 | 0.046312817 |
    | 24 | 48 | 0.007084198 |
    | 25 | 50 | **0.001002047** |
    | 26 | 52 | **0.000131458** |

    Thus the first polynomial degree is **$k=52$**, corresponding to the original summation cutoff 26. Rounding the degree-50 error to “0.001” conceals that it is still greater than the threshold.

6. For what value of $k$ does the least squares error of $F_k(x)$ first become less than 0.00001? Show your work.

    The least squares error of $F_4(x)$ is already less than 0.00001. See #3.


> [!correction]
> The original $T_4,T_6,T_8$ formulas actually had degrees 6, 8, and 10; their errors were 6416.169, 25822.818, and 71126.021. Degree labels and associated errors are corrected above. The derivative values at zero were mislabeled as derivatives for general $x$. The coefficient integrals in the Fourier section lacked their normalization factors, although the numerical coefficients already used them. The error decreases when harmonics are **added**, rather than removed.
>
> The graph embeds retain the original calculations; use the reviewed equations and degree labels in this note. The Fourier coefficients minimize the integrated squared error among trigonometric polynomials of the same order, so adding modes cannot increase this error. [MIT: orthogonal functions and least squares](https://ocw.mit.edu/courses/res-18-008-calculus-revisited-complex-variables-differential-equations-and-linear-algebra-fall-2011/ce644220afe5090bea10c929aeea6817_ZYf0tz9oVz8.pdf).

Related: [[quiz-07|Fourier coefficients for even functions]], [[quiz-08|Odd and general Fourier series]], [[quiz-11|Changing the Fourier interval]].
