<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-as-learning-tool-solving-integrals/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r-posts.com/r-as-learning-tool-solving-integrals/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img data-orig-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/05/images-1.png?fit=279%2C181" data-orig-size="279,181" data-medium-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/05/images-1.png?fit=279%2C181" data-attachment-id="500" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/05/images-1.png?zoom=1.5&resize=279%2C181" data-permalink="http://r-posts.com/r-as-learning-tool-solving-integrals/images-1/" data-large-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/05/images-1.png?fit=279%2C181" alt="" data-image-title="images (1)" width="279" data-comments-opened="1" class="wp-image-500" height="181"></p>
<p></p>
<p><span>Integrals are so easy only math teachers could make them difficult.</span><span>When I was in high school I really disliked math and, with hindsight, I would say it was just because of the the prehistoric teaching tools (when I saw this</span><span> </span><a rel="nofollow" href="https://youtu.be/dqTTojTija8" target="_blank">video</a><span> </span><span>I thought I’m not alone). I strongly believe that interaction CAUSES learning (I’m using “causes” here on purpose being quite aware of the difference between correlation and causation), practice should come before theory and imagination is not a skill you, as a teacher, could assume in your students. Here follows a short and simple practical explanation of integrals. The only math-thing I will write here is the following:</span><span> </span><em>f(x) = x + 7</em><span>. From now on everything will be coded in R. So, first of all,</span><span> </span><strong>what is a function?</strong><span> </span><span>Instead of using the complex math philosophy let’s just look at it with a programming eye: it is a</span><span> </span><em>tool</em><span> </span><span>that takes something in input and returns something else as output. For example, if we use the previous tool with 2 as an input we get a 9. Easy peasy. Let’s look at the code:</span></p>
<pre># here we create the tool (called &quot;f&quot;)
# it just takes some inputs and add it to 7
f &lt;- function(x){x+7}

# if we apply it to 2 it returns a 9
f(2)
9

</pre>
<p><span>Then the second question comes by itself.</span><span> </span><strong>What is an integral?</strong><span> </span><span>Even simpler, it is just the sum of this tool applied to many inputs in a range. Quite complicated, let’s make it simpler with code: </span></p>
<pre># first we create the range of inputs
# basically x values go from 4 to 6 
# with a very very small step (0.01)
# seq stands for sequence(start, end, step)
</pre>
<pre></pre>
<pre>x &lt;- seq(4, 6, 0.01) 
x
4.00 4.01 4.02 4.03 4.04 4.05 4.06 4.07...

x[1]
4

x[2]
4.01
</pre>
<p><span>As you see,</span><span> </span><em>x</em><span> </span><span>has many values and each of them is indexed so it’s easy to find, e.g. the first element is 4 (x[1]). Now that we have many x values (201) within the interval from 4 to 6, we compute the integral.</span></p>
<pre># since we said that the integral is 
# just a sum, let's call it IntSum and 
# set it to the start value of 0
# in this way it will work as an accumulator
</pre>
<pre>IntSum = 0
</pre>
<p><span>Differently from the theory in which the calculation of the integral produces a new</span><span> </span><em>non-sense</em><span> </span><span>formula (just kidding, but this seems to be what math teachers are supposed to explain), the integral does produce an output, i.e. a number. We find this number by summing the output of</span><span> </span><u>each</u><span> </span><span>input value we get from the</span><span> </span><em>tool</em><span> </span><span>(e.g. 4+7, 4.01+7, 4.02+7, etc) multiplied by the step between one value and the following (e.g. 4.01-4, 4.02-4.01, 4.03-4.02, etc). Let’s clarify this, look down here:</span></p>
<pre># for each value of x 
for(i in 2:201){
    
    # we do a very simple thing:
    # we cumulate with a sum
    # the output value of the function f 
    # multiplied by each steps difference
    
    IntSum = IntSum + f(x[i])*(x[i]-x[i-1])
    
    
    # So for example,  
    # with the first and second x values the numbers will be:
    #0.1101 = 0 + (4.01 + 7)*(4.01 - 4)
    
    # with the second and third:
    #0.2203 = 0.1101 + (4.02 + 7)*(4.02 - 4.01)
    
    # with the third and fourth:
    #0.3306 = 0.2203 + (4.03 + 7)*(4.03 - 4.02)
    
    # and so on... with the sum (integral) growing and growing
    # up until the last value
}

IntSum
24.01
</pre>
<p><span>Done! We have the integral but let’s have a look to the visualization of this because it can be represented and made crystal clear. Let’s add a short line of code to serve the purpose of saving the single number added to the sum each time. The reason why we decide to call it “bin” instead of, for example, “many_sum” will be clear in a moment.</span></p>
<pre># we need to store 201 calculation and we
# simply do what we did for IntSum but 201 times
bin = rep(0, 201)
bin
0 0 0 0 0 0 0 0 0 0 0 0 ...
</pre>
<p><span>Basically, we created a sort of memory to host each of the calculation as you see down here:</span></p>
<pre>for (i in 2:201){
    
    # the sum as earlier
    IntSum = IntSum + f(x[i])*(x[i]-x[i-1])
    
    # overwrite each zero with each number
    bin[i] = f(x[i])*(x[i]-x[i-1])
}

IntSum
24.01

bin
0.0000 0.1101 0.1102 0.1103 0.1104 0.1105 ..

sum(bin)
24.01
</pre>
<p><span>Now if you look at the plot below you get the whole story: each</span><span> </span><em>bin</em><span> </span><span>is a tiny bar with a very small area and is the smallest part of the integral (i.e. the sum of all the bins).</span></p>
<pre># plotting them all
barplot(bin, names.arg=x)
</pre>
<div><img src="https://media.licdn.com/dms/image/C4E12AQFvLm-bK4s6IQ/article-inline_image-shrink_1500_2232/0?e=2125267200&v=beta&t=dhKDlK4lVVy-F-HTPUe-_joQxKPb2m1gcUpdaMzHikM"></div>
<p><span>This tells you a lot about the</span><span> </span><a rel="nofollow" href="https://en.wikipedia.org/wiki/Integral" target="_blank">purpose of integral</a><span> </span><span>and the possibility of calculating areas of curvy surfaces. To have an idea of this just change the function</span><span> </span><em>f<span> </span></em><span>with, let’s say,</span><span> </span><em>sin(x)</em><span> </span><span>or</span><span> </span><em>log(x)</em><span>. What is happening? And what if you increase/decrease the number of bins? Have fun replicating the code changing some numbers and functions. Integrals should be clearer in the end. That’s all folks! #R #rstats #maRche #Rbloggers </span><em>This post is also shared in </em><a rel="nofollow" href="https://www.r-bloggers.com/" target="_blank"><em>www.r-bloggers.com</em></a></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://r-posts.com/r-as-learning-tool-solving-integrals/"> R-posts.com</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fQzd1dOij78" height="1"></a></div>