<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>freakingpenguin blog</title><id>https://freakingpenguin.com/feeds/tags/gnuplot.xml</id><subtitle>Tag: gnuplot</subtitle><updated>2024-03-06T00:00:00Z</updated><link href="https://freakingpenguin.com/feeds/tags/gnuplot.xml" rel="self" /><link href="https://freakingpenguin.com" /><entry><title>Fourier Part 2: Integrating a discontinuous function</title><id>https://freakingpenguin.com/blog/fourier-part-2-integrating-a-discontinuous-function.html</id><author><name>Richard Sent</name><email>richard@freakingpenguin.com</email></author><updated>2021-04-10T00:00:00Z</updated><link href="https://freakingpenguin.com/blog/fourier-part-2-integrating-a-discontinuous-function.html" rel="alternate" /><content type="html">&lt;h1&gt;Checking if &lt;code&gt;gsl_integration_qng()&lt;/code&gt; will work&lt;/h1&gt;&lt;p&gt;At the end of &lt;a href=&quot;https://www.freakingpenguin.com/blog/fourier-part-1-what-is-a-fourier-series.html&quot;&gt;part 1&lt;/a&gt;, I suggested
that &lt;code&gt;gsl_integration_qng()&lt;/code&gt; would not be able to successfully
integrate the function we want to find the Fourier series of. Here is
the function I will find the Fourier series of.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;/static/images/graphs/gnuplot-fourier-function-orig.png&quot; alt=&quot;A graph showing a sawtooth wave function with discontinuous jumps
between teeth. Similar in shape to a series of forward slashes.&quot; /&gt;&lt;/p&gt;&lt;p&gt;Because this function is discontinuous, we have to approximate it with
a Fourier series instead of a Taylor series. If we recall the formulas
shown in the last part, we know that we will have to integrate &lt;math display=&quot;inline&quot; alttext=&quot;f(x)&quot;&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt; from &lt;math display=&quot;inline&quot; alttext=&quot;0&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/math&gt; to &lt;math display=&quot;inline&quot; alttext=&quot;2l&quot;&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;, among other things. Before calculating the
Fourier series, let's try using &lt;code&gt;gsl_integration_qng()&lt;/code&gt; to perform
basic integration.&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;gsl/gsl_integration.h&amp;gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;/* f(x) = x for 0 &amp;lt;= x &amp;lt; 4, repeating */&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;void&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;4&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;fmod&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;8&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;function&lt;/span&gt; = &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;num_evals&lt;/span&gt;;

  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_qng&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt;,
      		&lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt;,
      		&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;, &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;num_evals&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;result error num_evals\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;%f %f %zu\n&amp;quot;&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;num_evals&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;Results:
&lt;code&gt;|    result |    error | num_evals |
| 14.804436 | 8.015135 |        21 |&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Well, something happened here, but it's not quite what we wanted. We
told &lt;code&gt;GSL&lt;/code&gt; to take gsl_integral-i. Since we're dealing with a
discontinuous function, we can rewrite this as gsl-integral-rewrite-i. Now I'm no mathematician, but this answer should be
16, not 14.8. Additionally, our error is very large, and if I attempt
to lower &lt;code&gt;err_rel&lt;/code&gt; to a smaller value, &lt;code&gt;GSL&lt;/code&gt; yells at me saying that
it failed to reach the requested error.&lt;/p&gt;&lt;p&gt;This is not a problem on &lt;code&gt;GSL&lt;/code&gt;'s end. We aren't using the right
integration function for the job! According to the &lt;code&gt;GSL&lt;/code&gt; manual,&lt;/p&gt;&lt;blockquote&gt;The QNG algorithm is a non-adaptive procedure which uses fixed
Gauss-Kronrod-Patterson abscissae to sample the integrand at a maximum
of 87 points. It is provided for fast integration of smooth functions.
&lt;/blockquote&gt;&lt;p&gt;While that's a lot of words, the important part is the last sentence. &lt;code&gt;gsl_integration_qng()&lt;/code&gt; is for smooth functions. Our function is not smooth, it has a discontinuity! We will need to find an alternative
function in &lt;code&gt;GSL&lt;/code&gt; that can handle discontinuous graphs.&lt;/p&gt;&lt;aside&gt;&lt;p&gt;Strictly speaking, this isn't actually true. Because the discontinuity occurs
at the limits of integration for our function (as long as we only integrate
from 0 to 4), we can use &lt;code&gt;gsl_integration_qng()&lt;/code&gt;. Not all functions
are like this, so it's still best to find an alternative.&lt;/p&gt;&lt;/aside&gt;&lt;h1&gt;&lt;code&gt;gsl_integration_qng()&lt;/code&gt; and discontinuities&lt;/h1&gt;&lt;p&gt;Fortunately we do not have to look far. The very next function
mentioned in the
&lt;a href=&quot;https://www.gnu.org/software/gsl/doc/html/integration.html&quot;&gt;manual&lt;/a&gt; is what we need. &lt;code&gt;gsl_integration_qag()&lt;/code&gt; has the following signature.&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_qag&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_function&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;,
            &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt;,
            &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;epsabs&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;epsrel&lt;/span&gt;,
            &lt;span class=&quot;syntax-symbol&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;key&lt;/span&gt;,
            &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;workspace&lt;/span&gt;,
            &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;abserr&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;f&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, &lt;code&gt;epsabs&lt;/code&gt;, &lt;code&gt;epsrel&lt;/code&gt;, &lt;code&gt;result&lt;/code&gt;, and &lt;code&gt;abserr&lt;/code&gt; are all the same as &lt;code&gt;gsl_integration_qng()&lt;/code&gt;. We can see that several
new terms are introduced however.&lt;/p&gt;&lt;p&gt;&lt;code&gt;workspace&lt;/code&gt; is a pointer to an area of memory used by &lt;code&gt;gsl_integration_qag&lt;/code&gt;. We allocate space by using the &lt;code&gt;gsl_integration_workspace_alloc()&lt;/code&gt; function, This function is passed
an integer to adjust how much memory we want to allocate. Whatever
integer we pass to &lt;code&gt;gsl_integration_workspace_alloc()&lt;/code&gt;we need to
ensure that &lt;code&gt;limit&lt;/code&gt; is the same. This way, &lt;code&gt;gsl_integration_qag&lt;/code&gt;
knows how much memory it has available.&lt;/p&gt;&lt;p&gt;&lt;code&gt;key&lt;/code&gt; is a integer between 0 through 6. &lt;code&gt;GSL&lt;/code&gt; recommends using higher values when integrating smooth
functions, and lower values when functions are discontinuous.
&lt;/p&gt;&lt;p&gt;Let's see how well &lt;code&gt;gsl_integration_qag()&lt;/code&gt; performs!
&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;gsl/gsl_integration.h&amp;gt;&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;void&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;4&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;fmod&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1024&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;w&lt;/span&gt;
    = &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace_alloc&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;8&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;function&lt;/span&gt; = &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1e&lt;/span&gt;-&lt;span class=&quot;syntax-symbol&quot;&gt;7&lt;/span&gt;;

  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_qag&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt;,
      		&lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt;,
      		&lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;w&lt;/span&gt;,
      		&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-comment&quot;&gt;/* get into the habit of freeing memory when done! */&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace_free&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;result error\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;%f %f\n&amp;quot;&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;Results:
&lt;code&gt;| result | error |
|   16.0 |   0.0 |&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Look at that! We are getting the exact value we expected, even though
we're integrating with a discontinuity. We are now at the point where
we can calculate the Fourier series for our function.
&lt;/p&gt;&lt;h1&gt;Generating our Fourier series&lt;/h1&gt;&lt;p&gt;Let's recall that we can find our &lt;math display=&quot;inline&quot; alttext=&quot;a_{n}&quot;&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt; and &lt;math display=&quot;inline&quot; alttext=&quot;b_{n}&quot;&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt; coefficients
with the following formulas.
&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;a_{n}=\frac{1}{l}\int_{0}^{2l}f(x)\cos(\frac{n\pi x}{l})dx&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;b_{n}=\frac{1}{l}\int_{0}^{2l}f(x)\sin(\frac{n\pi x}{l})dx&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;GSL&lt;/code&gt; expects us to only pass one function to it as an argument. That
means that, unlike before, we can't just pass a pointer to our function
&lt;code&gt;f(x)&lt;/code&gt;, as &lt;code&gt;GSL&lt;/code&gt; won't be multiplying it by the cosine and sine terms.&lt;/p&gt;&lt;p&gt;There are a couple of solutions to this that I can think of. The first
is to make use of the &lt;code&gt;void *&lt;/code&gt; argument that &lt;code&gt;GSL&lt;/code&gt; includes in the
&lt;code&gt;gsl_function&lt;/code&gt; structure. Using this, we could pass extra information
to &lt;code&gt;f(x)&lt;/code&gt;, adapting it to our specific n value.
&lt;/p&gt;&lt;p&gt;Alternatively, we could write a parent function, like &lt;code&gt;aorb_subn()&lt;/code&gt;. This
function would then be stored in the &lt;code&gt;gsl_function&lt;/code&gt; structure. The
advantage of this approach is that the function we want the Fourier
series of, &lt;code&gt;f(x)&lt;/code&gt;, is distinct in our code. This should make it a bit
easier to change &lt;code&gt;f(x)&lt;/code&gt; to any function that we want. This is the
approach that I will take. &lt;/p&gt;&lt;p&gt;To make this code hopefully a bit more modular, I moved the
integration out of main, and instead into a function called
&lt;code&gt;get_aorb_subn()&lt;/code&gt;. This function calculates the nth Fourier
coefficient for a function &lt;code&gt;f(x)&lt;/code&gt;, using the variable &lt;code&gt;get_a&lt;/code&gt; to
determine if it should calculate &lt;math display=&quot;inline&quot; alttext=&quot;a_{n}&quot;&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt; or &lt;math display=&quot;inline&quot; alttext=&quot;b_{n}&quot;&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;. It may be
better to wrap this in a &lt;code&gt;fourier&lt;/code&gt; structure that contains the two
terms, but I elected not to do that to hopefully maintain some vague
semblance of readability.&lt;/p&gt;&lt;p&gt;This is what the code to calculate the Fourier series looks like.&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;stdbool.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;gsl/gsl_integration.h&amp;gt;&lt;/span&gt;

&lt;span class=&quot;syntax-keyword&quot;&gt;#define&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;PI&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;3&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;14159&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;4&lt;/span&gt;;
&lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;l&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt; / &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt;;

&lt;span class=&quot;syntax-special&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;aorb_params&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;*&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;void&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;calc_a&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;;

&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;void&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;fmod&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;aorb_subn&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;aorb_params&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;-&amp;gt;&lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;trig&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;-&amp;gt;&lt;span class=&quot;syntax-symbol&quot;&gt;calc_a&lt;/span&gt; ? &lt;span class=&quot;syntax-symbol&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;PI&lt;/span&gt;*&lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt;*&lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;/&lt;span class=&quot;syntax-symbol&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; : &lt;span class=&quot;syntax-symbol&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;PI&lt;/span&gt;*&lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt;*&lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;/&lt;span class=&quot;syntax-symbol&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;-&amp;gt;&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;trig&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;get_aorb_subn&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;*&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;void&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt;,
      	       &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;get_a&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;normalization&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;/&lt;span class=&quot;syntax-symbol&quot;&gt;l&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get_a&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt; == &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;normalization&lt;/span&gt; /= &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt;;

  &lt;span class=&quot;syntax-symbol&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1024&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;w&lt;/span&gt;
    = &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace_alloc&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt; .&lt;span class=&quot;syntax-symbol&quot;&gt;function&lt;/span&gt; = &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;aorb_subn&lt;/span&gt;,
    .&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt; = &amp;amp;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;aorb_params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt; .&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;, .&lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;n&lt;/span&gt;,
      		 .&lt;span class=&quot;syntax-symbol&quot;&gt;calc_a&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;get_a&lt;/span&gt; &lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1e&lt;/span&gt;-&lt;span class=&quot;syntax-symbol&quot;&gt;7&lt;/span&gt;;

  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_qag&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt;,
      		&lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt;,
      		&lt;span class=&quot;syntax-symbol&quot;&gt;limit&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;w&lt;/span&gt;,
      		&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_workspace_free&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;

  &lt;span class=&quot;syntax-special&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;normalization&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;upto&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;10&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a_subn&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;upto&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;b_subn&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;upto&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;period&lt;/span&gt;;

  &lt;span class=&quot;syntax-special&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;i&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;; &lt;span class=&quot;syntax-symbol&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt; upto; i++) {
    a_subn[i] = get_aorb_subn(f, i, low, high, true);
    b_subn[i] = get_aorb_subn(f, i, low, high, false);
    printf(&amp;quot;%d %f %f\n&amp;quot;, i, a_subn[i], b_subn[i]);
  }
}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;Results:
&lt;code&gt;| n | a_subn |    b_subn |
|---+--------+-----------|
| 0 |    2.0 |       0.0 |
| 1 | -7e-06 | -1.273242 |
| 2 | -7e-06 | -0.636621 |
| 3 | -7e-06 | -0.424414 |
| 4 | -7e-06 |  -0.31831 |
| 5 | -7e-06 | -0.254648 |
| 6 | -7e-06 | -0.212207 |
| 7 | -7e-06 | -0.181892 |
| 8 | -7e-06 | -0.159155 |
| 9 | -7e-06 | -0.141471 |&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Like I mentioned before, &lt;code&gt;aorb_subn&lt;/code&gt; is a &amp;quot;parent function&amp;quot; that
combines &lt;code&gt;f(x)&lt;/code&gt; and the sine/cosine term. This is the function we
integrate, and we provide it enough information to know what term we
are calculating.
&lt;/p&gt;&lt;p&gt;Interestingly, if we use the &lt;math display=&quot;inline&quot; alttext=&quot;\pi&quot;&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;/math&gt; constant &lt;code&gt;M_PI&lt;/code&gt;, the integration
fails due to roundoff error. Fortunately we can get &amp;quot;close enough&amp;quot;
values by just using a few less digits.&lt;/p&gt;&lt;p&gt;Once we graph the Fourier series generated by these coefficients, this
is what we get.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;/static/images/graphs/gnuplot-asubn-bsubn.png&quot; alt=&quot;A graph showing an approximation of a sawtooth function generated using a Fourier series.
The approximation is periodic.&quot; /&gt;&lt;/p&gt;&lt;p&gt;And there we are! Here is a Fourier series generated for a discontinuous
periodic function. The more terms we add, the more accurate the approximation.&lt;/p&gt;</content></entry><entry><title>Fourier Part 1: What is a Fourier series?</title><id>https://freakingpenguin.com/blog/fourier-part-1-what-is-a-fourier-series.html</id><author><name>Richard Sent</name><email>richard@freakingpenguin.com</email></author><updated>2021-04-06T00:00:00Z</updated><link href="https://freakingpenguin.com/blog/fourier-part-1-what-is-a-fourier-series.html" rel="alternate" /><content type="html">&lt;h1&gt;An overview of Taylor series&lt;/h1&gt;&lt;p&gt;A fourier expansion is a way for us to approximate a function. If
you've taken calculus before, you may have heard of a similar concept,
Taylor series. With Taylor series, we can approximate any function as
a sum of polynomials. For example, we can write &lt;math display=&quot;inline&quot; alttext=&quot;\sin x&quot;&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁡&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt; as
&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;\sin x=x-\frac{x^{3}}{3!}+\frac{x^{5}}{5!}-\frac{x^{7}}{7!}&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁡&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mfrac&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mo&gt;!&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mfrac&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;mo&gt;!&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mfrac&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;7&lt;/mn&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mn&gt;7&lt;/mn&gt;&lt;mo&gt;!&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;p&gt;If we were to graph this, we see that as more terms are
added, our approximation becomes more and more accurate. Here's a
small demonstration.
&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;/static/images/graphs/gnuplot-taylor-example.png&quot; alt=&quot;A graph showing sin(x) and two Taylor series approximations with 3 and 5 terms.&quot; /&gt;&lt;/p&gt;&lt;p&gt;In the graph below, I tried demonstrating how the Taylor series can
&amp;quot;home in&amp;quot; on a function. Since we can't add part of a term in a Taylor
series, I tried to demonstrate this effect by multiplying the term by
a value. For example, when you see 1.1 terms, that means the first
term + 0.1 * the second term. For &lt;math display=&quot;inline&quot; alttext=&quot;\sin x&quot;&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁡&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;, that's &lt;math display=&quot;inline&quot; alttext=&quot;\sin x=x-0.1*\frac{x^{3}}{3!}&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁡&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mn&gt;0.1&lt;/mn&gt;&lt;mo rspace=&quot;0.222em&quot; lspace=&quot;0.222em&quot;&gt;∗&lt;/mo&gt;&lt;mfrac&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mo&gt;!&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;/static/images/graphs/gnuplot-taylor-animated-example.gif&quot; alt=&quot;An animated graph showing how adding terms to the Taylor series improves its accuracy.&quot; /&gt;&lt;/p&gt;&lt;p&gt;Now, Taylor series can be useful, but they have significant
limitations. First, we see that the Taylor series does a poor job
modeling periodic functions. Even though &lt;math display=&quot;inline&quot; alttext=&quot;\sin x&quot;&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁡&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt; repeats, our
Taylor series does not. At large &lt;math display=&quot;inline&quot; alttext=&quot;x&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt; values, this Taylor series is
completely wrong!&lt;/p&gt;&lt;p&gt;Second, a Taylor series relies on the function being continuous (no
holes or jumps). In addition to the function being continous, its
derivatives must be as well. Let's consider the following graph.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;/static/images/graphs/gnuplot-discontinuous-derivative.png&quot; alt=&quot;A graph showing a piecewise function y=-x for x&amp;lt;0 and y=x for x&amp;gt;=0, along with its first derivative.&quot; /&gt;&lt;/p&gt;&lt;p&gt;Even though &lt;math display=&quot;inline&quot; alttext=&quot;f(x)&quot;&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt; is continuous, its derivative &lt;math display=&quot;inline&quot; alttext=&quot;f^{\prime}(x)&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt; is
not. As such, a Taylor series cannot be used to approximate this
function. This issue would come up even if &lt;math display=&quot;inline&quot; alttext=&quot;f^{\prime\prime\prime\prime\prime\prime\prime\prime\prime\prime}(x)&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;′′′′′′′′′′&lt;/mo&gt;&lt;/msup&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt; was
discontinuous. (Keep in mind that discontinuous is not the same thing
as 0! We can model &lt;math display=&quot;inline&quot; alttext=&quot;f(x)=x&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt; as a Taylor series, as its
higher order derivatives are continuous. They just also happen to be
0. Also, &lt;math display=&quot;inline&quot; alttext=&quot;f(x)=x&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt; is its own Taylor series.)&lt;/p&gt;&lt;p&gt;So, to summarize, Taylor series have the following issues:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;They do not model periodic functions well&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;They require the function and all of its derivatives to be continuous&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Fortunately, the Fourier series provides answers to both of these
problems! (At least for most functions. Some functions, like a
function that is discontinuous everywhere, exist solely for the
purpose of making us sad.)&lt;/p&gt;&lt;h1&gt;What we're all here for, Fourier series&lt;/h1&gt;&lt;p&gt;I'll be sticking to the basics of Fourier series for now. Let's assume
we have a periodic function &lt;math display=&quot;inline&quot; alttext=&quot;f(x)&quot;&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt; that has a period of &lt;math display=&quot;inline&quot; alttext=&quot;T&quot;&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/math&gt;. I
am going to fintroduce the symbol l-i where &lt;math display=&quot;inline&quot; alttext=&quot;l=\frac{T}{2}&quot;&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/math&gt;. While this isn't the only option, we can write the Fourier series as&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;f(x)=\sum_{n=0}^{\infty}a_{n}\cos(\frac{n\pi x}{l})\ +b_{n}\sin(\frac{n\pi x}{%
l})&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo rspace=&quot;0.111em&quot;&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∞&lt;/mi&gt;&lt;/munderover&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo stretchy=&quot;false&quot; rspace=&quot;0.500em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;p&gt;In this case, we can find &lt;math display=&quot;inline&quot; alttext=&quot;a_{n}&quot;&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt; and &lt;math display=&quot;inline&quot; alttext=&quot;b_{n}&quot;&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt; with the formulas&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;a_{n}=\frac{1}{l}\int_{0}^{2l}f(x)\cos(\frac{n\pi x}{l})dx&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;b_{n}=\frac{1}{l}\int_{0}^{2l}f(x)\sin(\frac{n\pi x}{l})dx&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;p&gt;I won't go into detail as to where these formulas come from. (That is
left as an exercise for the reader. Hah!) However, I will point out
that we can adjust the limits of integration to any values we want,
just as long as the difference between the upper and lower limits
equals our period.&lt;/p&gt;&lt;p&gt;There is one special case that we need to discuss. When &lt;math display=&quot;inline&quot; alttext=&quot;n=0&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;,
we need a new formula for &lt;math display=&quot;inline&quot; alttext=&quot;a_{0}&quot;&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;. This formula will look like&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;a_{0}=\frac{1}{2l}\int_{0}^{2l}f(x)dx&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;p&gt;Fortunately there is no special case for &lt;math display=&quot;inline&quot; alttext=&quot;b_{0}&quot;&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;. This occurs due to
the fact that &lt;math display=&quot;inline&quot; alttext=&quot;a_{0}&quot;&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt; is a constant term ( &lt;math display=&quot;inline&quot; alttext=&quot;\cos 0=1&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo lspace=&quot;0.167em&quot;&gt;⁡&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt; ) as
opposed to periodic.&lt;/p&gt;&lt;p&gt;That is all the theory that we need to calculate the Fourier series!
As long as we can find a library that can perform integration for us,
we should be able to calculate the Fourier series for any periodic
function.&lt;/p&gt;&lt;h1&gt;Using GSL to calculate an integral&lt;/h1&gt;&lt;h2&gt;Explanation of &lt;code&gt;gsl_integration_qng()&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;Because of inscrutible magic mumbo jumbo, I decided to use C for
calculating the Fourier expansion. In order to do that, I needed to
pick out a library that could perform the integration for me. I
settled on &lt;code&gt;GSL&lt;/code&gt; or the GNU Scientific Library. There are many, many,
MANY functions available in this library, but luckily I only need to
worry about integration.&lt;/p&gt;&lt;p&gt;Before going too far into Fourier stuff, I'm going to do a simple
sanity check so I can make sure I'm calculating integrals correctly. I
want to calculate the following integral.&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;math display=&quot;block&quot; alttext=&quot;\int_{0}^{8}xdx&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;&lt;p&gt;To do this using &lt;code&gt;GSL&lt;/code&gt;, I can use the &lt;code&gt;gsl_integration_qng()&lt;/code&gt; function.
This function has the following signature.&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_qng&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_function&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;,
            &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt;,
            &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;epsabs&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;epsrel&lt;/span&gt;,
            &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;abserr&lt;/span&gt;,
            &lt;span class=&quot;syntax-symbol&quot;&gt;size_t&lt;/span&gt; * &lt;span class=&quot;syntax-symbol&quot;&gt;neval&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;f&lt;/code&gt; is a pointer to a structure that contains a function pointer. For
those who don't speak nerd, this is how &lt;code&gt;gsl-integration-qng()&lt;/code&gt; knows
what function to integrate. It's our &lt;code&gt;f(x)&lt;/code&gt;. (Mostly. The reason for
making it a structure is because the structure also contains a &lt;code&gt;void *&lt;/code&gt; or void pointer. This void pointer can be used to pass parameters
to the function. This could be used to let us change the slope of the
function without having to modify the function itself.&lt;/p&gt;&lt;p&gt;&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are the lower and upper limits of integration.
That's fairly straightforward.&lt;/p&gt;&lt;p&gt;&lt;code&gt;epsabs&lt;/code&gt; and &lt;code&gt;epsrel&lt;/code&gt; help &lt;code&gt;gsl_integration_qng()&lt;/code&gt; decide when to stop
integrating. It's not possible to integrate the function to an exact
value with &lt;code&gt;GSL&lt;/code&gt;. Instead, it tries to zero in on a specific value or
best guess as to what the answer is. &lt;code&gt;epsabs&lt;/code&gt; is the absolute error
that we want. If &lt;code&gt;epsabs&lt;/code&gt; = 0.1, we don't know what the answer is, but
we know we are no more than 0.1 away from it. &lt;code&gt;epsrel&lt;/code&gt; is similar, but
percentage based instead of absolute. (e.g. &lt;code&gt;epsrel&lt;/code&gt; = 0.01 means our
answer is within 1% of the actual value.)&lt;/p&gt;&lt;p&gt;&lt;code&gt;result&lt;/code&gt; and &lt;code&gt;abserr&lt;/code&gt; are used by the function to store the result
and estimated absolute error, respectively. The number of iterations
it took to calculate the result is stored in &lt;code&gt;neval&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;It is possible for the integration to fail. This might happen if we
set the error tolerances too tight. Since the function we're
integrating is so simple, I don't think that's a likely concern.&lt;/p&gt;&lt;h2&gt;&lt;code&gt;gsl_integration_qng()&lt;/code&gt; in use&lt;/h2&gt;&lt;p&gt;To compile this program, you will need to tell the compiler what
external libraries to use. You can do with with the &lt;code&gt;-lgsl&lt;/code&gt; compile
flag, e.g. &lt;code&gt;gcc main.c -lgsl&lt;/code&gt;. Because &lt;code&gt;GSL&lt;/code&gt; depends on another
library, &lt;code&gt;CBLAS&lt;/code&gt;, you will also need the &lt;code&gt;-lcblas&lt;/code&gt; flag. If &lt;code&gt;CBLAS&lt;/code&gt;
isn't available, you can use a version of &lt;code&gt;CBLAS&lt;/code&gt; provided by &lt;code&gt;GSL&lt;/code&gt;
with &lt;code&gt;-lgslcblas&lt;/code&gt;. (Don't forget to install &lt;code&gt;GSL&lt;/code&gt;!) Since I'm using
some math function, I'm also going to include the math library with
&lt;code&gt;-lm&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;In the end, our command will look like &lt;code&gt;gcc main.c -lgsl -lcblas -lm&lt;/code&gt;,
which will compile &lt;code&gt;main.c&lt;/code&gt; and create an output file &lt;code&gt;a.out&lt;/code&gt;.
(Actually I'm using &lt;code&gt;org-babel&lt;/code&gt; so I don't have to deal with this, but
I'm assuming most readers here are not.)&lt;/p&gt;&lt;p&gt;Alright! Time to integrate! I need to preface the &lt;code&gt;gsl_integration.h&lt;/code&gt;
header with &lt;code&gt;gsl/&lt;/code&gt; as the headers are installed in a &lt;code&gt;gsl&lt;/code&gt;
subdirectory. (If you installed this through your systems package
manager, you can probably find this file in &lt;code&gt;/usr/include/gsl/&lt;/code&gt;.)&lt;/p&gt;&lt;div class=&quot;code-block&quot;&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;gsl/gsl_integration.h&amp;gt;&lt;/span&gt;
&lt;span class=&quot;syntax-keyword&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;/* This is where we define the function */&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;, &lt;span class=&quot;syntax-special&quot;&gt;void&lt;/span&gt; *&lt;span class=&quot;syntax-symbol&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;x&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;8&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt; = &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;.&lt;span class=&quot;syntax-symbol&quot;&gt;function&lt;/span&gt; = &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;;
  &lt;span class=&quot;syntax-special&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt; = &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;num_evals&lt;/span&gt;;

  &lt;span class=&quot;syntax-symbol&quot;&gt;gsl_integration_qng&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;F&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;low&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;high&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_abs&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;err_rel&lt;/span&gt;,
      		&amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;, &amp;amp;&lt;span class=&quot;syntax-symbol&quot;&gt;num_evals&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;result error num_evals\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
  &lt;span class=&quot;syntax-symbol&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;%f %f %zu\n&amp;quot;&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;error&lt;/span&gt;, &lt;span class=&quot;syntax-symbol&quot;&gt;num_evals&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;Results:
&lt;code&gt;| result | error | num_evals |
|   32.0 |   0.0 |        21 |&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There we go! &lt;code&gt;GSL&lt;/code&gt; was able to successfully integrate &lt;math display=&quot;inline&quot; alttext=&quot;\int_{0}^{8}xdx&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo&gt;∫&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot;&gt;⁢&lt;/mo&gt;&lt;mrow&gt;&lt;mo rspace=&quot;0em&quot;&gt;𝑑&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;.&lt;/p&gt;&lt;p&gt;In the next part, we'll start using &lt;code&gt;GSL&lt;/code&gt; to calculate the Fourier
series of a discontinuous, periodic function. We'll see if we can
naively get away with using the simple &lt;code&gt;gsl_integration_qng()&lt;/code&gt;
function, or if we need to find a more complicated, but more powerful, alternative. &lt;/p&gt;</content></entry></feed>