Skip to content

Instantly share code, notes, and snippets.

@halserach
Last active March 20, 2024 05:42
Show Gist options
  • Save halserach/d9cc244565502a536fbfde16a94ace07 to your computer and use it in GitHub Desktop.
Save halserach/d9cc244565502a536fbfde16a94ace07 to your computer and use it in GitHub Desktop.
Homework related to fourth assignment.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "973098da-2675-49e2-beae-329c17116732",
"metadata": {},
"source": [
"# Fourier Coefficient Prove: Via Calculus\n",
"\n",
"To find formula for Fourier coefficients of a series with this kind of form:\n",
"$$ f(x) =\\frac{1}{2} a_0 + \\sum_{n=1}^{\\infty} a_n \\cos(nx) + \\sum_{n=1}^{\\infty} b_n \\sin(nx) \\tag{1} $$\n",
"\n",
"We need the following integrals:\n",
"$$ \\int_{-\\pi}^{\\pi} \\sin(mx) \\sin(nx) \\mathrm{d} x = \\pi \\delta_{m,n} \\tag{2.1}, $$\n",
"$$ \\int_{-\\pi}^{\\pi} \\cos(mx) \\cos(nx) \\mathrm{d} x = \\pi \\delta_{m,n}\\tag{2.2}, $$\n",
"for every $ (m,n) $ that if $ m = 0 $, then $ n \\neq 0 $ and vice versa, and\n",
"$$ \\int_{-\\pi}^{\\pi} \\sin(mx) \\cos(nx) \\mathrm{d} x = 0 \\tag{2.3}, $$\n",
"$$ \\int_{-\\pi}^{\\pi} \\cos(nx) \\mathrm{d} x = 2\\pi \\delta_{n,0} \\tag{2.4}, $$\n",
"$$ \\int_{-\\pi}^{\\pi} \\sin(nx) \\mathrm{d} x = 0 \\tag{2.5}, $$\n",
"for all $ (m,n) $\n",
"\n",
"The trick to get the coefficients is by multiply either $ \\sin(mx) $ or $ \\cos(mx) $ to $ \\text{Eq.}(1) $, and then integrate the whole series from $ -\\pi $ to $\\pi$. Let's multiply $ \\cos(mx) $ first, and integrate:\n",
"\\begin{aligned}\n",
"\\int_{-\\pi}^{\\pi}\\cos(mx)f(x)\\mathrm{d} x &= \\int_{-\\pi}^{\\pi}\\cos(mx) \\left [ \\frac{1}{2} a_0 + \\sum_{n=1}^{\\infty} a_n \\cos(nx) + \\sum_{n=1}^{\\infty} b_n \\sin(nx) \\right ] \\mathrm{d} x \\\\\n",
"&= \\frac{1}{2} a_0 \\int_{-\\pi}^{\\pi}\\cos(mx) \\mathrm{d} x + \\sum_{n=1}^{\\infty} a_n \\int_{-\\pi}^{\\pi}\\cos(mx)\\cos(nx) \\mathrm{d} x + \\sum_{n=1}^{\\infty} b_n \\int_{-\\pi}^{\\pi}\\cos(mx)\\sin(nx) \\mathrm{d} x \\\\\n",
"&= a_0 \\pi \\delta_{m,0} + \\sum_{n=1}^{\\infty} a_n \\pi \\delta_{m,n} + 0 \\\\\n",
"\\int_{-\\pi}^{\\pi}\\cos(mx)f(x)\\mathrm{d} x &= a_m \\pi \\\\\n",
"& \\Rightarrow a_m = \\frac{1}{\\pi} \\int_{-\\pi}^{\\pi}\\cos(mx)f(x)\\mathrm{d} x \\hspace{1cm} (3.1)\n",
"\\end{aligned}\n",
"Meanwhile, by multiply $ \\sin(mx) $ to $ \\text{Eq.}(1) $, we get:\n",
"\\begin{aligned}\n",
"\\int_{-\\pi}^{\\pi}\\sin(mx)f(x)\\mathrm{d} x &= \\int_{-\\pi}^{\\pi}\\sin(mx) \\left [ \\frac{1}{2} a_0 + \\sum_{n=1}^{\\infty} a_n \\cos(nx) + \\sum_{n=1}^{\\infty} b_n \\sin(nx) \\right ] \\mathrm{d} x \\\\\n",
"&= \\frac{1}{2} a_0 \\int_{-\\pi}^{\\pi}\\sin(mx) \\mathrm{d} x + \\sum_{n=1}^{\\infty} a_n \\int_{-\\pi}^{\\pi}\\sin(mx)\\cos(nx) \\mathrm{d} x + \\sum_{n=1}^{\\infty} b_n \\int_{-\\pi}^{\\pi}\\sin(mx)\\sin(nx) \\mathrm{d} x \\\\\n",
"&= 0 + 0 + \\sum_{n=1}^{\\infty} b_n \\pi \\delta_{m,n} \\\\\n",
"\\int_{-\\pi}^{\\pi}\\sin(mx)f(x)\\mathrm{d} x &= b_m \\pi \\\\\n",
"& \\Rightarrow b_m = \\frac{1}{\\pi} \\int_{-\\pi}^{\\pi}\\sin(mx)f(x)\\mathrm{d} x \\hspace{1cm} (4.1)\n",
"\\end{aligned}\n",
"We can further simplify them by \"shift\" the integration horizontally (as long as it's within the same period):\n",
"$$ \\Rightarrow a_n = \\frac{1}{\\pi} \\int_{0}^{2\\pi}\\cos(nx)f(x)\\mathrm{d} x \\tag{3.2}, $$\n",
"$$ \\Rightarrow b_n = \\frac{1}{\\pi} \\int_{0}^{2\\pi}\\sin(nx)f(x)\\mathrm{d} x \\tag{4.2} $$\n",
"and by \"stretch\" the integration horizontally, we can replace the $ 2\\pi $ 'default' period with any length period / wavelength $ L $ :\n",
"$$ \\Rightarrow a_n = \\frac{2}{L} \\int_{0}^{L}\\cos \\left (n \\frac{2\\pi}{L} x \\right ) f(x)\\mathrm{d} x \\tag{3.3}, $$\n",
"$$ \\Rightarrow b_n = \\frac{2}{L} \\int_{0}^{L}\\sin \\left (n \\frac{2\\pi}{L} x \\right ) f(x)\\mathrm{d} x \\tag{4.3} $$\n",
"For the $ a_n $ and $ b_n $ is the coefficients of the \"stretched\" series,\n",
"$$ f(x) =\\frac{1}{2} a_0 + \\sum_{n=1}^{\\infty} a_n \\cos \\left (n \\frac{2\\pi}{L} x \\right ) + \\sum_{n=1}^{\\infty} b_n \\sin \\left (n \\frac{2\\pi}{L} x \\right ) \\tag{5} $$\n",
"\n",
"Therefore the proof for Equations (39) - (41) in the first slide. By substitute $ x \\rightarrow t $ and $ L \\rightarrow T $, we got the expression for $ f(t) $ (however the terms for $ f(t) $ in Equations (42) - (45) are wrong, therefore it cannot be proved):\n",
"$$ f(t) =\\frac{1}{2} a_0 + \\sum_{n=1}^{\\infty} a_n \\cos \\left (n \\frac{2\\pi}{T} t \\right ) + \\sum_{n=1}^{\\infty} b_n \\sin \\left (n \\frac{2\\pi}{T} t \\right ) \\tag{6}, $$\n",
"$$ a_n = \\frac{2}{T} \\int_{0}^{T}\\cos \\left (n \\frac{2\\pi}{T} x \\right ) f(x)\\mathrm{d} x \\tag{3.4}, $$\n",
"$$ b_n = \\frac{2}{T} \\int_{0}^{T}\\sin \\left (n \\frac{2\\pi}{T} x \\right ) f(x)\\mathrm{d} x \\tag{4.4} $$"
]
},
{
"cell_type": "markdown",
"id": "3dce582c-9c62-4e4a-96bf-f03aa18af08c",
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-18T10:22:42.689345Z",
"iopub.status.busy": "2024-03-18T10:22:42.688697Z",
"iopub.status.idle": "2024-03-18T10:22:42.696141Z",
"shell.execute_reply": "2024-03-18T10:22:42.695076Z",
"shell.execute_reply.started": "2024-03-18T10:22:42.689314Z"
}
},
"source": [
"## Application of Fourier series: Decomposition (1)\n",
"\n",
"If all of the integrations are finite, then *any* Fourier series converge to *any* periodic function. Let's try Fourier series on a periodic function, like this:\n",
"$$ f(x) = 10 + 2 \\sin \\left(\\frac{2\\pi}{7}x \\right) + 3 \\cos \\left(\\frac{2\\pi}{5}x \\right) $$\n",
"First, what's is the periodicity of the function above? The second term cycle every x multiple of 7, and the third term cycle every x multiple of 5. Thus, f(x) turns one cycle every x multiple of $ 7 \\times 5 = 35 $, so,\n",
"\\begin{align}\n",
"a_n &= \\frac{2}{35} \\int_{0}^{35} \\cos \\left(n \\frac{2\\pi}{35} x \\right) \\left[ 10 + 2 \\sin \\left(\\frac{2\\pi}{7}x \\right) + 3 \\cos \\left(\\frac{2\\pi}{5}x \\right) \\right] \\mathrm{d} x \\\\\n",
"&= \\frac{2}{35} \\left[ 10 \\int_{0}^{35} \\cos \\left(n \\frac{2\\pi}{35} x \\right) \\mathrm{d} x + 2 \\int_{0}^{35} \\cos \\left(n \\frac{2\\pi}{35} x \\right) \\sin \\left(5 \\frac{2\\pi}{35}x \\right) \\mathrm{d} x + 3 \\int_{0}^{35} \\cos \\left(n \\frac{2\\pi}{35} x \\right) \\cos \\left(7 \\frac{2\\pi}{35}x \\right) \\mathrm{d} x \\right] \\\\\n",
"&= \\frac{2}{35} \\left[ 10 \\cdot 35 \\delta_{n,0} + 0 + 3 \\cdot 17.5 \\delta_{n,7} \\right] \\\\\n",
"a_n &= 20 \\delta_{n,0} + 3 \\delta_{n,7}\n",
"\\end{align}\n",
"and,\n",
"\\begin{align}\n",
"b_n &= \\frac{2}{35} \\int_{0}^{35} \\sin \\left(n \\frac{2\\pi}{35} x \\right) \\left[ 10 + 2 \\sin \\left(\\frac{2\\pi}{7}x \\right) + 3 \\cos \\left(\\frac{2\\pi}{5}x \\right) \\right] \\mathrm{d} x \\\\\n",
"&= \\frac{2}{35} \\left[ 10 \\int_{0}^{35} \\sin \\left(n \\frac{2\\pi}{35} x \\right) \\mathrm{d} x + 2 \\int_{0}^{35} \\sin \\left(n \\frac{2\\pi}{35} x \\right) \\sin \\left(5 \\frac{2\\pi}{35}x \\right) \\mathrm{d} x + 3 \\int_{0}^{35} \\sin \\left(n \\frac{2\\pi}{35} x \\right) \\cos \\left(7 \\frac{2\\pi}{35}x \\right) \\mathrm{d} x \\right] \\\\\n",
"&= \\frac{2}{35} \\left[ 0 + 2 \\cdot 17.5 \\delta_{n,5} + 0 \\right] \\\\\n",
"b_n &= 2 \\delta_{n,5}\n",
"\\end{align}\n",
"Only $a_0 = 20$, $a_7 = 3 $, and $b_5 = 2$ that are non-zero in the series. Thus we get:\n",
"\\begin{align}\n",
"f(x) &= \\frac{1}{2}a_0 + a_7 \\cos \\left(7 \\frac{2\\pi}{35} x \\right) + b_5 \\sin \\left(5 \\frac{2\\pi}{35} x \\right) \\\\\n",
"f(x) &= 10 + 2 \\sin \\left(\\frac{2\\pi}{7}x \\right) + 3 \\cos \\left(\\frac{2\\pi}{5}x \\right) \n",
"\\end{align}\n",
"Therefore we've shown that Fourier Series can decompose a periodic function into some harmonic functions whose frequencies are an integer multiplication to a basic frequency (in this case, the basic frequency is 1/35 Hz)."
]
},
{
"cell_type": "markdown",
"id": "363503f8-6237-4cff-bdd0-bac92ee3a8af",
"metadata": {},
"source": [
"### What if our base period has no common factor with the periods of each harmonic component in $ f(x) $?\n",
"\n",
"First, the more general form of $ (2.1) $ and $ (2.2) $ with the boundary of integration from $0$ to $2\\pi$ is:\n",
"$$ \\int_{0}^{2\\pi} \\sin(mx)\\sin(nx) \\mathrm{d} x = \\frac{\\sin \\left( (m-n) \\pi\\right)}{2(m-n)} - \\frac{\\sin \\left( (m+n) \\pi\\right)}{2(m+n)} \\tag{5.1} $$\n",
"$$ \\int_{0}^{2\\pi} \\cos(mx)\\cos(nx) \\mathrm{d} x = \\frac{\\sin \\left( (m-n) \\pi\\right)}{2(m-n)} + \\frac{\\sin \\left( (m+n) \\pi\\right)}{2(m+n)} \\tag{5.2} $$\n",
"\n",
"If $m$ and $n$ are integers, then expressions above are reduced to $ (2.1) $ and $ (2.2) $, and the only way we make it into an integer is if we use Fourier period that can be fully divided by all the constituent periods of each harmonic component in $ f(x) $ (the variable $ x $ can be 'stretched' by a factor of 1/L).\n",
"\n",
"If $m$ and $n$ are non-integer real number, then the value of $(5.1)$ and $(5.2)$ will always be non-zero. Thus, there would be many non-zero $a_n$ and $b_n$ other than $a_0$, $a_7$, and $b_5$.\n",
"\n",
"It's because the series tries to approximate an periodic signal but none of the harmonic oscillation in the series match any harmonic terms in $f(x)$. But, the larger the L, the better the approximation will be (it'll not be as perfect as if our series base period L contains the factor both in all of the periods of each harmonic component in $ f(x) $, unless if $L \\rightarrow \\infty$).\n",
"\n",
"To show this, we will use _sympy_'s integration library and recalculate the Fourier coefficients for $f(x)$ above, but with $ L = 20 $."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0144375c-058f-44a7-97b8-b04cb1dd623d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-20T04:10:35.634196Z",
"iopub.status.busy": "2024-03-20T04:10:35.633596Z",
"iopub.status.idle": "2024-03-20T04:10:39.021831Z",
"shell.execute_reply": "2024-03-20T04:10:39.020734Z",
"shell.execute_reply.started": "2024-03-20T04:10:35.634173Z"
}
},
"outputs": [],
"source": [
"from sympy import *\n",
"\n",
"x = Symbol('x')\n",
"\n",
"def fun(x):\n",
" return 10 + 2*sin(2*pi*x/7) + 3*cos(2*pi*x/5)\n",
"\n",
"# Define L\n",
"L = 20\n",
"\n",
"# Calculate c0\n",
"c0 = float((1/L)*integrate(fun(x), (x, 0, L)))\n",
"\n",
"# Calculate a_n and b_n\n",
"an = []\n",
"bn = []\n",
"for idx in range(1,10):\n",
" a_i = float((2/L)*integrate(fun(x) * cos(2*idx*pi*x/L), (x, 0, L)))\n",
" b_i = float((2/L)*integrate(fun(x) * sin(2*idx*pi*x/L), (x, 0, L)))\n",
" an.append(a_i)\n",
" bn.append(b_i)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7ce4c222-1c0d-4765-81a2-9bd707af3a51",
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-20T04:10:39.023933Z",
"iopub.status.busy": "2024-03-20T04:10:39.023432Z",
"iopub.status.idle": "2024-03-20T04:10:39.034388Z",
"shell.execute_reply": "2024-03-20T04:10:39.033284Z",
"shell.execute_reply.started": "2024-03-20T04:10:39.023907Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a_n</th>\n",
" <th>b_n</th>\n",
" <th>c</th>\n",
" </tr>\n",
" <tr>\n",
" <th>n</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>10.041946</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.095604</td>\n",
" <td>-0.069484</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.164496</td>\n",
" <td>-0.239105</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>-0.818467</td>\n",
" <td>1.784542</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2.912612</td>\n",
" <td>0.254049</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>-0.040675</td>\n",
" <td>0.147811</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>-0.024602</td>\n",
" <td>0.107282</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>-0.016770</td>\n",
" <td>0.085318</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>-0.012265</td>\n",
" <td>0.071312</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>-0.009402</td>\n",
" <td>0.061501</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a_n b_n c\n",
"n \n",
"0 0.000000 0.000000 10.041946\n",
"1 0.095604 -0.069484 0.000000\n",
"2 0.164496 -0.239105 0.000000\n",
"3 -0.818467 1.784542 0.000000\n",
"4 2.912612 0.254049 0.000000\n",
"5 -0.040675 0.147811 0.000000\n",
"6 -0.024602 0.107282 0.000000\n",
"7 -0.016770 0.085318 0.000000\n",
"8 -0.012265 0.071312 0.000000\n",
"9 -0.009402 0.061501 0.000000"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"results = [[0,0,c0]]\n",
"\n",
"for ai, bi in zip(an,bn):\n",
" results.append([ai, bi, 0])\n",
"\n",
"df = pd.DataFrame(results, columns=[\"a_n\", \"b_n\", \"c\"])\n",
"\n",
"df.index.name = 'n'\n",
"df.round(1)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "8f522120-91e7-4993-afe4-7c219d331b5e",
"metadata": {},
"source": [
"The maximum $a_n$ value is at $a_4 = 2.91$, which is the coefficient of a cosine function with a wavelength of $ \\left( \\frac{20}{4} \\right) = 5 $, and the maximum $b_n$ value is at $b_3 = 1.78$, which is the coefficient of a sine function with a wavelength of $ \\left( \\frac{20}{3} \\right) = 6.66 $, close to $ 7 $.\n",
"\n",
"(In the table, $ c[0] = \\frac{1}{2} a_0 $.)"
]
},
{
"cell_type": "markdown",
"id": "9bbdeab3-282b-48d7-90a5-c2756e0006fb",
"metadata": {},
"source": [
"## Application of Fourier series: Decomposition (2)\n",
"\n",
"As another example, let's decompose this function:\n",
"$$ f(t) = 2 + 5 \\cos(20\\pi t) + 3\\sin(8 \\pi t) \\tag{6} $$\n",
"\n",
"The basic period is 2s, or the basic frequency is 0.5Hz.\n",
"\n",
"To calculate the integration in the coefficients, we will use _sympy_ again."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "98e0e100-cb96-4306-b301-f32a234db916",
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-20T04:10:39.036149Z",
"iopub.status.busy": "2024-03-20T04:10:39.035573Z",
"iopub.status.idle": "2024-03-20T04:10:48.621008Z",
"shell.execute_reply": "2024-03-20T04:10:48.620007Z",
"shell.execute_reply.started": "2024-03-20T04:10:39.036120Z"
}
},
"outputs": [],
"source": [
"from sympy import *\n",
"\n",
"t = Symbol('t')\n",
"\n",
"def func(t):\n",
" return 2+5*cos(20*pi*t)+3*sin(8*pi*t)\n",
"\n",
"# Define f1 = 0.5Hz or T = 2s\n",
"T = 2\n",
"\n",
"# Calculate c0\n",
"c0 = float((1/T)*integrate(func(t), (t, 0, T)))\n",
"\n",
"# Calculate a_n and b_n\n",
"an = []\n",
"bn = []\n",
"for idx in range(1,26):\n",
" a_i = float((2/T)*integrate(func(t) * cos(2*idx*pi*t/T), (t, 0, T)))\n",
" b_i = float((2/T)*integrate(func(t) * sin(2*idx*pi*t/T), (t, 0, T)))\n",
" an.append(a_i)\n",
" bn.append(b_i)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "90d5f01d-7148-4c20-99e6-d4d22ed8b7d2",
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-20T04:10:48.623065Z",
"iopub.status.busy": "2024-03-20T04:10:48.622581Z",
"iopub.status.idle": "2024-03-20T04:10:48.635769Z",
"shell.execute_reply": "2024-03-20T04:10:48.634956Z",
"shell.execute_reply.started": "2024-03-20T04:10:48.623040Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a_n</th>\n",
" <th>b_n</th>\n",
" <th>c</th>\n",
" </tr>\n",
" <tr>\n",
" <th>n</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>0.0</td>\n",
" <td>3.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>5.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a_n b_n c\n",
"n \n",
"0 0.0 0.0 2.0\n",
"1 0.0 0.0 0.0\n",
"2 0.0 0.0 0.0\n",
"3 0.0 0.0 0.0\n",
"4 0.0 0.0 0.0\n",
"5 0.0 0.0 0.0\n",
"6 0.0 0.0 0.0\n",
"7 0.0 0.0 0.0\n",
"8 0.0 3.0 0.0\n",
"9 0.0 0.0 0.0\n",
"10 0.0 0.0 0.0\n",
"11 0.0 0.0 0.0\n",
"12 0.0 0.0 0.0\n",
"13 0.0 0.0 0.0\n",
"14 0.0 0.0 0.0\n",
"15 0.0 0.0 0.0\n",
"16 0.0 0.0 0.0\n",
"17 0.0 0.0 0.0\n",
"18 0.0 0.0 0.0\n",
"19 0.0 0.0 0.0\n",
"20 5.0 0.0 0.0\n",
"21 0.0 0.0 0.0\n",
"22 0.0 0.0 0.0\n",
"23 0.0 0.0 0.0\n",
"24 0.0 0.0 0.0\n",
"25 0.0 0.0 0.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"results = [[0,0,c0]]\n",
"\n",
"for ai, bi in zip(an,bn):\n",
" results.append([ai, bi, 0])\n",
"\n",
"df = pd.DataFrame(results, columns=[\"a_n\", \"b_n\", \"c\"])\n",
"\n",
"df.index.name = 'n'\n",
"df.round(1)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "a1982ce9-12df-4132-83f2-7f856fa70794",
"metadata": {},
"source": [
"So, \n",
"\\begin{align}\n",
"f(t) &= c_0 + \\sum_{n=1}^{\\infty} a_n \\cos \\left(n \\left( \\frac{2\\pi}{T} \\right) t \\right) + \\sum_{n=1}^{\\infty} b_n \\sin \\left(n \\left( \\frac{2\\pi}{T} \\right) t \\right) \\\\\n",
"&= c_0 + a_{20} \\cos \\left(20 \\left( \\frac{2\\pi}{2} \\right) t \\right) + b_8 \\sin \\left(8 \\left( \\frac{2\\pi}{2} \\right) t \\right) \\\\\n",
"&= 2 + 5 \\cos(20\\pi t) + 3\\sin(8 \\pi t)\n",
"\\end{align}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@halserach
Copy link
Author

(For some reason, gist add additional equation tags, even within the same equation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment