Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
174 views
ubuntu2004
Kernel: SageMath 9.3

MAT 151, Spring 2023

Name: Sofia Allali


Lab 1: Approximating areas using rectangles and Riemann sums

In this lab, we are going to explore

  • how to approximate the signed area under the graph of a function using rectangles and Riemann sums

  • whether these approximations are overestimates are underestimates

  • how the approximation changes as you break the interval into more and more pieces

Directions: Follow the prompts below, and respond to the writing prompts with detailed, clearly written explanations, using proper grammar and punctuation. Collaboration with your classmates is encouraged, but you must type your own lab report in your own words. If you need more time, you can continue working on the activity after the lab period is over. But unless you have made arrangements with me ahead of time, I'll expect your work to be in a finished state by 11:59pm, so that I can "collect" the assignment as early as Saturday morning.


Core Code: Run the following code cell to define the core code for this lab.

def RS(Type,Subdivisions,f,a,b): width = (b-a)/Subdivisions estarea = 0 p = plot(f,(x,a,b),rgbcolor=(1,0,0),fill=True,thickness=2,legend_label=f) for i in range(0,Subdivisions): leftside = a+i*width rightside = a+(i+1)*width midpoint = (leftside+rightside)/2 if Type =='Left': p+=line([(leftside,0),(rightside,0),(rightside,f(x=leftside)),(leftside,f(x=leftside)),(leftside,0)]) estarea+= width*f(x=leftside) elif Type == 'Right': p+=line([(leftside,0),(rightside,0),(rightside,f(x=rightside)),(leftside,f(x=rightside)),(leftside,0)]) estarea+= width*f(x=rightside) elif Type == 'Midpoint': p+=line([(leftside,0),(rightside,0),(rightside,f(x=midpoint)),(leftside,f(x=midpoint)),(leftside,0)]) estarea+= width*f(x=midpoint) return [estarea,p] def LRStable(f,a,b,n1,n2): return table([(i,N(RS("Left",i,f,a,b)[0]),N(RS("Right",i,f,a,b)[0]),N(RS("Midpoint",i,f,a,b)[0])) for i in (n1..n2)],header_row = ["$n$", "$L_n$", "$R_n$", "$M_n$"], frame = 'True')

Problem 1

Run the following to create an interactive element to explore Riemann sums.

@interact def _(Type=selector(['Left','Right','Midpoint'],default='Left'), Subdivisions = slider(vmin=1,vmax=50,default=4,label='n'), f = input_box(default="1-x^2/4"), a = input_box(default="0"),b = input_box(default="2")): RSdata = RS(Type,Subdivisions,f,a,b) show(RSdata[1]) print("The estimated signed area is","%0.8f"%RSdata[0])
Interactive function <function _ at 0x7f02bd869430> with 5 widgets Type: Dropdown(description='Type', option…

The default example here is f(x)=1x2/4f(x) = 1-x^2/4 on the interval [0,2][0,2] using n=4n=4 subdivisions. Experiment with all of the various parameters here to get a feel for this: try different nn, but also try moving the endpoints aa and bb and picking a different function as well. In the text cell below, write several sentences describing what this interactive element is doing, and what each of the various parameters ("Type", "n", "f", "a", and "b") controls.

The interactive element is modeling various possibilities of an integral, The "Type" parameter controls the type of the function, with "User-defined" allowing the user to input their own function. The "n" parameter controls the number of trapezoids used in the integration. The "f" parameter allows the user to choose from a list of preset functions, or input their own if "User-defined" is selected for the type. The "a" and "b" parameters control the endpoints of the interval on which the function is being integrated. After selecting parameters and clicking "Update", the simulation displays the graph of the selected function on the selected interval, with the trapezoids plotted underneath. The approximate value of the integral is also calculated and displayed."a" and "b" control where the shaded region begins and ends.


Problem 2

(a) Run the following and examine the four plots.

show(RS("Left",10,x^2,0,2)[1]) show(RS("Right",10,x^2,0,2)[1]) show(RS("Left",10,1/x,1,2)[1]) show(RS("Right",10,1/x,1,2)[1])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-e87fa7cac36f> in <module> ----> 1 show(RS("Left",Integer(10),x**Integer(2),Integer(0),Integer(2))[Integer(1)]) 2 show(RS("Right",Integer(10),x**Integer(2),Integer(0),Integer(2))[Integer(1)]) 3 show(RS("Left",Integer(10),Integer(1)/x,Integer(1),Integer(2))[Integer(1)]) 4 show(RS("Right",Integer(10),Integer(1)/x,Integer(1),Integer(2))[Integer(1)]) NameError: name 'RS' is not defined
Image in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebook

(b) Riemann sums are sometimes underestimates and are sometimes overestimates of the true area. In the text cell below, answer the following questions: What feature or properties of the function do you think determines which it will be? What do left vs right endpoints have to do with this? Try to formulate rules like the following:

  • If the function is [fill in the blank] and we're using left endpoints, then the Riemann sum is an underestimate.

  • If the function is [fill in the blank] and we're using left endpoints, then the Riemann sum is an overestimate.

Formulate several such "rules" (try to come up with at least four), describing conditions that lead to both underestimates and overestimates, depending on whether you're using left endpoints or right endpoints.

If the function is increasing and we're using left endpoints, then the Riemann sum is an underestimate. If the function is decreasing and we're using left endpoints, then the Riemann sum is an overestimate. If the function is increasing and we're using right endpoints, then the Riemann sum is an overestimate. If the function is decreasing and we're using right endpoints, then the Riemann sum is an underestimate.

(c) Run the following code cell and examine the graph. Then in the text cell afterward, answer the following question: Do you think it's possible to say whether the Riemann sum is an underestimate or an overestimate in this case? Write at least two or three sentences explaining your thinking.

It is difficult to say whether this is an overestimate or an underestimate. There are some rectangles that include extra area that is not under the graph, but there are also some rectangles that omit area that is under the graph. It is hard to tell whether there is more area omitted, or more extraneous area included. this is because theres not much area being covered.
show(RS("Left",30,3+sin(5*x)+2*cos(3*x),0,10)[1])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-3-a9d33e7d807b> in <module> ----> 1 show(RS("Left",Integer(30),Integer(3)+sin(Integer(5)*x)+Integer(2)*cos(Integer(3)*x),Integer(0),Integer(10))[Integer(1)]) NameError: name 'RS' is not defined


Problem 3

(a) Run the following to explore the area under the graph of y=1+x/2y=1+x/2 between x=2x=2 and x=6x=6.

@interact def _(Type=selector(['Left','Right','Midpoint'],default='Left'), Subdivisions = slider(vmin=1,vmax=100,default=4,label='n'), f = input_box(default="1+x/2"), a = input_box(default="2"),b = input_box(default="6")): RSdata = RS(Type,Subdivisions,f,a,b) show(RSdata[1]) print("The estimated area is","%0.8f"%RSdata[0])
Interactive function <function _ at 0x7f81411caf70> with 5 widgets Type: Dropdown(description='Type', option…

(b) Experiment with the number of subdivisions nn and the Left/Right setting. In the next text cell, answer the following questions: What happens with nn is large? What does this suggest that the true value of the area is?

When n is large, we get a better estimate of the true value of the area which is 12. This is because larger values of n will cause smaller subintervals, leading to a better approximation of the shape of the region under the curve. This suggests that the actual area is 12.

(c) Use some basic geometry to determine the exact area of this region. In the next text cell, explain what you did and what answer you got. How does your answer compare to the approximations you found previously?

The region can be decomposed into a rectangle and a triangle. The rectangle has base 4 and height 2, so its area is 8. The triangle has base 4 and height 2, so its area is 1/2 x 4 x 2 = 4. Adding these, we get a total area of 12.


Problem 4

The previous problems illustrate how Riemann sums approach the true area as the number of subdivisions grows larger and larger. In this problem, we'll see how quickly they approach the true area.

(a) Run the following to make a table of the first hundred left, right, and midpoint Riemann sums for y=1/xy=1/x on the interval [1,2][1,2].

LRStable(1/x,1,2,1,100)

(b) Now run the following to compute the true area under the graph of y=1/xy=1/x between 11 and 22.

integral(1/x,x,1,2).n()
0.693147180559945

(c) Scroll through the table and examine the values. In the next text cell, answer the following questions: Are the approximations underestiamtes or overestimates? Why? (Think about your responses to some previous questions from this lab activity.) Do the approximations appear to approach the true value? Do they do so more quickly or more slowly than you expected? (If you had no expectation either way, that's ok too - just say so.)

The left-hand are overestimates , the left endpoint rectangles include too much area. The right-hand sums are overestimates because this is a decreasing function, so the right endpoint rectangles cover some of the area.

The estimates appear to approach the true value. ANd The midpoint estimates appear to approach the true value the most, because the midpoint rectangles are somewhere

between an overestimate and an underestimate.

(d) Let's try f(x)=sin(x)f(x)=\sin(x) on [0,π][0,\pi] and make a table like the previous example. In the next text cell, explain what you think the true value of the area might be, and why you think that.

The true area may be equal to 2, because all of the estimates seem to be getting close to 2.
LRStable(sin(x),0,pi,1,50)


Summary of Lab

In the last text cell, write a paragraph describing

  • what this lab was about

  • what you learned

  • anything that was confusing or difficult

  • anything that was surprising or interesting

If you collaborated with other students from class, be sure to acknowledge your colleagues here as well.

In this lab I learned how midpoint is better to get a more accurate representation of your area, as well as having more subdivisions makes ur area total more accurate. What was confusing was running the code sometimes, that can be annoying to do but overall interesting because this is the first time ive done anything like this before. I worked on my own during this LAb.