# Lab 15 – Integration and Area ```Lab 15 – Integration and Area
Date: December 6, 2011
Assignment Due Date: December 9, 2011
Goal: In today’s lab we will explore finding approximate areas in R using Riemann sums. We will see
what happens as we make the rectangles smaller and smaller in order to approach exact definite
integrals which we will compute by hand.
Riemann Sums
Integrals can be thought of as taking the area under a curve. We can approximate integrals using
Riemann Sums which involves calculating the area with a finite number of boxes. The figure below
shows a right-hand Riemann sum for the function
from x = 0 to x = 2 with 10 boxes.
The width of each of these boxes is .2 or
. Therefore, the width of each box can be determined by
. The area of each box is simply the width of the box multiplied by the height of
the box which is the function value at the right side of each box for the right-hand Riemann sum. When
we add up the area of all the boxes we get an approximate area under the curve which approximates
the integral
.
Using R to compute Riemann sums
Below is an outline for a script in R to calculate a right-hand Riemann sum as explained before. Fill in the
blanks in order to write a script that produces figures like the one seen above and also computes the
approximate area.
f=
#Define N as number of boxes to use to compute the Riemann sum
N=
#Define the lower limit
lower =
#Define the upper limit
upper =
#Define the width of each box
width =
#Define a vector in order to plot going from the lower limit to the upper limit
x=
#plot the function f(x) as a green line
plot()
lines()
#Initialize area so we can add to it in the for loop.
area =
#begin for loop with the index going from 1 to N for each box
for (){
#define the left x value of the current box
left =
#define the right x value of the current box
right =
#define the height of the current box using the function value at the right side
height =
#add the area of the current box to the total area
area =
lines(c(left,left,right,right),c(0,height,height,0),col=&quot;purple&quot;)
}
#Print the area to the console
print(area)
#This code allows you to print the approximate area to the title of your figure.
text = paste(&quot;Approximate Area =&quot;, as.character(area))
title(main = text)
How would you change this code to calculate the left-hand Riemann sum?