<jupyter_start><jupyter_text>Let's look at Fourier series using `interactive`A Fourier series is a function representation (or approximation)for a periodic function usefulin many applications of mathematics such as electrical engineering.One variety of fourier series for a function represents the function as a sum oftrigonometric curves:$$f(x) = \frac{1}{2} a_0 +  \sum_{n = 1}^{\infty}     a_n \cos (n x) +  \sum_{n = 1}^{\infty}     b_n \sin (n x)$$Here is a simplified version of a fourier series using just a few $a$s:<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

def fourier3(x, a1, a2, a3):
    return a1 * np.cos(x) + a2 * np.cos(2*x) + a3 * np.cos(3*x)

fourier3(1,2,3,4)
# What does a fourier approximation look like?
def plot_fourier3(a1, a2, a3):
    low = 0
    high = 4 * np.pi
    x = np.linspace(low, high, 100)
    y = fourier3(x, a1, a2, a3)
    fig, ax = plt.subplots()
    ax.plot(x, y)
    ax.grid(True)
    plt.show()
    
plot_fourier3(1,2,3)
# We can use interact to interact with the approximation
from ipywidgets import interact

interact(plot_fourier3, a1=(-5.1,5), a2=(-5.1,5), a3=(-5.1,5))<jupyter_output><empty_output><jupyter_text>ExerciseCreate `fourier5` by including $b_1$ and $b_2$ from the definition$$f(x) = \frac{1}{2} a_0 +  \sum_{n = 1}^{\infty}     a_n \cos (n x) +  \sum_{n = 1}^{\infty}     b_n \sin (n x)$$And use an interactive interface to `fourier5` to try to find $a$ and $b$values which approximate a "step" function that looks similar to this:<jupyter_code>step_x = [0, 1, 1, 2, 2, 3]
step_y = [-1, -1, 1, 1, -1, -1]
fig, ax = plt.subplots()
ax.plot(step_x, step_y)
#ax.grid(True)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14377556984219755, 'fraction_numerical': 0.040911747516072475, 'mean_word_length': 4.095238095238095, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '554453', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 696, 'n_tokens_neox': 666, 'n_words': 207, 'path': 'notebooks/Interactive Fourier Series.ipynb', 'repo_name': 'flatironinstitute/mfa_jupyter_for_programming', 'size': '1711'}