<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Calculus &#8211; BetterExplained</title>
	<atom:link href="https://betterexplained.com/articles/category/math/calculus/feed/" rel="self" type="application/rss+xml" />
	<link>https://betterexplained.com</link>
	<description>Math lessons that click</description>
	<lastBuildDate>Sat, 20 Aug 2022 03:09:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Intuitive Guide to Convolution</title>
		<link>https://betterexplained.com/articles/intuitive-convolution/</link>
					<comments>https://betterexplained.com/articles/intuitive-convolution/#disqus_thread</comments>
		
		<dc:creator><![CDATA[kalid]]></dc:creator>
		<pubDate>Mon, 23 Nov 2020 19:08:46 +0000</pubDate>
				<category><![CDATA[Calculus]]></category>
				<category><![CDATA[Convolution]]></category>
				<category><![CDATA[Fourier Transform]]></category>
		<guid isPermaLink="false">https://betterexplained.com/?p=12383</guid>

					<description><![CDATA[Like making engineering students squirm? Have them explain convolution and (if you're barbarous) the convolution theorem. They'll mutter something about sliding windows as they try to escape through one. Convolution is usually introduced with its formal definition: Yikes. Let's start without calculus: Convolution is fancy multiplication. Part 1: Hospital Analogy Imagine you manage a hospital [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Like making engineering students squirm? Have them explain convolution and (if you're barbarous) the convolution theorem. They'll mutter something about sliding windows as they try to escape through one.</p>

<p>Convolution is usually introduced with its formal definition:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/3b38c59c70f4d4c69bbc5d81c2fd0d91.png' width='250' height='43' alt='\displaystyle{ (f * g )(t) = \int_{-\infty}^\infty f(\tau) g(t - \tau) d\tau }' class='tex'></p>

<p>Yikes. Let's start <em>without</em> calculus: <strong>Convolution is fancy multiplication.</strong></p>
</p>

<h2><span id="Part_1_Hospital_Analogy">Part 1: Hospital Analogy</span></h2>

<p>Imagine you manage a hospital treating patients with a single disease. You have:</p>

<ul>
<li><strong>A treatment plan:</strong> <code>[3]</code> Every patient gets 3 units of the cure on their first day.</li>
<li><strong>A list of patients:</strong> <code>[1 2 3 4 5]</code> Your patient count for the week (1 person Monday, 2 people on Tuesday, etc.).</li>
</ul>

<p>Question: How much medicine do you use each day? Well, that's just a quick multiplication:</p>

<pre><code>Plan  *  Patients       = Daily Usage
[3]   *  [1 2 3 4 5]    = [3 6 9 12 15]
</code></pre>

<p>Multiplying the plan by the patient list gives the usage for the upcoming days: <code>[3 6 9 12 15]</code>. Everyday multiplication (<code>3 x 4</code>) means using the plan with a single day of patients: <code>[3] * [4] = [12]</code>.</p>

<h3><span id="Intuition_For_Convolution">Intuition For Convolution</span></h3>

<p>Let's say the disease mutates and requires a multi-day treatment. You create a new plan: <code>Plan: [3 2 1]</code></p>

<p>That means 3 units of the cure on the first day, 2 on the second, and 1 on the third. Ok. Given the same patient schedule  <code>[1 2 3 4 5]</code>, what's our medicine usage each day?</p>

<p>Uh... shoot. It's not a quick multiplication:</p>

<ul>
<li>On Monday, 1 patient comes in. It's her first day, so she gets 3 units.</li>
<li>On Tuesday, the Monday gal gets 2 units (her second day), but two new patients arrive, who get 3 each (2 * 3 = 6). The total is 2 + (2 * 3) = 8 units.</li>
<li>On Wednesday, it's trickier: The Monday gal finishes (1 unit, her last day), the Tuesday people get 2 units (2 * 2), and there are 3 new Wednesday people... argh.</li>
</ul>

<p>The patients are overlapping and it's hard to track. How can we organize this calculation?</p>

<p>An idea: imagine <em>flipping</em> the patient list, so the first patient is on the right:</p>

<pre><code>           Start of line
 5 4 3 2 1
</code></pre>

<p>Next, imagine we have 3 separate rooms where we apply the proper dose:</p>

<pre><code> Rooms     3 2 1              
</code></pre>

<p>On your first day, you walk into the first room and get 3 units of medicine. The next day, you walk into room #2 and get 2 units. On the last day, you walk into room #3 and get 1 unit. There's no rooms afterwards, and your treatment is done.</p>

<p>To calculate the total medicine usage, line up the patients and walk them through the rooms:</p>

<pre><code> Monday
 ----------------------------
 Rooms                  3 2 1                                      
 Patients       5 4 3 2 1

 Usage                  3
</code></pre>

<p>On Monday (our first day), we have a single patient in the first room. She gets 3 units, for a total usage of 3. Makes sense, right?</p>

<p>On Tuesday, everyone takes a step forward:</p>

<pre><code> Tuesday
 ----------------------------
 Rooms                  3 2 1              
 Patients -&gt;      5 4 3 2 1

 Usage                  6 2      = 8
</code></pre>

<p>The first patient is now in the second room, and there's 2 new patients in the first room. We multiply each room's dose by the patient count, then combine.</p>

<p>Every day we just walk the list forward:</p>

<pre><code> Wednesday
 ----------------------------
 Rooms                  3 2 1              
 Patients -&gt;        5 4 3 2 1

 Usage                  9 4 1    = 14


 Thursday
 -----------------------------
 Rooms                  3 2 1              
 Patients -&gt;          5 4 3 2 1

 Usage                 12 6 2    = 20


 Friday
 -----------------------------
 Rooms                  3 2 1              
 Patients -&gt;            5 4 3 2 1

 Usage                 15 8 3    = 26
</code></pre>

<p>Whoa! It's intricate, but we figured it out, right? We can find the usage for any day by reversing the list, sliding it to the desired day, and combining the doses.</p>

<p>The total day-by-day usage looks like this (don't forget Sat and Sun, since some patients began on Friday):</p>

<pre><code>Plan      *  Patient List   = Total Daily Usage

[3 2 1]   *  [1 2 3 4 5]    = [3 8 14 20 26 14 5]
              M T W T F        M T W  T  F  S  S
</code></pre>

<p>This calculation is the <em>convolution</em> of the plan and patient list. It's a fancy multiplication between a list of input numbers and a "program".</p>

<h3><span id="Interactive_Demo">Interactive Demo</span></h3>

<p>Here's a <a href="https://www.desmos.com/calculator/uxtsoxmppq">live demo</a>. Try changing <code>F</code> (the plan) or <code>G</code> (the patient list). The convolution <img src="https://betterexplained.com/latex/5b8b0db110679d1177f3.png" alt="$c(t)$" width="29" height="19" style="vertical-align:-4.53px" class="bex-math"></img> matches our manual calculation above.</p>

<iframe src="https://www.desmos.com/calculator/lrcayuc5te" width="100%" height="700px" style="left: 0%; max-width: 100vw; position: relative; width: 100vw; max-width: 900px;" frameborder=0></iframe>

<p>(We define functions <img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img> and <img src="https://betterexplained.com/latex/67db49cba7a7b09a6950.png" alt="$g(x)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img> to pad each list with zero, and adjust for the list index starting at 1.)</p>

<p>You can do a quick convolution with <a href="https://www.wolframalpha.com/input/?i=ListConvolve%5B%7B3%2C+2%2C+1%7D%2C+%7B1%2C+2%2C+3%2C+4%2C+5%7D%2C+%7B1%2C+-1%7D%2C+0%5D+">Wolfram Alpha</a>:</p>

<pre><code>ListConvolve[{3, 2, 1}, {1, 2, 3, 4, 5}, {1, -1}, 0]
{3, 8, 14, 20, 26, 14, 5}
</code></pre>

<p>(The extra <code>{1, -1}, 0</code> aligns the lists and pads with zero.)</p>

<h3><span id="Application_COVID_Ventilator_Usage">Application: COVID Ventilator Usage</span></h3>

<p>I started this article 5 years ago (intuition takes a while...), but unfortunately the analogy is relevant today.</p>

<p>Let's use convolution to estimate ventilator usage for incoming patients.</p>

<ul>
<li>Set <img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img> as the percent of patients needing ventilators. For example, <code>[.05 .03 .01]</code> means 5% of patients need ventilators the first week, 3% the second week, and 1% the third week.</li>
<li>Set <img src="https://betterexplained.com/latex/67db49cba7a7b09a6950.png" alt="$g(x)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img> as the weekly incoming patients, in thousands.</li>
<li>The convolution <img src="https://betterexplained.com/latex/3caf58f7c352a8c0e542.png" alt="$c(t) = f * g$" width="89" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, shows how many ventilators are needed each week (in thousands). <img src="https://betterexplained.com/latex/7929c9805f7d2b4a4997.png" alt="$c(5)$" width="31" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is how many ventilators are needed 5 weeks from now.</li>
</ul>

<p>Let's try it out:</p>

<ul>
<li><code>F = [.05, .03, .01]</code> is the ventilator use percentage by week</li>
<li><code>G = [10, 20, 30, 20, 10, 10, 10]</code>, is the incoming hospitalized patients. It starts at 10k per week, rises to 30k, then decays to 10k.</li>
</ul>

<p>With these numbers, we expect a max ventilator use of 2.2k in 2 weeks:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/convolution/Convolution_Demo-covid.png" alt="Convolution_Demo" class="webpexpress-processed"></p>

<p>The convolution drops to 0 after 9 weeks because the patient list has run out. In this example, we're interested in the peak value the convolution hits, not the long-term total.</p>

<p>Other plans to convolve may be drug doses, vaccine appointments (one today, another a month from now), reinfections, and other complex interactions.</p>

<p>The hospital analogy is the mental model I wish I had when learning. Now that we've tried it with actual numbers, let's add the Math Juice and convert the analogy to calculus.</p>

<h2><span id="Part_2_The_Calculus_Definition">Part 2: The Calculus Definition</span></h2>

<p>So, what happened in our example? We had a list of patients and a plan. If the plan were simple (single day <code>[3]</code>), regular multiplication would have worked. Because the plan was complex, we had to "convolve" it.</p>

<p>Time for some Fun Facts&#x2122;:</p>

<ul>
<li><p>Convolution is written <img src="https://betterexplained.com/latex/39d7d2e7d2ebe3ce2ebc.png" alt="$f * g$" width="36" height="17" style="vertical-align:-3.71px" class="bex-math"></img>, with an asterisk. Yes, an asterisk usually indicates multiplication, but in advanced calculus class, it indicates a convolution. Regular multiplication is just implied (<img src="https://betterexplained.com/latex/78104d82be8c6a943e1d.png" alt="$fg$" width="19" height="17" style="vertical-align:-3.71px" class="bex-math"></img>).</p></li>
<li><p>The result of a convolution is a new <em>function</em> that gives the total usage for any day ("What was the total usage on day <img src="https://betterexplained.com/latex/472ee22169c1b2c44993.png" alt="$t=3$" width="40" height="14" style="vertical-align:-1.49px" class="bex-math"></img>?"). We can graph the convolution over time to see the day-by-day totals.</p></li>
</ul>

<p>Now the big aha: <strong>Convolution reverses one of the lists!</strong> Here's why.</p>

<p>Let's call our treatment plan <img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. In our example, we used <code>[3 2 1]</code>.</p>

<p>The list of patients (inputs) is <img src="https://betterexplained.com/latex/67db49cba7a7b09a6950.png" alt="$g(x)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. However, we need to <em>reverse</em> this list as we slide it, so the earliest patient (Monday) enters the hospital first (first in, first out). This means we need to use <img src="https://betterexplained.com/latex/941c6d33136c5abf3e97.png" alt="$g(-x)$" width="48" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, the horizontal reflection of <img src="https://betterexplained.com/latex/67db49cba7a7b09a6950.png" alt="$g(x)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. <code>[1 2 3 4 5]</code> becomes <code>[5 4 3 2 1]</code>.</p>

<p>Now that we have the reversed list, pick a day to compute (<img src="https://betterexplained.com/latex/ab09c71153409a08a2f8.png" alt="$t = 0, 1, 2...$" width="93" height="16" style="vertical-align:-3.51px" class="bex-math"></img>). To slide our patient list by this much, we use: <img src="https://betterexplained.com/latex/44ae1d72b35c47571302.png" alt="$g(-x + t)$" width="76" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. That is, we reverse the list (<img src="https://betterexplained.com/latex/65120d102a8a3f5d1adf.png" alt="$-x$" width="25" height="13" style="vertical-align:-1.49px" class="bex-math"></img>) and jump to the correct day (<img src="https://betterexplained.com/latex/0e1f959a5ad0480273f7.png" alt="$+t$" width="21" height="13" style="vertical-align:-1.49px" class="bex-math"></img>).</p>

<p>We have our scenario:</p>

<ul>
<li><img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is the plan to use</li>
<li><img src="https://betterexplained.com/latex/44ae1d72b35c47571302.png" alt="$g(-x + t)$" width="76" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is the list of inputs (flipped and slid to the right day).</li>
</ul>

<p>To get the total usage on day <img src="https://betterexplained.com/latex/2de0691d41e87626a221.png" alt="$t$" width="7" height="12" style="vertical-align:-0.2px" class="bex-math"></img>, we multiply each patient with the plan, and sum the results (an integral). To account for any possible length, we go from -infinity to +infinity.</p>

<p>Now we can describe convolution formally using calculus:</p>

<p><img decoding="async" src="https://betterexplained.com/ColorizedMath/img/Convolution.png" alt="convolution" /></p>

<p>(Like <a href="https://betterexplained.com/articles/colorized-math-equations/">colorized math</a>? There's more.)</p>

<p>Phew! That's quite few symbols. Some notes:</p>

<ul>
<li>We use a dummy variable <img src="https://betterexplained.com/latex/9656236114bc9ec97310.png" alt="$\tau$" width="10" height="9" style="vertical-align:-0.23px" class="bex-math"></img> (tau) for the intermediate computation. Imagine <img src="https://betterexplained.com/latex/9656236114bc9ec97310.png" alt="$\tau$" width="10" height="9" style="vertical-align:-0.23px" class="bex-math"></img> as knocking on each room (<img src="https://betterexplained.com/latex/93452d264f283579cb30.png" alt="$\tau={0, 1, 2, 3...}$" width="112" height="16" style="vertical-align:-3.51px" class="bex-math"></img>), finding the dosage [<img src="https://betterexplained.com/latex/6c6f71d18fe4039ac62b.png" alt="$f(\tau)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img>], the number of patients [<img src="https://betterexplained.com/latex/af93a706afe6f590c1af.png" alt="$g(t - \tau)$" width="61" height="19" style="vertical-align:-4.53px" class="bex-math"></img>], multiplying them, and totaling things in the integral. Yowza. The so-called "dummy" variable <img src="https://betterexplained.com/latex/9656236114bc9ec97310.png" alt="$\tau$" width="10" height="9" style="vertical-align:-0.23px" class="bex-math"></img> is like <code>i</code> in a <code>for</code> loop: it's temporary, but does the work. (By analogy, <img src="https://betterexplained.com/latex/2de0691d41e87626a221.png" alt="$t$" width="7" height="12" style="vertical-align:-0.2px" class="bex-math"></img> is a global variable has a fixed value during the loop: it's the day we're calculating the usage for, such as <code>t = Day 5</code>).</li>
<li>In the official definition, you'll see <img src="https://betterexplained.com/latex/af93a706afe6f590c1af.png" alt="$g(t - \tau)$" width="61" height="19" style="vertical-align:-4.53px" class="bex-math"></img> instead of <img src="https://betterexplained.com/latex/9e513e47cb178651c036.png" alt="$g(- \tau+ t)$" width="75" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. The second version shows the flip (<img src="https://betterexplained.com/latex/a2f5121e6aefc7695261.png" alt="$-\tau$" width="24" height="13" style="vertical-align:-1.49px" class="bex-math"></img>) and slide (<img src="https://betterexplained.com/latex/0e1f959a5ad0480273f7.png" alt="$+t$" width="21" height="13" style="vertical-align:-1.49px" class="bex-math"></img>). Writing <img src="https://betterexplained.com/latex/af93a706afe6f590c1af.png" alt="$g(t - \tau)$" width="61" height="19" style="vertical-align:-4.53px" class="bex-math"></img> makes it seem like we're interested in the difference between the variables, which confused me.</li>
<li>The treatment plan (program to run) is called the <em>kernel</em>: you convolve a kernel with an input.</li>
</ul>

<p>Not too bad, right? The equation is a formal description of the analogy.</p>

<h2><span id="Part_3_Mathematical_Properties_of_Convolution">Part 3: Mathematical Properties of Convolution</span></h2>

<p>We can't discover a new math operation without taking it for a spin. Let's see how it behaves.</p>

<h3><span id="Convolution_is_commutative_f_g_g_f">Convolution is commutative: f * g = g * f</span></h3>

<p>In our computation, we flipped the patient list and kept the plan the same. Could we have flipped the plan instead?</p>

<p>You bet. Imagine the patients are immobile, and stay in their rooms: <code>[1 2 3 4 5]</code>. To deliver the medicine, we have 3 medical carts that go to each room and deliver the dose. Each day, they slide forward one position.</p>

<pre><code>  Carts -&gt;
  1 2 3

      1 2 3 4 5
      Patients
</code></pre>

<p>As before, though our plan is written <code>[3 2 1]</code> (3 units on the first day), we flip the order of the carts to<code>[1 2 3]</code>. That way, a patient gets 3 units on their first day, as we expect. Checking with Wolfram Alpha, the <a href="https://www.wolframalpha.com/input/?i=ListConvolve%5B%7B1%2C+2%2C+3%2C+4%2C+5%7D%2C+%7B3%2C+2%2C+1%7D%2C+%7B1%2C+-1%7D%2C+0%5D+">calculation</a> is the same.</p>

<pre><code>ListConvolve[{1, 2, 3, 4, 5}, {3, 2, 1}, {1, -1}, 0]
{3, 8, 14, 20, 26, 14, 5}
</code></pre>

<p>Cool! It looks like convolution is commutative:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/e16bac899ff20ecdadbab51b60c37d3b.png' width='97' height='17' alt='\displaystyle{f * g = g * f}' class='tex'></p>

<p>and we can decide to flip either <img src="https://betterexplained.com/latex/220f6de5269f95327e46.png" alt="$f$" width="10" height="17" style="vertical-align:-3.71px" class="bex-math"></img> or <img src="https://betterexplained.com/latex/e1527dc2bb80433e9e49.png" alt="$g$" width="9" height="12" style="vertical-align:-3.71px" class="bex-math"></img> when calculating the integral. Surprising, right?</p>

<h3><span id="The_integral_of_the_convolution">The integral of the convolution</span></h3>

<p>When all treatments are finished, what was the <em>total</em> medicine usage? This is the <strong>integral of the convolution</strong>. (A few minutes ago, that phrase would have you jumping out of a window.)</p>

<p>But it's a simple calculation. Our plan gives each patient <code>sum([3 2 1]) = 6</code> units of medicine. And we have <code>sum([1 2 3 4 5]) = 15</code> patients. The total usage is just <code>6 x 15 = 90</code> units.</p>

<p>Wow, that was easy: the usage for the <em>entire</em> convolution is just the product of the subtotals!</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/56e822f91d16dfbb8f13a0a8d52d0b46.png' width='168' height='41' alt='\displaystyle{\int (f * g) = \int f \cdot \int g }' class='tex'></p>

<p>I hope this clicks intuitively. Note that this trick works for convolution, but not integrals in general. For example:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/78f2d953a7a319e8866bcc10aa892a79.png' width='165' height='41' alt='\displaystyle{ \int (x \cdot x) \ne \int x \cdot \int x }' class='tex'></p>

<p>If we separate <img src="https://betterexplained.com/latex/9ebea033313303badad3.png" alt="$x \cdot x$" width="34" height="9" style="vertical-align:-0.2px" class="bex-math"></img> into two integrals we get:</p>

<ul>
<li><img src="https://betterexplained.com/latex/445126f31274e60f8c89.png" alt="$\int (x \cdot x) = \int x^2 = \frac{1}{3} x^3$" width="173" height="23" style="vertical-align:-6.53px" class="bex-math"></img></li>
<li><img src="https://betterexplained.com/latex/f0fa88310b5d3573ae0f.png" alt="$\int x \cdot \int x = \frac{1}{2}x^2 \cdot \frac{1}{2}x^2 = \frac{1}{4}x^4$" width="222" height="22" style="vertical-align:-6.25px" class="bex-math"></img></li>
</ul>

<p>and those aren't the same. (Calculus would be much easier if we could split integrals like this.) It's strange, but <img src="https://betterexplained.com/latex/4ae6f081a1614aa4c82f.png" alt="$\int (f * g)$" width="61" height="21" style="vertical-align:-5.53px" class="bex-math"></img> is probably easier to solve than <img src="https://betterexplained.com/latex/c7956480841fd47d6358.png" alt="$\int (fg)$" width="44" height="21" style="vertical-align:-5.53px" class="bex-math"></img>.</p>

<h3><span id="Impulse_Response">Impulse Response</span></h3>

<p>What happens if we sent a single patient through the hospital? The convolution would just be that day's plan.</p>

<pre><code>Plan    * Patients = Convolution
[3 2 1] * [1]      = [3 2 1]
</code></pre>

<p>In other words, convolving with <code>[1]</code> gives us the original plan.</p>

<p>In calculus terms, a spike of <code>[1]</code> (and 0 otherwise) is the <a href="https://en.wikipedia.org/wiki/Dirac_delta_function">Dirac Delta Function</a>. In terms of convolutions, this function acts like the number 1 and returns the original function:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/b29d7fb1b5d01e09a53c6559aa4b95a8.png' width='132' height='19' alt='\displaystyle{f(t) * \delta(t) = f(t)}' class='tex'></p>

<p>We can delay the delta function by T, which delays the resulting convolution function too. Imagine our single patient shows up a week late (<img src="https://betterexplained.com/latex/09d266a2482413c04031.png" alt="$\delta(t - T)$" width="64" height="19" style="vertical-align:-4.53px" class="bex-math"></img>), so our medicine usage gets delayed for a week too:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/1507a724e304125262fce84ee01f209c.png' width='202' height='19' alt='\displaystyle{f(t) * \delta(t - T) = f(t - T)}' class='tex'></p>

<h2><span id="Part_4_Convolution_Theorem_The_Fourier_Transform">Part 4: Convolution Theorem &amp; The Fourier Transform</span></h2>

<p>The <a href="https://betterexplained.com/articles/an-interactive-guide-to-the-fourier-transform/">Fourier Transform</a> (written with a fancy <img src="https://betterexplained.com/latex/fb3d22404d8f07b8e247.png" alt="$\mathscr{F}$" width="18" height="14" style="vertical-align:-0.65px" class="bex-math"></img>) converts a function <img src="https://betterexplained.com/latex/34867dbe6c2bfca4bf25.png" alt="$f(t)$" width="31" height="19" style="vertical-align:-4.53px" class="bex-math"></img> into a list of cyclical ingredients <img src="https://betterexplained.com/latex/1c5b76a5d6dc15af8a83.png" alt="$F(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img>:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/8c9ad40ce73b75644d58416c309d966d.png' width='101' height='25' alt='\displaystyle{f(t) \xrightarrow{\mathscr{F}} F(s)}' class='tex'></p>

<p>As an operator, this can be written <img src="https://betterexplained.com/latex/fcae50f315cb63515e43.png" alt="$\mathscr{F}\lbrace f \rbrace = F$" width="84" height="19" style="vertical-align:-4.53px" class="bex-math"></img>.</p>

<p>In our analogy, we convolved the plan and patient list with a fancy multiplication. Since the Fourier Transform gives us lists of ingredients, could we get the same result by mixing the <em>ingredient lists</em>?</p>

<p>Yep, we can: <strong>Fancy multiplication in the regular world is <em>regular</em> multiplication in the fancy world.</strong></p>

<p>In math terms, "Convolution in the time domain is multiplication in the frequency (Fourier) domain."</p>

<p>Mathematically, this is written:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/975d50cac9c5ad016dec8d02a36d609c.png' width='99' height='25' alt='\displaystyle{f * g \xrightarrow{\mathscr{F}} FG}' class='tex'></p>

<p>or</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/9de81fb69762e5905e57bfe66b54bdf8.png' width='126' height='19' alt='\displaystyle{\mathscr{F}\lbrace f * g \rbrace = FG}' class='tex'></p>

<p>where <img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img> and <img src="https://betterexplained.com/latex/67db49cba7a7b09a6950.png" alt="$g(x)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img> are functions to convolve, with transforms <img src="https://betterexplained.com/latex/1c5b76a5d6dc15af8a83.png" alt="$F(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img> and <img src="https://betterexplained.com/latex/bf0c3c97a503725c8c7a.png" alt="$G(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img>.</p>

<p>We can <a href="https://www-structmed.cimr.cam.ac.uk/Course/Convolution/convolution.html">prove this theorem</a> with advanced calculus, that uses theorems I don't quite understand, but let's think through the meaning.</p>

<p>Because <img src="https://betterexplained.com/latex/1c5b76a5d6dc15af8a83.png" alt="$F(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is the Fourier Transform of <img src="https://betterexplained.com/latex/34867dbe6c2bfca4bf25.png" alt="$f(t)$" width="31" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, we can ask for a specific frequency (<img src="https://betterexplained.com/latex/f81f1ef00f8f2705acee.png" alt="$s = 2\text{Hz}$" width="64" height="14" style="vertical-align:-1.49px" class="bex-math"></img>) and get the <em>combined interaction</em> of every data point with that frequency. Let's suppose:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/0884a3f9a6e39cfddf47bc84424a6d89.png' width='98' height='19' alt='\displaystyle{F(2) = 3 + i}' class='tex'></p>

<p>That means after every data point has been multiplied against the 2Hz cycle, the result is <img src="https://betterexplained.com/latex/7e9a92304539e47e0804.png" alt="$3 + i$" width="38" height="14" style="vertical-align:-1.49px" class="bex-math"></img>. But we could have kept each interaction separate:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/23d27254660014f82e5c40701fc59a5e.png' width='303' height='19' alt='\displaystyle{F(2) = 3 + i = c_0 + c_1 + c_2 + c_3 ... + c_t}' class='tex'></p>

<p>Where <img src="https://betterexplained.com/latex/7347763bda3d70cefa8d.png" alt="$c_t$" width="14" height="11" style="vertical-align:-2.86px" class="bex-math"></img> is the contribution to the 2Hz frequency from datapoint <img src="https://betterexplained.com/latex/2de0691d41e87626a221.png" alt="$t$" width="7" height="12" style="vertical-align:-0.2px" class="bex-math"></img>. Similarly, we can expand <img src="https://betterexplained.com/latex/bf0c3c97a503725c8c7a.png" alt="$G(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img> into a list of interactions with the 2Hz ingredient. Let's suppose <img src="https://betterexplained.com/latex/349dfb89eb186ffd5c22.png" alt="$G(2) = 7 - i$" width="99" height="19" style="vertical-align:-4.53px" class="bex-math"></img>:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/173bd83355b2b44e61b86c691e34d49c.png' width='311' height='19' alt='\displaystyle{G(2) = 7 - i = d_0 + d_1 + d_2 + d_3 ... + d_t}' class='tex'></p>

<p>The Convolution Theorem is really saying:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/e7a16cd19ecb142a8c2905935522697b.png' width='414' height='25' alt='\displaystyle{f * g \xrightarrow{\mathscr{F}} FG = (c_0 + c_1 + c_2 + ...)(d_0 + d_1 + d_2 + ...)}' class='tex'></p>

<p>Our convolution in the regular domain involves a lot of cross-multiplications. In the fancy frequency domain, we <em>still</em> have a bunch of interactions, but <img src="https://betterexplained.com/latex/1c5b76a5d6dc15af8a83.png" alt="$F(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img> and <img src="https://betterexplained.com/latex/bf0c3c97a503725c8c7a.png" alt="$G(s)$" width="37" height="19" style="vertical-align:-4.53px" class="bex-math"></img> have consolidated them. We can just multiply <img src="https://betterexplained.com/latex/3b4ca8388308b95c8475.png" alt="$F(2)G(2) = (3 + i)(7-i)$" width="202" height="19" style="vertical-align:-4.53px" class="bex-math"></img> to find the 2Hz ingredient in the convolved result.</p>

<p>By analogy, suppose you want to calculate:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/833c92dbdffc2f4600786c0d34386a51.png' width='260' height='19' alt='\displaystyle{(1 + 2 + 3 + 4)(5 + 6 + 7 + 8) = ?}' class='tex'></p>

<p>It's a lot of work to cross-multiply every term: <img src="https://betterexplained.com/latex/62154df5a1faefd85792.png" alt="$(1 \cdot 5) + (1\cdot 6) + (1\cdot 7) + ...$" width="216" height="19" style="vertical-align:-4.53px" class="bex-math"></img></p>

<p>It's better to consolidate the groups into <img src="https://betterexplained.com/latex/4784672072af20446340.png" alt="$(1 + 2 + 3 + 4) = 10$" width="159" height="19" style="vertical-align:-4.53px" class="bex-math"></img> and <img src="https://betterexplained.com/latex/7ffe7bfa68251a457d57.png" alt="$(5 + 6 + 7 + 8) = 26$" width="159" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, and <em>then</em> multiply to get <img src="https://betterexplained.com/latex/4781c3d13ce53560c8c8.png" alt="$10 \cdot 26 = 260$" width="101" height="14" style="vertical-align:-1.49px" class="bex-math"></img>.</p>

<p>This nuance caused me a lot of confusion. It seems like <img src="https://betterexplained.com/latex/9eb0fc49d439ae51f1d0.png" alt="$FG$" width="28" height="14" style="vertical-align:-0.4px" class="bex-math"></img> is a single multiplication, while <img src="https://betterexplained.com/latex/39d7d2e7d2ebe3ce2ebc.png" alt="$f * g$" width="36" height="17" style="vertical-align:-3.71px" class="bex-math"></img> involves a bunch of intermediate terms. I forgot that <img src="https://betterexplained.com/latex/4f57ad0bbe6f740c74fa.png" alt="$F$" width="14" height="13" style="vertical-align:0px" class="bex-math"></img> already did the work of merging a bunch of entries into a single one.</p>

<p>Now, we aren't <em>quite</em> done.</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/b16131bbdcf74439d0327764cc3d3874.png' width='154' height='25' alt='\displaystyle{f * g \xrightarrow{\mathscr{F}} FG \xrightarrow{\mathscr{F^{-1}}} \ ?}' class='tex'></p>

<p>We can convert <img src="https://betterexplained.com/latex/39d7d2e7d2ebe3ce2ebc.png" alt="$f * g$" width="36" height="17" style="vertical-align:-3.71px" class="bex-math"></img> in the time domain into <img src="https://betterexplained.com/latex/9eb0fc49d439ae51f1d0.png" alt="$FG$" width="28" height="14" style="vertical-align:-0.4px" class="bex-math"></img> in the frequency domain, but we probably need it back in the time domain for a usable result:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/4838206025511ca3236c054e272048a5.png' width='144' height='21' alt='\displaystyle{f * g = \mathscr{F}^{-1} \lbrace FG \rbrace}' class='tex'></p>

<p>You have a riddle in English (<img src="https://betterexplained.com/latex/39d7d2e7d2ebe3ce2ebc.png" alt="$f * g$" width="36" height="17" style="vertical-align:-3.71px" class="bex-math"></img>), you translate it to French (<img src="https://betterexplained.com/latex/9eb0fc49d439ae51f1d0.png" alt="$FG$" width="28" height="14" style="vertical-align:-0.4px" class="bex-math"></img>), get your smart French friend to work out that calculation, then convert it back to English (<img src="https://betterexplained.com/latex/d6d2ad667bd988d616aa.png" alt="$\mathscr{F}^{-1}$" width="37" height="16" style="vertical-align:-0.65px" class="bex-math"></img>).</p>

<h3><span id="And_in_reverse">And in reverse...</span></h3>

<p>The convolution theorem works this way too:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/c5a0816925f9e3aa674885d90abe00b2.png' width='99' height='25' alt='\displaystyle{fg \xrightarrow{\mathscr{F}} F * G}' class='tex'></p>

<p><strong>Regular multiplication in the regular world is fancy multiplication in the fancy world.</strong></p>

<p>Cool, eh? Instead of multiplying two functions like some cave dweller, put on your monocle, convolve the Fourier Transforms, and convert to the time domain:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/6869aaa36d056ce64f590d5d92220d83.png' width='144' height='21' alt='\displaystyle{fg = \mathscr{F}^{-1} \lbrace F*G \rbrace}' class='tex'></p>

<p>I'm not saying this is fun, just that it's possible. If your French friend has a gnarly calculation they're struggling with, it might look like arithmetic to you.</p>

<h3><span id="Mini_proof">Mini proof</span></h3>

<p>Remember how we said the integral of a convolution was a multiplication of the individual integrals?</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/56e822f91d16dfbb8f13a0a8d52d0b46.png' width='168' height='41' alt='\displaystyle{\int (f * g) = \int f \cdot \int g }' class='tex'></p>

<p>Well, the Fourier Transform is just a very specific integral, right?</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/502869363dd0f3b4dc8a76053bcbb534.png' width='64' height='41' alt='\displaystyle{\int \rightarrow \mathscr{F}}' class='tex'></p>

<p>So (handwaving), it seems we could swap the general-purpose integral <img src="https://betterexplained.com/latex/a268fc1b5df463b5edf0.png" alt="$\int$" width="12" height="21" style="vertical-align:-5.53px" class="bex-math"></img> for <img src="https://betterexplained.com/latex/fb3d22404d8f07b8e247.png" alt="$\mathscr{F}$" width="18" height="14" style="vertical-align:-0.65px" class="bex-math"></img> and get</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/56b33fef4c78829009660e538336af81.png' width='191' height='19' alt='\displaystyle{\mathscr{F} (f * g) = \mathscr{F} (f) \cdot \mathscr{F} (g) }' class='tex'></p>

<p>which is the convolution theorem. I need a deeper intuition for the <a href="https://math.stackexchange.com/questions/1077990/integral-of-the-convolution-of-two-functions-int-infty-infty-fgxd">proof</a>, but this helps things click.</p>

<h2><span id="Part_5_Applications">Part 5: Applications</span></h2>

<p>The trick with convolution is finding a useful "program" (kernel) to apply to your input. Here's a few examples.</p>

<h3><span id="Moving_averages">Moving averages</span></h3>

<p>Let's say you want a moving average between neighboring items in a list. That is half of each element, added together:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/4e9c801fbbc424a674e4e466d939a091.png' width='113' height='38' alt='\displaystyle{\frac{a + b}{2} = \frac{a}{2} + \frac{b}{2}}' class='tex'></p>

<p>This is a "multiplication program" of <code>[0.5 0.5]</code> convolved with our list:</p>

<pre><code>ListConvolve[{1, 4, 9, 16, 25}, {0.5, 0.5}, {1, -1}, 0] 
{0.5, 2.5, 6.5, 12.5, 20.5, 12.5}
</code></pre>

<p>We can perform a moving average with a single operation. Neat!</p>

<p>A 3-element moving average would be <code>[.33 .33 .33]</code>, a weighted average could be <code>[.5 .25 .25]</code>.</p>

<h3><span id="Derivatives">Derivatives</span></h3>

<p>The derivative finds the difference between neighboring values. Here's the plan: <code>[1 -1]</code></p>

<pre><code>ListConvolve[{1, 2, 3, 4, 5}, {1, -1}, {1, -1}, 0] 
{1, 1, 1, 1, 1, -5}                         // -5 since we ran out of entries

ListConvolve[{1, 4, 9, 16, 25}, {1, -1}, {1, -1}, 0] 
{1, 3, 5, 7, 9, -25}                        // discrete derivative is 2x + 1
</code></pre>

<p>With a simple kernel, we can find a useful math property on a discrete list. And to get a second derivative, just apply the derivative convolution twice:</p>

<p><code>F * [1 -1] * [1 -1] </code></p>

<p>As a shortcut, we can precompute the final convolutions (<code>[1 -1] * [1 -1]</code> ) and get:</p>

<pre><code>ListConvolve[{1, -1}, {1,-1}, {1, -1}, 0] 
{1, -2, 1}
</code></pre>

<p>Now we have a <em>single</em> kernel <code>[1, -2, 1]</code> that gets the second derivative of a list:</p>

<pre><code>ListConvolve[{1, 4, 9, 16, 25}, {1, -2, 1}, {1, -1}, 0] 
{1, 2, 2, 2, 2, -34, 25}
</code></pre>

<p>Excluding the boundary items, we get the expected second derivative:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/93d03b535aa28ab5c13b0e3d7ef4dec2.png' width='147' height='23' alt='\displaystyle{x^2 \xrightarrow{d/dx} 2x \xrightarrow{d/dx} 2}' class='tex'></p>

<h3><span id="Blurring_unblurring_images">Blurring / unblurring images</span></h3>

<p>An image blur is essentially a convolution of your image with some "blurring kernel":</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/a133d2923fb791fe5e9bd5474083ada4.png' width='178' height='17' alt='\displaystyle{\text{blurred} = \text{image} * \text{blur}}' class='tex'></p>

<p>The blur of our 2D image requires a <a href="https://en.wikipedia.org/wiki/Kernel_(image_processing)">2D average</a>:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/convolution/convolution-blur-wikipedia.png" alt="Kernel__image_processing__-_Wikipedia" class="webpexpress-processed"></p>

<p>Can we undo the blur? Yep! With our friend the Convolution Theorem, we can do:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/a133d2923fb791fe5e9bd5474083ada4.png' width='178' height='17' alt='\displaystyle{\text{blurred} = \text{image} * \text{blur}}' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/1e2717feb61734bf83d7818c909d24a5.png' width='250' height='19' alt='\displaystyle{\mathscr{F} \lbrace \text{blurred} \rbrace = \mathscr{F} \lbrace \text{image} * \text{blur} \rbrace}' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/45f6a8360051ab4ec3ba8e5af159b539.png' width='270' height='19' alt='\displaystyle{\mathscr{F} \lbrace \text{blurred} \rbrace = \mathscr{F} \lbrace \text{image} \rbrace \mathscr{F} \lbrace \text{blur} \rbrace}' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/417952a1063203f96d0b0428209b56fd.png' width='203' height='44' alt='\displaystyle{\frac{ \mathscr{F} \lbrace \text{blurred} \rbrace }{\mathscr{F} \lbrace \text{blur} \rbrace} = \mathscr{F} \lbrace \text{image} \rbrace }' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/ac0a303f5414c3148279e482d396cae1.png' width='224' height='44' alt='\displaystyle{\mathscr{F}^{-1} \lbrace \frac{ \mathscr{F} \lbrace \text{blurred} \rbrace }{\mathscr{F} \lbrace \text{blur} \rbrace} \rbrace = \text{image}}' class='tex'></p>

<p>Whoa! We can recover the original image by dividing out the blur. Convolution is a simple multiplication in the frequency domain, and <em>deconvolution</em> is a simple division in the frequency domain.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/convolution/image-blur.png" alt="image-blur" class="webpexpress-processed"></p>

<p>A short while back, the concept of "deblurring by dividing Fourier Transforms" was gibberish to me. While it can be daunting mathematically, it's getting simpler conceptually.</p>

<p>More reading:</p>

<ul>
<li>Wikipedia on <a href="https://en.wikipedia.org/wiki/Deconvolution">deconvolution</a>,  <a href="https://en.wikipedia.org/wiki/Deblurring">deblurring</a>.</li>
<li><a href="http://www.robots.ox.ac.uk/~az/lectures/ia/lect3.pdf">Image Restoration Lecture</a>, <a href="http://user.engineering.uiowa.edu/~dip/LECTURE/PreProcessing4.html#invconvolution">another one</a>, and <a href="http://fourier.eng.hmc.edu/e161/demos/e161_demo4.html">example for above</a></li>
<li><a href="https://www.mathworks.com/help/images/deblurring-images-using-the-blind-deconvolution-algorithm.html">Blind deconvolution</a> is when you don't know the blur kernel (make a guess)</li>
</ul>

<h3><span id="Algorithm_Trick_Multiplication">Algorithm Trick: Multiplication</span></h3>

<p>What is a number? A list of digits:</p>

<pre><code>1234 = 1000 + 200 + 30 + 4 = [1000 200 30 4]
5678 = 5000 + 600 + 70 + 8 = [5000 600 70 8]
</code></pre>

<p>And what is regular, grade-school multiplication? A digit-by-digit convolution! We sweep one list of digits by the other, multiplying and adding as we go:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/convolution/gradeschoolalgorithm.png" alt="Intuitive Guide to Convolution" class="webpexpress-processed"></p>

<p><a href="https://pythonandr.com/2015/10/13/karatsuba-multiplication-algorithm-python-code/">Source</a></p>

<p>We can perform the calculation by convolving the lists of digits (<a href="https://www.wolframalpha.com/input/?i=ListConvolve%5B%7B1000%2C+200%2C+30%2C+4%7D%2C+%7B8%2C+70%2C+600%2C+5000%7D%2C+%7B1%2C+-1%7D%2C+0%5D">wolfram alpha</a>):</p>

<pre><code>ListConvolve[{1000, 200, 30, 4}, {8, 70, 600, 5000}, {1, -1}, 0]
{8000, 71600, 614240, 5122132, 1018280, 152400, 20000}

sum {8000, 71600, 614240, 5122132, 1018280, 152400, 20000}
7006652
</code></pre>

<p>Note that we pre-flip one of the lists (it gets swapped in the convolution later), and the intermediate calculations are a bit different. But, combining the subtotals gives the expected result.</p>

<h3><span id="Faster_Convolutions">Faster Convolutions</span></h3>

<p>Why convolve instead of doing a regular digit-by-digit multiplication? Well, the convolution theorem lets us substitute convolution with Fourier Transforms:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/4838206025511ca3236c054e272048a5.png' width='144' height='21' alt='\displaystyle{f * g = \mathscr{F}^{-1} \lbrace FG \rbrace}' class='tex'></p>

<p>The convolution (<img src="https://betterexplained.com/latex/39d7d2e7d2ebe3ce2ebc.png" alt="$f * g$" width="36" height="17" style="vertical-align:-3.71px" class="bex-math"></img>) has complexity <img src="https://betterexplained.com/latex/103277ff874e4c17b58d.png" alt="$O(n^2)$" width="47" height="20" style="vertical-align:-4.53px" class="bex-math"></img>. We have <img src="https://betterexplained.com/latex/e5e0f1078316e19bf059.png" alt="$n$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img> positions to process, with <img src="https://betterexplained.com/latex/e5e0f1078316e19bf059.png" alt="$n$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img> intermediate multiplications at each position.</p>

<p>The right side involves:</p>

<ul>
<li>Two Fourier Transforms, which are normally <img src="https://betterexplained.com/latex/103277ff874e4c17b58d.png" alt="$O(n^2)$" width="47" height="20" style="vertical-align:-4.53px" class="bex-math"></img>. However, the Fast Fourier Transform (a <a href="https://www.youtube.com/watch?v=iTMn0Kt18tg">divide-and-conquer approach</a>) makes them <img src="https://betterexplained.com/latex/a339edf5ab740f9613dc.png" alt="$O(n\log(n))$" width="90" height="19" style="vertical-align:-4.53px" class="bex-math"></img>.</li>
<li>Pointwise multiplication of the final result of the transforms  (<img src="https://betterexplained.com/latex/ca98954286e33e21a66d.png" alt="$\sum a_n \cdot b_n$" width="71" height="19" style="vertical-align:-4.53px" class="bex-math"></img>), which is <img src="https://betterexplained.com/latex/f82fa4d11920dd3253f2.png" alt="$O(n)$" width="39" height="19" style="vertical-align:-4.53px" class="bex-math"></img></li>
<li>An inverse transform, which is <img src="https://betterexplained.com/latex/a339edf5ab740f9613dc.png" alt="$O(n\log(n))$" width="90" height="19" style="vertical-align:-4.53px" class="bex-math"></img></li>
</ul>

<p>And the total complexity is: <img src="https://betterexplained.com/latex/caf4354a88182d1d3573.png" alt="$O(n\log(n)) + O(n\log(n)) + O(n) + O(n\log(n)) = O(n\log(n))$" width="489" height="19" style="vertical-align:-4.53px" class="bex-math"></img></p>

<p>Regular multiplication in the fancy domain is <em>faster</em> than a fancy multiplication in the regular domain. Our French friend is no slouch. (<a href="http://www.ams.org/publicoutreach/feature-column/fcarc-multiplication">More</a>)</p>

<h3><span id="Convolutional_Neural_Nets_CNN">Convolutional Neural Nets (CNN)</span></h3>

<p>Machine learning is about discovering the math functions that transform input data into a desired result (a prediction, classification, etc.).</p>

<p>Starting with an input signal, we could convolve it with a bunch of kernels:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/ec62a4fc08cd4f262a0f7fa9221eef1e.png' width='229' height='16' alt='\displaystyle{\text{input} * k_1 * k_2 * k_3 ... = \text{result}}' class='tex'></p>

<p>Given that convolution can do complex math (moving averages, blurs, derivatives...), it seems <em>some</em> combination of kernels should turn our input into something useful, right?</p>

<p>Convolutional Neural Nets (CNNs) process an input with layers of kernels, optimizing their weights (plans) to reach a goal. Imagine tweaking the treatment plan to keep medicine usage below some threshold.</p>

<p>CNNs are often used with image classifiers, but 1D data sets work just fine.</p>

<ul>
<li>Nice writeup: <a href="https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/">https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/</a></li>
<li>Digit classifier demo: <a href="https://cs.stanford.edu/people/karpathy/convnetjs/demo/mnist.html">https://cs.stanford.edu/people/karpathy/convnetjs/demo/mnist.html</a></li>
</ul>

<h3><span id="LTI_System_Behavior">LTI System Behavior</span></h3>

<p>A linear, time-invariant system means:</p>

<ul>
<li>Linear: Scaling and combining inputs scales and combines outputs by the same amount</li>
<li>Time invariant: Outputs depend on relative time, not absolute time. You get 3 units on <em>your</em> first day, and it doesn't matter if it's Wednesday or Thursday.</li>
</ul>

<p>A fancy phrase is "A LTI system is characterized by its impulse response". Translation: If we send a <em>single</em> patient through the hospital <code>[1]</code>, we'll discover the treatment plan. Then we can predict the usage for <em>any</em> sequence of patients by convolving it with the plan.</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/1ed77a8b2cc34caedc58aa7b05416c35.png' width='352' height='17' alt='\displaystyle{\text{system response} = \text{impulse response} * \text{inputs}}' class='tex'></p>

<p>If the system isn't LTI, we can't extrapolate based on a <em>single</em> person's experience. Scaling the inputs may not scale the outputs, and the actual calendar day, not relative day, may impact the result (imagine fewer rooms available on weekends).</p>

<h3><span id="Engineering_Analogies">Engineering Analogies</span></h3>

<p>From <a href="https://www.quora.com/What-is-an-intuitive-explanation-for-convolution/answer/David-Greenspan">David Greenspan</a>: "Suppose you have a special laser pointer that makes a star shape on the wall. You tape together a bunch of these laser pointers in the shape of a square. The pattern on the wall now is the convolution of a star with a square."</p>

<p>Regular multiplication gives you a single scaled copy of an input. Convolution creates multiple overlapping copies that follow a pattern you've specified.</p>

<p>Real-world systems have squishy, not instantaneous, behavior: they ramp up, peak, and drop down. The convolution lets us model systems that echo, reverb and overlap.</p>

<p>Now it's time for the famous sliding window example. Think of a pulse of inputs (red) sliding through a system (blue), and having a combined effect (yellow): the convolution.</p>

<p><img decoding="async" src="https://upload.wikimedia.org/wikipedia/commons/b/b9/Convolution_of_spiky_function_with_box2.gif" alt="Intuitive Guide to Convolution" /></p>

<p>(<a href="https://commons.wikimedia.org/wiki/File:Convolution_of_spiky_function_with_box2.gif">Source</a>)</p>

<h2><span id="Summary">Summary</span></h2>

<p>Convolution has an advanced technical definition, but the basics can be understood with the right analogy.</p>

<p>Quick rant: I study math for fun, yet it took years to find a satisfying intuition for:</p>

<ul>
<li>Why is one function reversed?</li>
<li>Why is convolution commutative?</li>
<li>Why does the integral of the convolution = product of integrals?</li>
<li>Why are the Fourier Transforms multiplied point-by-point, and not overlapped?</li>
</ul>

<p>Why'd it take so long? Imagine learning multiplication with <img src="https://betterexplained.com/latex/159fcc70dfe383db4f8e.png" alt="$f \times g = z$" width="74" height="17" style="vertical-align:-3.71px" class="bex-math"></img> instead of  <img src="https://betterexplained.com/latex/06251d1d8858978ebc28.png" alt="$3 \times 5 = 15$" width="83" height="14" style="vertical-align:-1.49px" class="bex-math"></img>. Without an example I can explore <em>in my head</em>, I could only memorize results, not intuit them. Hopefully this analogy can save you years of struggle.</p>

<p>Happy math.</p>

]]></content:encoded>
					
					<wfw:commentRss>https://betterexplained.com/articles/intuitive-convolution/feed/</wfw:commentRss>
			<slash:comments>33</slash:comments>
		

			</item>
	<item>
		<title>Integral of Sin(x): Geometric Intuition</title>
		<link>https://betterexplained.com/articles/integral-sinx/</link>
					<comments>https://betterexplained.com/articles/integral-sinx/#disqus_thread</comments>
		
		<dc:creator><![CDATA[kalid]]></dc:creator>
		<pubDate>Wed, 20 Nov 2019 00:01:09 +0000</pubDate>
				<category><![CDATA[Calculus]]></category>
		<guid isPermaLink="false">https://betterexplained.com/?p=12244</guid>

					<description><![CDATA[You're minding your own business when some punk asks what the integral of means. Your options: Pretend to be asleep (except not in the engineering library again) Canned response: "As with any function, the integral of sine is the area under its curve." Geometric intuition: "The integral of sine is the horizontal distance along a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>You're minding your own business when some punk asks what the integral of <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> means. Your options:</p>

<ul>
<li>Pretend to be asleep (except not in the engineering library again)</li>
<li>Canned response: "As with any function, the integral of sine is the area under its curve."</li>
<li>Geometric intuition: "The integral of sine is the <strong>horizontal distance</strong> along a circular path."</li>
</ul>

<p>Option 1 is tempting, but let's take a look at the others.</p>

<iframe width="640" height="360" src="https://www.youtube.com/embed/6pE_WQjCSGA" frameborder="0" allowfullscreen=""></iframe>

<h2>Why "Area Under the Curve" is Unsatisfying</h2>

<p>Describing an integral as "area under the curve" is like describing a book as a list of words. Technically correct, but misses the message and I suspect you haven't done the assigned reading.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/11/Riemann_sums.png" alt="Integral of Sin(x): Geometric Intuition" class="webpexpress-processed"></p>

<p>Unless you're trapped in LegoLand, integrals mean something besides rectangles.</p>

<h2>Decoding the Integral</h2>

<p>My calculus conundrum was not having an intuition for all the mechanics.</p>

<p>When we see:</p>

<p><img src="https://betterexplained.com/latex/766bfcdf4f4c92722fb7.png" alt="$\int \sin(x) dx$" width="81" height="21" style="vertical-align:-5.53px" class="bex-math"></img></p>

<p>We can call on a few insights:</p>

<ul>
<li><p>The integral is just <a href="https://betterexplained.com/articles/a-calculus-analogy-integrals-as-multiplication/">fancy multiplication</a>. Multiplication accumulates numbers that don't change (3 + 3 + 3 + 3). Integrals add up numbers that <em>might</em> change, based on a pattern (1 + 2 + 3 + 4). But if we squint our eyes and pretend items are identical we have a multiplication.</p></li>
<li><p><img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> just a <a href="https://betterexplained.com/articles/intuitive-trigonometry/">percentage</a>. Yes, it's also fancy curve with nice properties. But at any point (like 45 degrees), it's a single <em>percentage</em> from -100% to +100%. Just regular numbers.</p></li>
<li><p><img src="https://betterexplained.com/latex/3180123ed37d6de4b4e6.png" alt="$dx$" width="20" height="13" style="vertical-align:-0.2px" class="bex-math"></img> is a tiny, <a href="https://betterexplained.com/articles/why-do-we-need-limits-and-infinitesimals/">infinitesimal part</a> of the path we're taking. 0 to <img src="https://betterexplained.com/latex/d29b4638e781db60be51.png" alt="$x$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img> is the full path, so <img src="https://betterexplained.com/latex/3180123ed37d6de4b4e6.png" alt="$dx$" width="20" height="13" style="vertical-align:-0.2px" class="bex-math"></img> is (intuitively) a nanometer wide.</p></li>
</ul>

<p>Ok. With those 3 intuitions, our rough (rough!) conversion to Plain English is:</p>

<blockquote>

<p>The integral of sin(x) multiplies our intended path length (from 0 to x) by a percentage</p>
</blockquote>

<p>We <em>intend</em> to travel a simple path from 0 to x, but we end up with a smaller percentage instead. (Why? Because <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is usually less than 100%). So we'd expect something like 0.75x.</p>

<p>In fact, if <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> did have a fixed value of 0.75, our integral would be:</p>

<p><img src="https://betterexplained.com/latex/82213c1fe69d65a4dfeb.png" alt="$\int \text{fixedsin}(x) \ dx = \int 0.75 \ dx = 0.75 \int dx = 0.75x$" width="378" height="21" style="vertical-align:-5.53px" class="bex-math"></img></p>

<p>But the real <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, that rascal, changes as we go. Let's see what fraction of our path we really get.</p>

<h2>Visualize The Change in Sin(x)</h2>

<p>Now let's visualize <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> and its changes:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/11/sine-dx-change.png" alt="Integral of Sin(x): Geometric Intuition" class="webpexpress-processed"></p>

<p>Here's the decoder key:</p>

<ul>
<li><p><img src="https://betterexplained.com/latex/d29b4638e781db60be51.png" alt="$x$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img> is our current angle in <a href="https://betterexplained.com/articles/intuitive-guide-to-angles-degrees-and-radians/">radians</a>. On the unit circle (radius=1), the angle is the distance along the circumference.</p></li>
<li><p><img src="https://betterexplained.com/latex/3180123ed37d6de4b4e6.png" alt="$dx$" width="20" height="13" style="vertical-align:-0.2px" class="bex-math"></img> is a tiny change in our angle, which becomes the same change along the circumference (moving 0.01 units in our angle moves 0.01 along the circumference).</p></li>
<li><p>At our tiny scale, a circle is a <a href="https://betterexplained.com/articles/prehistoric-calculus-discovering-pi/">a polygon with many sides</a>, so we're moving along a <em>line segment</em> of length <img src="https://betterexplained.com/latex/3180123ed37d6de4b4e6.png" alt="$dx$" width="20" height="13" style="vertical-align:-0.2px" class="bex-math"></img>. This puts us at a new position.</p></li>
</ul>

<p>With me? With trigonometry, we can find the exact change in height/width as we slide along the circle by <img src="https://betterexplained.com/latex/3180123ed37d6de4b4e6.png" alt="$dx$" width="20" height="13" style="vertical-align:-0.2px" class="bex-math"></img>.</p>

<p>By similar triangles, our change just just our original triangle, rotated and scaled.</p>

<ul>
<li>Original triangle (hypotenuse = 1): height = <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, width = <img src="https://betterexplained.com/latex/279dacec896950f1f76b.png" alt="$\cos(x)$" width="49" height="19" style="vertical-align:-4.53px" class="bex-math"></img></li>
<li>Change triangle (hypotenuse = dx): height = <img src="https://betterexplained.com/latex/8052c6ebbff8077807ef.png" alt="$\sin(x) dx$" width="67" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, width = <img src="https://betterexplained.com/latex/18a3329fa78b4eb74308.png" alt="$\cos(x)  dx$" width="69" height="19" style="vertical-align:-4.53px" class="bex-math"></img></li>
</ul>

<p>Now, remember that sine and cosine are functions that return percentages. (A number like 0.75 doesn't have its orientation. It shows up and makes things 75% of their size in whatever direction they're facing.)</p>

<p>So, given how we've drawn our Triangle of Change, <img src="https://betterexplained.com/latex/8052c6ebbff8077807ef.png" alt="$\sin(x) dx$" width="67" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is our horizontal change. Our plain-English intuition is:</p>

<blockquote>

<p>The integral of sin(x) adds up the horizontal change along our path</p>
</blockquote>

<h2>Visualize The Integral Intuition</h2>

<p>Ok. Let's graph this bad boy to see what's happening. With our "<img src="https://betterexplained.com/latex/8052c6ebbff8077807ef.png" alt="$\sin(x) dx$" width="67" height="19" style="vertical-align:-4.53px" class="bex-math"></img> = tiny horizontal change" insight we have:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/11/sine-integral-horizontal.png" alt="Integral of Sin(x): Geometric Intuition" class="webpexpress-processed"></p>

<p>As we circle around, we have a bunch of <img src="https://betterexplained.com/latex/3180123ed37d6de4b4e6.png" alt="$dx$" width="20" height="13" style="vertical-align:-0.2px" class="bex-math"></img> line segments (in red). When sine is small (around x=0) we barely get any horizontal motion. As sine gets larger (top of circle), we are moving up to 100% horizontally.</p>

<p>Ultimately, the various <img src="https://betterexplained.com/latex/8052c6ebbff8077807ef.png" alt="$\sin(x) dx$" width="67" height="19" style="vertical-align:-4.53px" class="bex-math"></img> segments move us horizontally from one side of the circle to the other.</p>

<p>A more technical description:</p>

<p><img src="https://betterexplained.com/latex/8c8f26da1b8abd2a66f7.png" alt="$\int_0^x \sin(x) dx = \text{horizontal distance traveled on arc from 0 to x}$" width="474" height="22" style="vertical-align:-6.45px" class="bex-math"></img></p>

<p>Aha! That's the meaning. Let's eyeball it. When moving from <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img> to <img src="https://betterexplained.com/latex/ac6169e88d2d649830b3.png" alt="$x=\pi$" width="45" height="13" style="vertical-align:-1.49px" class="bex-math"></img> we move exactly 2 units horizontally. It makes complete sense in the diagram.</p>

<h2>The Official Calculation</h2>

<p>Using the Official Calculus Fact that <img src="https://betterexplained.com/latex/ab1c8b9f53ef989fb78a.png" alt="$\int \sin(x) dx = -\cos(x)$" width="171" height="21" style="vertical-align:-5.53px" class="bex-math"></img> we would calculate:</p>

<p><img src="https://betterexplained.com/latex/85d58a77e115690d9b8e.png" alt="$\int_0^\pi \sin(x) dx = -\cos(x) \Big|_0^\pi = -\cos(\pi) - -\cos(0) = -(-1) -(-1) = 1 + 1 = 2$" width="604" height="31" style="vertical-align:-11.09px" class="bex-math"></img></p>

<p>Yowza. See how awkward it is, those double negations? Why was the visual intuition so much simpler?</p>

<p>Our path along the circle (<img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img> to <img src="https://betterexplained.com/latex/ac6169e88d2d649830b3.png" alt="$x=\pi$" width="45" height="13" style="vertical-align:-1.49px" class="bex-math"></img>) moves from right-to-left. But the x-axis goes positive from left-to-right. When convert distance along our path into Standard Area&#x2122;, we have to flip our axes:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/11/sine-unroll-changes.png" alt="Integral of Sin(x): Geometric Intuition" class="webpexpress-processed"></p>

<p>Our excitement to put things in the official format stamped out the intuition of what was happening.</p>

<h2>Fundamental Theorem of Calculus</h2>

<p>We don't really talk about the <a href="https://betterexplained.com/calculus/lesson-11/">Fundamental Theorem of Calculus</a> anymore. (Is it something I did?)</p>

<p>Instead of adding up all the tiny segments, just do: end point - start point.</p>

<p>The intuition was staring us in the face: <img src="https://betterexplained.com/latex/279dacec896950f1f76b.png" alt="$\cos(x)$" width="49" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is the anti-derivative, and tracks the horizontal position, so we're just taking a difference between horizontal positions! (With awkward negatives to swap the axes.)</p>

<p><em>That's</em> the power of the Fundamental Theorem of Calculus. Skip the intermediate steps and just subtract endpoints.</p>

<h2>Onward and Upward</h2>

<p>Why did I write this? Because I couldn't <a href="https://betterexplained.com/articles/gutcheck/">instantly figure out</a>:</p>

<p><img src="https://betterexplained.com/latex/a79450ffc412dd138052.png" alt="$\int_0^\pi \sin(x) dx = 2$" width="124" height="22" style="vertical-align:-6.45px" class="bex-math"></img></p>

<p>This isn't an exotic function with strange parameters. It's like asking someone to figure out <img src="https://betterexplained.com/latex/a853ebb3f76eb6f4f9de.png" alt="$2^3$" width="17" height="16" style="vertical-align:0px" class="bex-math"></img> without a calculator. If you claim to understand exponents, it should be possible, right?</p>

<p>Now, we can't always visualize things. But for the <em>most common</em> functions we owe ourselves a visual intuition. I certainly can't eyeball the 2 units of area from 0 to <img src="https://betterexplained.com/latex/6cbee51e8365518a121a.png" alt="$\pi$" width="11" height="8" style="vertical-align:-0.2px" class="bex-math"></img> under a sine curve.</p>

<p>Happy math.</p>

<h2>Appendix: Average Efficiency</h2>

<p>As a fun fact, the "average" efficiency of motion around the top of a circle (0 to <img src="https://betterexplained.com/latex/6cbee51e8365518a121a.png" alt="$\pi$" width="11" height="8" style="vertical-align:-0.2px" class="bex-math"></img>) is: <img src="https://betterexplained.com/latex/113ec743aa652fa83ea7.png" alt="$\frac{2}{\pi} = .6366$" width="81" height="23" style="vertical-align:-6.38px" class="bex-math"></img></p>

<p>So on average, 63.66% of your path's length is converted to horizontal motion.</p>

<h2>Appendix: Height controls width?</h2>

<p>It seems weird that height controls the width, and vice-versa, right?</p>

<p>If height controlled height, we'd have runaway <a href="https://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">exponential growth</a>. But a circle needs to regulate itself.</p>

<p><img src="https://betterexplained.com/latex/e7aded5a42342d4b3f23.png" alt="$e^x$" width="18" height="13" style="vertical-align:-0.2px" class="bex-math"></img> is the kid who eats candy, grows bigger, and can therefore eat more candy.</p>

<p><img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is the kid who eats candy, gets sick, waits for an appetite, and eats more candy.</p>

<h2>Appendix: Area isn't literal</h2>

<p>The "area" in our integral isn't literal area, it's a percentage of our length. We visualized the multiplication as a 2d rectangle in our generic integral, but it can be confusing. If you earn money and are taxed, do you visualize 2d area (income * (1 - tax))? Or just a quantity helplessly shrinking?</p>

<p>Area primarily indicates a multiplication happened. Don't let team Integrals Are Literal Area win every battle!</p>

]]></content:encoded>
					
					<wfw:commentRss>https://betterexplained.com/articles/integral-sinx/feed/</wfw:commentRss>
			<slash:comments>21</slash:comments>
		

			</item>
	<item>
		<title>Intuition for Taylor Series (DNA Analogy)</title>
		<link>https://betterexplained.com/articles/taylor-series/</link>
					<comments>https://betterexplained.com/articles/taylor-series/#disqus_thread</comments>
		
		<dc:creator><![CDATA[kalid]]></dc:creator>
		<pubDate>Thu, 24 Jan 2019 06:14:43 +0000</pubDate>
				<category><![CDATA[Calculus]]></category>
		<guid isPermaLink="false">https://betterexplained.com/?p=11904</guid>

					<description><![CDATA[Your body has a strange property: you can learn information about the entire organism from a single cell. Pick a cell, dive into the nucleus, and extract the DNA. You can now regrow the entire creature from that tiny sample. There's a math analogy here. Take a function, pick a specific point, and dive in. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Your body has a strange property: you can learn information about the <em>entire</em> organism from a single cell. Pick a cell, dive into the nucleus, and extract the DNA. You can now regrow the entire creature from that tiny sample.</p>

<p>There's a math analogy here. Take a function, pick a specific point, and dive in. You can pull out enough data from a single point to rebuild the entire function. Whoa. It's like remaking a movie from a single frame.</p>

<p>The Taylor Series discovers the "math DNA" behind a function and lets us rebuild it from a single data point. Let's see how it works.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/01/sine-frog.png" alt="Intuition for Taylor Series (DNA Analogy)" class="webpexpress-processed"></p>

<h2>Pulling information from a point</h2>

<p>Given a function like <img src="https://betterexplained.com/latex/e486fc934e0d604d2933.png" alt="$f(x) = x^2$" width="77" height="20" style="vertical-align:-4.53px" class="bex-math"></img>, what can we discover at a single location?</p>

<p>Normally we'd expect to calculate a single value, like <img src="https://betterexplained.com/latex/a44f4b38255157d64122.png" alt="$f(4) = 16$" width="76" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. But there's much more beneath the surface:</p>

<ul>
<li><img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img> = Value of function at point <img src="https://betterexplained.com/latex/d29b4638e781db60be51.png" alt="$x$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img></li>
<li><img src="https://betterexplained.com/latex/368afd59ace254ef750f.png" alt="$f'(x)$" width="41" height="19" style="vertical-align:-4.53px" class="bex-math"></img> = First derivative, or how fast the function is changing (the velocity)</li>
<li><img src="https://betterexplained.com/latex/350739c785798110a8ce.png" alt="$f''(x)$" width="44" height="19" style="vertical-align:-4.53px" class="bex-math"></img> = Second derivative, or how fast the <em>changes</em> are changing (the acceleration)</li>
<li><img src="https://betterexplained.com/latex/2ba8d4446f080fe6591a.png" alt="$f'''(x)$" width="48" height="19" style="vertical-align:-4.53px" class="bex-math"></img> = Third derivative, or how fast the <em>changes</em> in the changes are changing (acceleration of the acceleration)</li>
<li>And so on</li>
</ul>

<p>Investigating a single point reveals multiple, possibly infinite, bits of information about the behavior. (Some functions have an endless amount of data (derivatives) at a single point).</p>

<p>So, given all this information, what should we do? Regrow the organism from a single cell, of course! (<em>Maniacal cackle here.</em>)</p>

<h2>Growing a Function from a point</h2>

<p>Our plan is to grow a function from a single starting point. But how can we describe any function in a generic way?</p>

<p>The big aha moment: imagine any function, at its core, is a polynomial (with possibly infinite terms):</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/c3f9e195f6520dcc434c3212454e6dff.png' width='277' height='21' alt='\displaystyle{f(x) = c_0 + c_1 x + c_2 x^2 + c_3x^3 + \cdots}' class='tex'></p>

<p>To rebuild our function, we start at a fixed point (<img src="https://betterexplained.com/latex/a529c14e00c7eed408ea.png" alt="$c_0$" width="16" height="11" style="vertical-align:-3px" class="bex-math"></img>) and add in a bunch of other terms based on the value we feed it (like <img src="https://betterexplained.com/latex/3d79bfc7a1caa5d3ad6a.png" alt="$c_1x$" width="27" height="11" style="vertical-align:-2.71px" class="bex-math"></img>). The "DNA" is the values <img src="https://betterexplained.com/latex/aa1550c3003be1a685e1.png" alt="$c_0, c_1, c_2, c_3$" width="88" height="12" style="vertical-align:-3.51px" class="bex-math"></img> that describe our function exactly.</p>

<p>Ok, we have a generic "function format". But how do we find the coefficients for a specific function like <a href="https://betterexplained.com/articles/intuitive-understanding-of-sine-waves/">sin(x)</a> (height of angle x on the unit circle)? How do we pull out its DNA?</p>

<p>Time for the magic of 0.</p>

<p>Let's start by plugging in the function value at <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>. Doing this, we get:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/135e33269e43926435a87cdb93652f3e.png' width='249' height='19' alt='\displaystyle{f(0) = c_0 + 0 + 0 + 0 + \cdots = c_0}' class='tex'></p>

<p>Every term vanishes except <img src="https://betterexplained.com/latex/a529c14e00c7eed408ea.png" alt="$c_0$" width="16" height="11" style="vertical-align:-3px" class="bex-math"></img>, which makes sense: the starting point of our blueprint should be <img src="https://betterexplained.com/latex/1dc91764f2ad87509573.png" alt="$f(0)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. For <img src="https://betterexplained.com/latex/d0bb25e279f003cbfca3.png" alt="$f(x) = \sin(x)$" width="106" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, we can work out <img src="https://betterexplained.com/latex/08c4140bb7c232d4a520.png" alt="$c_0 = \sin(0) = 0$" width="119" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. We have our first bit of DNA!</p>

<h2>Getting More DNA</h2>

<p>Now that we know <img src="https://betterexplained.com/latex/a529c14e00c7eed408ea.png" alt="$c_0$" width="16" height="11" style="vertical-align:-3px" class="bex-math"></img>, how do we isolate <img src="https://betterexplained.com/latex/ebaac3a08f9d57216abd.png" alt="$c_1$" width="16" height="11" style="vertical-align:-2.71px" class="bex-math"></img> in this equation?</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/c3f9e195f6520dcc434c3212454e6dff.png' width='277' height='21' alt='\displaystyle{f(x) = c_0 + c_1 x + c_2 x^2 + c_3x^3 + \cdots}' class='tex'></p>

<p>Hrm. A few ideas:</p>

<ul>
<li><p>Can we set <img src="https://betterexplained.com/latex/fea0f5a399f6bea754ba.png" alt="$x = 1$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>? That gives <img src="https://betterexplained.com/latex/ebacb9f09209f783e2b0.png" alt="$f(1) = c_0 + c_1(1) + c_2(1^2) + c_3(1^3) + \cdots$" width="316" height="20" style="vertical-align:-4.53px" class="bex-math"></img> . Although we know <img src="https://betterexplained.com/latex/a529c14e00c7eed408ea.png" alt="$c_0$" width="16" height="11" style="vertical-align:-3px" class="bex-math"></img>, the other constants are summed together. We can't pull out <img src="https://betterexplained.com/latex/ebaac3a08f9d57216abd.png" alt="$c_1$" width="16" height="11" style="vertical-align:-2.71px" class="bex-math"></img> by itself.</p></li>
<li><p>What if we divide by <img src="https://betterexplained.com/latex/d29b4638e781db60be51.png" alt="$x$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img>? This gives:</p></li>
</ul>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/c4a3d8eeaa143d00694f2d17ca508080.png' width='266' height='40' alt='\displaystyle{\frac{f(x)}{x} = \frac{c_0}{x} + c_1 + c_2 x + c_3x^2 + \cdots}' class='tex'></p>

<p>Then we can set <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img> to make the other terms disappear... right? It's a nice idea, except we're now dividing by zero.</p>

<p>Hrm. This approach is really close. How can we <em>almost</em> divide by zero? <a href="https://betterexplained.com/articles/an-intuitive-introduction-to-limits/">Using the derivative</a>!</p>

<p>If we take the derivative of the blueprint of <img src="https://betterexplained.com/latex/146ef99c55cfa5dede83.png" alt="$f(x)$" width="35" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, we get:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/c28fa94d6c91e4ee9383422467fc97e8.png' width='357' height='21' alt='\displaystyle{f&#039;(x) = (c_0)&#039; + (c_1 x)&#039; + (c_2 x^2)&#039; + (c_3x^3)&#039; + \cdots}' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/ab56957b423ff22cc67fce8ece28c207.png' width='330' height='21' alt='\displaystyle{f&#039;(x) = 0 + c_1 + (2\cdot c_2 x) + (3\cdot c_3x^2) + \cdots}' class='tex'></p>

<p>Every power gets reduced by 1 and the <img src="https://betterexplained.com/latex/a529c14e00c7eed408ea.png" alt="$c_0$" width="16" height="11" style="vertical-align:-3px" class="bex-math"></img>, a constant value, becomes zero. It's almost too convenient.</p>

<p>Now we can isolate <img src="https://betterexplained.com/latex/ebaac3a08f9d57216abd.png" alt="$c_1$" width="16" height="11" style="vertical-align:-2.71px" class="bex-math"></img> using our <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img> trick:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/99182e90baa124019b19353cd9cd8400.png' width='282' height='20' alt='\displaystyle{f&#039;(0) = 0 + c_1 + (0) + (0) + \cdots = c_1}' class='tex'></p>

<p>In our example, <img src="https://betterexplained.com/latex/32ce352a1af1b55026d7.png" alt="$\sin'(x) = \cos(x)$" width="125" height="19" style="vertical-align:-4.53px" class="bex-math"></img> so we compute: <img src="https://betterexplained.com/latex/ab584dc4212357add813.png" alt="$f'(0) = \sin'(0) = \cos(0) = 1 = c_1$" width="259" height="19" style="vertical-align:-4.53px" class="bex-math"></img></p>

<p>Yay, one more bit of DNA! This is the magic of the Taylor series: by repeatedly applying the derivative and setting <img src="https://betterexplained.com/latex/0da288f667bccdebccad.png" alt="$x = 0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>, we can pull out the polynomial DNA.</p>

<p>Let's try another round:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/758acf0d00c644afb6bcd39da99ffef0.png' width='325' height='21' alt='\displaystyle{f&#039;&#039;(x) = 0 + 0 + (2\cdot c_2) + (3\cdot 2 c_3x^1) + \cdots}' class='tex'></p>

<p>After taking the second derivative, the powers are reduced again. The first two terms (<img src="https://betterexplained.com/latex/a529c14e00c7eed408ea.png" alt="$c_0$" width="16" height="11" style="vertical-align:-3px" class="bex-math"></img> and <img src="https://betterexplained.com/latex/3d79bfc7a1caa5d3ad6a.png" alt="$c_1x$" width="27" height="11" style="vertical-align:-2.71px" class="bex-math"></img>) disappear, and we can again isolate <img src="https://betterexplained.com/latex/92886d21310c43e0653a.png" alt="$c_2$" width="16" height="11" style="vertical-align:-2.71px" class="bex-math"></img> by setting <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/0f41d01c03ce41ab1012296d323482a0.png' width='240' height='20' alt='\displaystyle{f&#039;&#039;(0) = 0 + 0 + 2\cdot c_2 + 0 + \cdots}' class='tex'></p>

<p>For our sine example, <img src="https://betterexplained.com/latex/55c30903ce6ba5abd3e6.png" alt="$\sin'' = -\sin$" width="95" height="16" style="vertical-align:-1.49px" class="bex-math"></img>, so:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/bdbe27dc403bfebf2c37062c58b6b2bd.png' width='301' height='20' alt='\displaystyle{f&#039;&#039;(0) = \sin&#039;&#039;(0) = -\sin(0) = 0 = 2\cdot c_2}' class='tex'></p>

<p>or <img src="https://betterexplained.com/latex/4bb7d3feb2cd5dedb58c.png" alt="$c_2 = 0$" width="49" height="15" style="vertical-align:-2.71px" class="bex-math"></img>.</p>

<p>As we keep taking derivatives, we're performing more multiplications and growing a factorial in front of each term (1!, 2!, 3!).</p>

<p><strong>The Taylor Series for a function around point x=0 is:</strong></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/ee1eb7e64f894e2c7e52247c0280bd5e.png' width='384' height='40' alt='\displaystyle{f(x) = f(0) + f&#039;(0) x + \frac{f&#039;&#039;(0)}{2!}x^2 + \frac{f&#039;&#039;&#039;(0)}{3!}x^3 + \cdots}' class='tex'></p>

<p>(Formally, the Taylor series around the point <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img> is called the MacLaurin series.)</p>

<p><strong>The generalized Taylor series, extracted from any point a is:</strong></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/9d6eb23fb27499213acb493c30f7b495.png' width='528' height='40' alt='\displaystyle{f(x) = f(a)+{\frac {f&#039;(a)}{1!}}(x-a)+{\frac {f&#039;&#039;(a)}{2!}}(x-a)^{2}+{\frac {f&#039;&#039;&#039;(a)}{3!}}(x-a)^{3}+\cdots }' class='tex'></p>

<p>The idea is the same. Instead of our regular blueprint, we use:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/9caa9850edf20b8a852a665c2dc42eaf.png' width='415' height='21' alt='\displaystyle{f(x) = c_0 + c_1 (x - a) + c_2 (x-a)^2 + c_3(x-a)^3 + \cdots}' class='tex'></p>

<p>Since we're growing from <img src="https://betterexplained.com/latex/114fce01e54a95fb5fed.png" alt="$f(a)$" width="34" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, we can see that <img src="https://betterexplained.com/latex/68bb6d686af0f8aa837b.png" alt="$f(a) = c_0 + 0 + 0 + \dots = c_0$" width="220" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. The other coefficients can be extracted by taking derivatives and setting <img src="https://betterexplained.com/latex/ecdd22b8c2480a3f663e.png" alt="$x = a$" width="45" height="13" style="vertical-align:-1.49px" class="bex-math"></img> (instead of <img src="https://betterexplained.com/latex/597d1bdaeafd020ec450.png" alt="$x =0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>).</p>

<h2>Example: Taylor Series of sin(x)</h2>

<p>Plugging in derivatives into the formula above, here's the Taylor series of <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> around <img src="https://betterexplained.com/latex/0da288f667bccdebccad.png" alt="$x = 0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/30d480d58a87ebd02b26b350551de380.png' width='256' height='41' alt='\displaystyle{\sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \dots}' class='tex'></p>

<p>And here's what that looks like:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/sine/sine-better-models.png" alt="Intuition for Taylor Series (DNA Analogy)" class="webpexpress-processed"></p>

<p>A few notes:</p>

<p><strong>1) Sine has infinite terms</strong></p>

<p>Sine is an infinite wave, and as you can guess, needs an infinite number of terms to keep it going. Simpler functions (like <img src="https://betterexplained.com/latex/191f47c56e099eb08568.png" alt="$f(x) = x^2 + 3$" width="108" height="20" style="vertical-align:-4.53px" class="bex-math"></img>) are already in their "polynomial format" and don't have infinite derivatives to keep the DNA going.</p>

<p><strong>2) Sine is missing every other term</strong></p>

<p>If we repeatedly take the derivative of sine at x = 0 we get:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/f15abf5ff736c599cc9a4d0f779a29f2.png' width='520' height='24' alt='\displaystyle{\sin(0) \xrightarrow{\text{derive}} \cos(0) \xrightarrow{\text{derive}} -\sin(0) \xrightarrow{\text{derive}} -\cos(0) \xrightarrow{\text{derive}} \sin(0) \dots}' class='tex'></p>

<p>with values:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/d5f5ac139c78aeba48bbaa954b08806a.png' width='101' height='16' alt='\displaystyle{0, 1, 0, -1, \dots}' class='tex'></p>

<p>Ignoring the division by the factorial, we get the pattern:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/492503f56b69e9866a6413b6ad59da34.png' width='410' height='21' alt='\displaystyle{(0) + (1)x^1 + (0)x^2 + (-1)x^3 + (0)x^4 + (-1)x^5+ \dots }' class='tex'></p>

<p>So the DNA of sine is something like [0, 1, 0, -1] repeating.</p>

<p><strong>3) Different starting positions have different DNA</strong></p>

<p>For fun, here's the Taylor series of <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> starting at <img src="https://betterexplained.com/latex/1e22e0d411fdb1561dd2.png" alt="$x =\pi$" width="45" height="13" style="vertical-align:-1.49px" class="bex-math"></img> (<a href="https://www.wolframalpha.com/input/?i=taylor+series+sin+x+at+x%3Dpi">link</a>):</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/01/taylor_series_x_pi.png" alt="taylor series sin x at x=pi - Wolfram|Alpha 2019-01-18 16-45-04" class="webpexpress-processed"></p>

<p>A few notes:</p>

<ul>
<li><p>The DNA is now something like [0, -1, 0, 1]. The cycle is similar, but the starting value has changed since we're starting at <img src="https://betterexplained.com/latex/ac6169e88d2d649830b3.png" alt="$x=\pi$" width="45" height="13" style="vertical-align:-1.49px" class="bex-math"></img>.</p></li>
<li><p>Written as calculated numbers, the denominators 1, 6, 120, 5040 look strange. But they're just every other factorial: 1! = 1, 3! = 6, 5! =120, 7! = 5040. In general, the Taylor series can have gnarly denominators.</p></li>
<li>The <img src="https://betterexplained.com/latex/f4e59142dd23f9b006da.png" alt="$O(x^{12})$" width="53" height="20" style="vertical-align:-4.53px" class="bex-math"></img> term means there are other components of order (power) <img src="https://betterexplained.com/latex/6affb21ca69aa0f53bb6.png" alt="$x^{12}$" width="25" height="16" style="vertical-align:-0.2px" class="bex-math"></img> and higher. Because <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img> has infinite derivatives, we have infinite terms and the computer has to cut us off somewhere. (<em>You've had enough Tayloring for today, buddy.</em>)</li>
</ul>

<h2>Application: Function Approximations</h2>

<p>A popular use of Taylor series is getting a quick approximation for a function. If you want a tadpole, do you need the DNA for the entire frog?</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2019/01/sine-frog.png" alt="Intuition for Taylor Series (DNA Analogy)" class="webpexpress-processed"></p>

<p>The Taylor series has a bunch of terms, typically ordered by importance:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/ee1eb7e64f894e2c7e52247c0280bd5e.png' width='384' height='40' alt='\displaystyle{f(x) = f(0) + f&#039;(0) x + \frac{f&#039;&#039;(0)}{2!}x^2 + \frac{f&#039;&#039;&#039;(0)}{3!}x^3 + \cdots}' class='tex'></p>

<ul>
<li><img src="https://betterexplained.com/latex/33cdb201016b1574080c.png" alt="$c_0 = f(0)$" width="73" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, the constant term, is the exact value at the point</li>
<li><img src="https://betterexplained.com/latex/a350b811b3347ca926f8.png" alt="$c_1 = f'(0)x$" width="90" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, the linear term, tells us what speed to move from our point</li>
<li><img src="https://betterexplained.com/latex/d81339af32640aa21e52.png" alt="$c_2= \frac{f''(0)}{2!}x^2$" width="97" height="26" style="vertical-align:-6.26px" class="bex-math"></img>, the quadratic term, tells us how much to accelerate away from our point</li>
<li>and so on</li>
</ul>

<p>If we only need a prediction for a few instants around our point, the initial position &amp; velocity may be good enough:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/efdfd256e95cde73f061c50626b9209c.png' width='483' height='20' alt='\displaystyle{\text{Linear model} = \text{initial point} + \text{velocity effect} = f(0) + f&#039;(0)x}' class='tex'></p>

<p>If we're tracking for longer, then acceleration becomes important:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/32cdc4341d9e885a2fa6e11b51cfda3e.png' width='545' height='17' alt='\displaystyle{\text{Quadratic model} = \text{initial point} + \text{velocity effect} + \text{acceleration effect}}' class='tex'>
<img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/af720b70459d131f58ddebc1cb150108.png' width='218' height='37' alt='\displaystyle{ = f(0) + f&#039;(0)x + \frac{1}{2}f&#039;&#039;(0)x^2}' class='tex'></p>

<p>As we get further from our starting point, we need more terms to keep our prediction accurate. For example, the linear model <img src="https://betterexplained.com/latex/e83e9a194630e44240ae.png" alt="$\sin(x) = x$" width="82" height="19" style="vertical-align:-4.53px" class="bex-math"></img> is a good prediction around <img src="https://betterexplained.com/latex/7721ba6d36060e43dd1f.png" alt="$x=0$" width="44" height="14" style="vertical-align:-1.49px" class="bex-math"></img>. As we get further out, we need to account for more terms.</p>

<p>Similarly, <img src="https://betterexplained.com/latex/c121f636bb1c71ed9c57.png" alt="$e^x \sim 1 + x$" width="83" height="14" style="vertical-align:-1.49px" class="bex-math"></img> works well for small interest rates: 1% discrete interest is 1.01 after one time period, 1% continuous interest is a tad higher than 1.01. As time goes on, the linear model falls behind because it ignores the compounding effects.</p>

<h2>Application: Comparing Functions</h2>

<p>What's a common application of DNA? Paternity tests.</p>

<p>If we have a few functions, we can compare their Taylor series to see if they're related.</p>

<p>Here's the expansions of <img src="https://betterexplained.com/latex/310374f3e5e01fc30505.png" alt="$\sin(x)$" width="47" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, <img src="https://betterexplained.com/latex/279dacec896950f1f76b.png" alt="$\cos(x)$" width="49" height="19" style="vertical-align:-4.53px" class="bex-math"></img>, and <img src="https://betterexplained.com/latex/e7aded5a42342d4b3f23.png" alt="$e^x$" width="18" height="13" style="vertical-align:-0.2px" class="bex-math"></img>:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/b5ea819a6da480914052558967c23ba2.png' width='374' height='41' alt='\displaystyle{ \sin x = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \dots \xrightarrow{DNA} [0, 1, 0 -1, \dots] }' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/f59b17744d88deb5f02df60480907e0b.png' width='375' height='40' alt='\displaystyle{ \cos x = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \dots \xrightarrow{DNA} [1, 0, -1, 0, \dots] }' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/b40225aedb74d8648649c58ecb22cfa7.png' width='373' height='41' alt='\displaystyle{ e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots \xrightarrow{DNA} [1, 1, 1, 1, \dots] }' class='tex'></p>

<p>There's a family resemblance in the sequences, right? Clean powers of <img src="https://betterexplained.com/latex/d29b4638e781db60be51.png" alt="$x$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img> divided by a factorial?</p>

<p>One problem is the sequence for <img src="https://betterexplained.com/latex/e7aded5a42342d4b3f23.png" alt="$e^x$" width="18" height="13" style="vertical-align:-0.2px" class="bex-math"></img> has positive terms, while sine and cosine alternate signs. How can we link these together?</p>

<p>Euler's great insight was realizing an <a href="https://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">imaginary number</a> could swap the sign from positive to negative:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/cf7a66a77e3612eab7cde26914da2c7e.png' width='590' height='202' alt='{\begin{aligned}e^{ix}&amp;=1+ix+{\frac {(ix)^{2}}{2!}}+{\frac {(ix)^{3}}{3!}}+{\frac {(ix)^{4}}{4!}}+{\frac {(ix)^{5}}{5!}}+{\frac {(ix)^{6}}{6!}}+{\frac {(ix)^{7}}{7!}}+{\frac {(ix)^{8}}{8!}}+\cdots \\[8pt]&amp;=1+ix-{\frac {x^{2}}{2!}}-{\frac {ix^{3}}{3!}}+{\frac {x^{4}}{4!}}+{\frac {ix^{5}}{5!}}-{\frac {x^{6}}{6!}}-{\frac {ix^{7}}{7!}}+{\frac {x^{8}}{8!}}+\cdots \\[8pt]&amp;=\left(1-{\frac {x^{2}}{2!}}+{\frac {x^{4}}{4!}}-{\frac {x^{6}}{6!}}+{\frac {x^{8}}{8!}}-\cdots \right)+i\left(x-{\frac {x^{3}}{3!}}+{\frac {x^{5}}{5!}}-{\frac {x^{7}}{7!}}+\cdots \right)\\[8pt]&amp;=\cos x+i\sin x.\end{aligned}} ' class='tex'></p>

<p>Whoa. Using an imaginary exponent and separating into odd/even powers reveals that sine and cosine are hiding inside the exponential function. Amazing.</p>

<p>Although this proof of <a href="https://betterexplained.com/articles/intuitive-understanding-of-eulers-formula/">Euler's Formula</a> doesn't show <em>why</em> the imaginary number makes sense, it reveals the baby daddy hiding backstage.</p>

<h2>Appendix: Assorted Aha! Moments</h2>

<p><strong>Relationship to Fourier Series</strong></p>

<p>The Taylor Series extracts the "polynomial DNA" and the <a href="https://betterexplained.com/articles/an-interactive-guide-to-the-fourier-transform/">Fourier Series/Transform</a> extracts the "circular DNA" of a function. Both see functions as built from smaller parts (polynomials or exponential paths).</p>

<p><strong>Does the Taylor Series always work?</strong></p>

<p>This gets into mathematical analysis beyond my depth, but certain functions aren't easily (or ever) approximated with polynomials.</p>

<p>Notice that powers like <img src="https://betterexplained.com/latex/0ccfd13c9ad9cf535b9a.png" alt="$x^2, x^3$" width="45" height="19" style="vertical-align:-3.51px" class="bex-math"></img> explode as <img src="https://betterexplained.com/latex/d29b4638e781db60be51.png" alt="$x$" width="11" height="9" style="vertical-align:-0.2px" class="bex-math"></img> grows. In order to have a slow, gradual curve, you need an army of polynomial terms fighting it out, with one winner barely emerging. If you stop the train too early, the approximation explodes again.</p>

<p>For example, here's the Taylor Series for <img src="https://betterexplained.com/latex/98ab825382c837a1ce1a.png" alt="$\ln(1 + x)$" width="71" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. The black line is the curve we want, and adding more terms, even dozens, barely gets us accuracy beyond <img src="https://betterexplained.com/latex/387985579f743d9e2822.png" alt="$x=1.0$" width="58" height="14" style="vertical-align:-1.49px" class="bex-math"></img>. It's just too hard to maintain a gentle slope with terms that want to run hog wild.</p>

<p><img decoding="async" src="https://upload.wikimedia.org/wikipedia/commons/2/27/Logarithm_GIF.gif" alt="Intuition for Taylor Series (DNA Analogy)" /></p>

<p><a href="https://en.wikipedia.org/wiki/File:Logarithm_GIF.gif">source</a></p>

<p>In this case, we only have a radius of convergence where the approximation stays accurate (such as around <img src="https://betterexplained.com/latex/ae32346c9156b84099cc.png" alt="$|x| &lt; 1$" width="54" height="19" style="vertical-align:-4.51px" class="bex-math"></img>).</p>

<p><strong>Turning geometric to algebraic definitions</strong></p>

<p>Sine is often defined geometrically: the height of a line on a circular figure.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/sine/sine-unit-circle.png" alt="Intuition for Taylor Series (DNA Analogy)" /></p>

<p>Turning this into an equation seems really hard. The Taylor Series gives us a process: If we know a single value and how it changes (the derivative), we can reverse-engineer the DNA.</p>

<p>Similarly, the description of <img src="https://betterexplained.com/latex/e7aded5a42342d4b3f23.png" alt="$e^x$" width="18" height="13" style="vertical-align:-0.2px" class="bex-math"></img> as "the function with its derivative equal to the current value" yields the DNA [1, 1, 1, 1], and polynomial <img src="https://betterexplained.com/latex/6a164d5d8401601a6ff6.png" alt="$f(x) = 1 + \frac{1}{1!}x + \frac{1}{2!}x^2 + \frac{1}{3!}x^3 + \dots$" width="278" height="23" style="vertical-align:-6.55px" class="bex-math"></img>. We went from a verbal description to an equation.</p>

<p>Phew! A few items to ponder.</p>

<p>Happy math.</p>

]]></content:encoded>
					
					<wfw:commentRss>https://betterexplained.com/articles/taylor-series/feed/</wfw:commentRss>
			<slash:comments>17</slash:comments>
		

			</item>
	<item>
		<title>How to Add 1 through 100 using Calculus</title>
		<link>https://betterexplained.com/articles/how-to-add-1-to-100-using-calculus/</link>
					<comments>https://betterexplained.com/articles/how-to-add-1-to-100-using-calculus/#disqus_thread</comments>
		
		<dc:creator><![CDATA[kalid]]></dc:creator>
		<pubDate>Fri, 17 Feb 2017 20:52:31 +0000</pubDate>
				<category><![CDATA[Calculus]]></category>
				<category><![CDATA[Math]]></category>
		<guid isPermaLink="false">https://betterexplained.com/?p=10127</guid>

					<description><![CDATA[Earlier we saw a few ways to add a set of numbers (1 to 10) . And the formula we found was: It seems that regular arithmetic, algebra, geometry, or even statistics could help work out the equation. But how about Calculus? Is this bringing a nuclear missile to a gun fight? Let's find out. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Earlier we saw a few ways to <a href="https://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/">add a set of numbers (1 to 10)</a>.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2017/02/1to100Strategies.png" alt="adding 1 to 100 strategies" class="webpexpress-processed"></p>

<p>And the formula we found was:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/2e9ef5f601b25cef7278a83960a9f8f5.png' width='225' height='39' alt='\displaystyle{\text{Sum from 1 to n} = \frac{n(n+1)}{2}}' class='tex'></p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/eaa99c27dd993571240dd1f837b4c6b5.png' width='434' height='39' alt='\displaystyle{\text{Sum from 1 to 100} = \frac{100(100+1)}{2} = (50)(101) = 5050}' class='tex'></p>

<iframe src="https://instacalc.com/50284/embed" width="450" height="200" frameborder="0"></iframe>

<p>It seems that regular arithmetic, algebra, geometry, or even statistics could help work out the equation.</p>

<p>But how about Calculus? Is this bringing a nuclear missile to a gun fight?</p>

<p>Let's find out.</p>

<p>The sequence to add (1 2 3 4 5 6 7 8 9 10...) looks a lot like <img src="https://betterexplained.com/latex/4380e9965d2a076913ac.png" alt="$f(x) = x$" width="69" height="19" style="vertical-align:-4.53px" class="bex-math"></img>. At every position on the x-axis, we put in a number and get the same one out.</p>

<p>Intuitively, the integral is "repeatedly adding a bunch of stuff" -- it seems like we could put it to work. From the rules of Calculus (or <a href="http://www.wolframalpha.com/input/?i=integral+of+f(x)+%3D+x">using Wolfram Alpha</a>) we get this:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/78e3616bdf1f6f24e6385c3375512160.png' width='85' height='41' alt='\displaystyle{&#10;\int x = \frac{1}{2} x^2&#10;}' class='tex'></p>

<p>Intuitively: Add up things following the <img src="https://betterexplained.com/latex/4380e9965d2a076913ac.png" alt="$f(x) = x$" width="69" height="19" style="vertical-align:-4.53px" class="bex-math"></img> pattern and you end up with <img src="https://betterexplained.com/latex/47a831e8a17e79312cb0.png" alt="$\frac{1}{2} x^2$" width="33" height="22" style="vertical-align:-6.25px" class="bex-math"></img>.</p>

<p>Well, let's see: the actual sum from 1 to 100 is 5050. But using the Calculus equation we get:</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/7263cbcdecd0ec1535ba69e0981101af.png' width='244' height='37' alt='\displaystyle{&#10;\frac{1}{2} x^2 = \frac{1}{2}100^2 = \frac{10,000}{2} = 5000&#10;}' class='tex'></p>

<p>Uh oh: there's a difference. What's going on?</p>

<p><strong>Calculus works with continuous patterns, and we used a discrete one</strong>.</p>

<p>Here's what's happening:</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2016/07/calculus-sum-x.png" alt="calculus add area under x" class="webpexpress-processed"></p>

<p>Calculus was built to measure <em>smoothly changing functions</em>, like a line, parabola, circle, etc. The pattern we have is a jumpy staircase (going from 1 to 2 without ever passing through 1.5, or 1.1, or 1.0001). In math class, books harp on analyzing whether a function is "continuous", aka changes smoothly enough for Calculus to work.</p>

<p>So when a pattern changes smoothly, Calculus works great. If a pattern changes suddenly, Calculus can only give an approximate answer. So what's the plan?</p>

<p>Use Calculus where possible, on the smooth part, and adjust for errors in the jumpy part.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2017/02/1to100integral.png" alt="calculus integral of x discrete" class="webpexpress-processed"></p>

<p>The area under the line is the integral. We a bunch of triangles above the line we need to include.</p>

<ul>
<li>How many of them? 1 for each item (x)</li>
<li>How big are they? They're half a of a 1x1 square, so they have area 1/2.</li>
<li>What's the total area to add back in? <img src="https://betterexplained.com/latex/bbe4061d3958a8930807.png" alt="$x \frac{1}{2} = \frac{x}{2}$" width="65" height="22" style="vertical-align:-6.25px" class="bex-math"></img></li>
</ul>

<p>So our final formula should be</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/a83e5801e5f8b09e484bcf52a3e1f8ee.png' width='267' height='37' alt='\displaystyle{\text{Integral} + \text{Adjustment} = \frac{1}{2} x^2 + \frac{x}{2}}' class='tex'></p>

<p>Aha! Learning Calculus doesn't mean we hunt around for Official Calculus Problems.</p>

<p>Nope. Take your scenario (adding 1 to 100) and realize what Calculus brings to the table: finding patterns in smoothly changing functions. Use Calculus on the smooth parts and adjust (or ignore) the other parts.</p>

<p>(Ironically, Calculus works by making jumpy approximations for smooth functions, and is in fact "jumpy" under the hood. If you are planning on working with jumpy patterns, use <a href="http://homepages.math.uic.edu/~kauffman/DCalc.pdf">Discrete Calculus</a>.)</p>

<h2>Example: Adding the first n squares</h2>

<p>Let's take this further: what's your guess for the sum of the first 100 square numbers?</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/7ae76815ac8f2a5af6b1c260d39c045a.png' width='226' height='18' alt='\displaystyle{1^2 + 2^2 + 3^2 + 4^2 + ... + 100^2}' class='tex'></p>

<p>Hrm. Getting the exact formula is tricky. But maybe we don't need the exact count, just an estimate.</p>

<p>With Calculus, we'd say: The pattern isn't continuous, but it looks like <img src="https://betterexplained.com/latex/e486fc934e0d604d2933.png" alt="$f(x) = x^2$" width="77" height="20" style="vertical-align:-4.53px" class="bex-math"></img>. Let's integrate x^2 from 0 to 100.</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/a1683483ce3ed88c798ed6a365a1da7c.png' width='93' height='41' alt='\displaystyle{&#10;\int x^2 = \frac{1}{3} x^3&#10;}' class='tex'></p>

<p>The indefinite integral is <img src="https://betterexplained.com/latex/38d4a5529dcbd4dd684c.png" alt="$\frac{1}{3} x^3$" width="33" height="23" style="vertical-align:-6.53px" class="bex-math"></img>, the running total for how much we have. From 0 to 100 it would be</p>

<p><img src='https://betterexplained.com/wp-content/plugins/wp-latexrender/pictures/2e892d985ba87d8ca6ce0b52351ab5f9.png' width='240' height='38' alt='\displaystyle{&#10;\frac{1}{3}100^3 = \frac{1,000,000}{3} \sim 333,333&#10;}' class='tex'></p>

<p>That's our guess, without a calculator. And the actual answer? 338350.</p>

<p><img decoding="async" src="https://betterexplained.com/wp-content/uploads/2016/07/calculus-arithmetic-3.png" alt="calculus area under x squared" class="webpexpress-processed"></p>

<p>How close were we? 99.9%. Not bad for something we worked out by hand in a minute!</p>

<p>Truly internalizing Calculus means it helps other elements of your math understanding, even regular addition problems.</p>

<p>Happy math.</p>

<p>PS. To keep building your intuition, check out the <a href="https://betterexplained.com/guides/calculus/">Calculus Guide</a>.</p>

]]></content:encoded>
					
					<wfw:commentRss>https://betterexplained.com/articles/how-to-add-1-to-100-using-calculus/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		

			</item>
	</channel>
</rss>
