Question: 3 think + code: integrate using rectangles ( 8 pts ) The simplest way to numerically integrate a function is to divide the interval of
thinkcode: integrate using rectangles 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 xi and the height if we estimate from the left edge will be h fxi So the total area under the curve sampled by N rectangles is Area X N i xi xi fxi 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 homejdarlingastrsharedcode to your $PYTHONPATH system variable so Python will look for modules stored in there: : # JupyterHub users only import sys syspath.appendhomejdarlingastrsharedcode 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 fx x where we know the analytic integral is Z x x x dx x x So for integration limits of x x we should get R x dx To compare, lets try to do that integral numerically using 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: : helpre: # set the integration limits x x # set the number of rectangles to use N printAnalytic: printx x # # Please use the appropriate resomething function to integrate # the function using some rectangles The limits of integration # should span from x to x and you should use N rectangles. printNumerical: printre 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: revisualizeRectanglesparabola x x 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. : revisualizeRectanglesgaussian x x N thinkpaircode: integration using the trapezoids pts In this thinkpaircode, please work with your neighbors 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 xi times the average height h fxi fxi the integral using trapezoid sums would be Area X N i xi xi fxi fxi Copy the code for integrateRectangles and visualizeRectangles from the rectangles.py module into code cells in your own notebook copypaste 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 neighbors 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! : printTesting parabola printAnalytic:x x printTrapezoids: integrateTrapezoidsparabola x x N visualizeTrapezoidsparabola x x N For fx x 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. : printTesting gaussian visualizeTrapezoidsgaussian x x N
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
