Question: 3 think + code: integrate using rectangles ( 8 pts ) The simplest way to numerically integrate a function is to divide the interval of

3 think+code: integrate using rectangles (8 pts) The simplest way to numerically integrate a function is to divide the interval of interest into rectangles. The area of each rectangle is its base, b, times its height, h. For each rectangle, the base will be b = xi+1 xi and the height (if we estimate from the left edge) will be h = f(xi). So, the total area under the curve sampled by N rectangles is Area = X N i (xi+1 xi) f(xi) A Python function to do this is contained in the rectangles.py module stored in this classs shared code directory and on Canvas. If youre running on JupyterHub youll need to temporarilty add this directory (/home/jdarling/astr2600/sharedcode/) to your $PYTHONPATH system variable so Python will look for modules stored in there: 1[]: # JupyterHub users only import sys sys.path.append('/home/jdarling/astr2600/sharedcode/') If you are using your own laptop youll need to copy rectangles.py from Canvas into the directory where you are working on this tutorial or a directory in your $PYTHONPATH (you can also upload the .py file into Jupyter like you did for the exam). We can then import those tools by simply typing: []: import rectangles as re Now, lets test this code out on f(x)= x 2 where we know the analytic integral is Z x2 x1 x 2 dx =13 x 32 x 31 So, for integration limits of x1=0, x2=3, we should get R 30 x 2dx =9. To compare, lets try to do that integral numerically using 5 rectangles. Using the ?? or help(), determine which of the functions inside our re module integrates a function using rectangles. Include the correct function call in the cell below where prompted: []: help(re)[]: # set the integration limits x1, x2=0.0,3.0 # set the number of rectangles to use N =5 print('Analytic:') print((x2**3- x1**3)/3.) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # Please use the appropriate "re.(something?)" function to *integrate* # the function using some *rectangles*. The limits of integration # should span from x1 to x2, and you should use N rectangles. print('Numerical:') print(re.???(???)) Our numerical solution isnt great. To see why, lets visualize it using another function inside that re module, which shows both the smooth function and the rectangles were effectively drawing underneath it.[]: re.visualizeRectangles(parabola, x1, x2, N) The gaps between the black and purples lines represent missing area between what were summing into our numerical integral and the actual integral. The bigger the gaps, the worse our numerical solution is. Lets also visualize this on the Gaussian function too, and well see that, for these integration limits, were likely overestimating the integral. []: re.visualizeRectangles(gaussian, x1, x2, N)4 think+pair+code: integration using the trapezoids (8 pts) In this think+pair+code, please work with your neighbor(s). Youll need to modify a (kind of complicated!) block of code, to make it do something more accurate. Be patient, and look carefully at each step! We can tell by looking at the above plots that we could get much more accurate integrals if we were to use trapezoids matched to our function instead of rectangular boxes. Since the area of a trapezoid is the base width b = xi+1 xi times the average height h =[f(xi+1)+ f(xi)]/2, the integral using trapezoid sums would be Area = X N i (xi+1 xi) f(xi+1)+ f(xi)2 Copy the code for integrateRectangles and visualizeRectangles from the rectangles.py module into code cells in your own notebook (copy+paste is allowed here!) You can access the source code for these functions either by running re?? in the jupyter notebook or by opening the original file in a text editor. Discuss this code with your neighbor(s), and identify the modifications you would need to make to these two functions to turn them into integrateTrapezoids and visualizeTrapezoids, making use of the trapezoid scheme for numerical integration. Make those modifications in your jupyter notebook, and then test your functions with the following code: []: # Create integrateTrapezoids and visualizeTrapezoids here! []: print("Testing", parabola) print('Analytic:',(x2**3- x1**3)/3.) print("Trapezoids:", integrateTrapezoids(parabola, x1, x2, N)) visualizeTrapezoids(parabola, x1, x2, N) For f(x)= x 2, that is quite a bit closer than the rectangular approximation, without much additional calculation. This is because the sloped lines can do a decent job of approximating the curve over each little chunk. []: print("Testing", gaussian) visualizeTrapezoids(gaussian, x1, x2, N)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!