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. #Define your function 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() #add a line to your plot on the x-axis 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 = #add lines to your plot to represent the current box on your figure lines(c(left,left,right,right),c(0,height,height,0),col="purple") } #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("Approximate Area =", as.character(area)) title(main = text) How would you change this code to calculate the left-hand Riemann sum? Using your script Test your code for the function 5.08. from 0 to 2 with 10 boxes. You should get an answer of Integrate the function by hand and see what the actual integral is. See what happens as you increase the number of boxes you use to calculate the Riemann sum. Compare these results with the answer you computed by hand. Use your code to approximate the integral of from 0 to 10. Compare this to the integral which you compute by hand. Again see what happens as you increase the number of boxes. What happens if the curve is below the x-axis? To explore this look at the same function from -10 to 10 and also calculate this definite integral by hand.