Question: The midpoint rule for approximating an integral can be expressed as I swew) x n rra - gotin where h = (b -a). a) Write

 The midpoint rule for approximating an integral can be expressed as

The midpoint rule for approximating an integral can be expressed as I swew) x n rra - gotin where h = (b -a). a) Write a function midpointfor(f, a, b, n) to compute the above approximation using a plain for loop. b) Write a vectorized midpoint function midpointpsum(f, a, b, n) using the built-in sum function. c) Write another vectorized midpoint function midpointnsum(f, a, b, n) using the Numpy sum function. d) Benchmark your functions with the same limits for n = 1000. In [ ]: def midpoint for(f, a, b, n): # YOUR CODE HERE raise NotImplementedError() In [ ]: from nose. tools import assert_equal, assert_almost_equal assert_equal(midpoint for(Lambda x: X**2 - 2* X, 1, 4, 10), 5.9775) In []; import numpy as np def midpointpsum(f, a, b, n): # YOUR CODE HERE raise Not ImplementedError() In ( ): assert_equal (midpointpsum(lambda x: ***2 - 2 * X, 1, 4, 10), 5.9775) In [ ]: def midpointnsum(f, a, b, n): # YOUR CODE HERE raise NotImplementedError() In (): assert_equal (midpointsum(lambda x: ***2 - 2 * X, 1, 4, 10), 5.9775) In [ ]: # YOUR CODE HERE raise Not ImplementedError() In [ ]: # YOUR CODE HERE raise Not ImplementedError() In [ ]: # YOUR CODE HERE raise Not ImplementedError() The midpoint rule for approximating an integral can be expressed as I swew) x n rra - gotin where h = (b -a). a) Write a function midpointfor(f, a, b, n) to compute the above approximation using a plain for loop. b) Write a vectorized midpoint function midpointpsum(f, a, b, n) using the built-in sum function. c) Write another vectorized midpoint function midpointnsum(f, a, b, n) using the Numpy sum function. d) Benchmark your functions with the same limits for n = 1000. In [ ]: def midpoint for(f, a, b, n): # YOUR CODE HERE raise NotImplementedError() In [ ]: from nose. tools import assert_equal, assert_almost_equal assert_equal(midpoint for(Lambda x: X**2 - 2* X, 1, 4, 10), 5.9775) In []; import numpy as np def midpointpsum(f, a, b, n): # YOUR CODE HERE raise Not ImplementedError() In ( ): assert_equal (midpointpsum(lambda x: ***2 - 2 * X, 1, 4, 10), 5.9775) In [ ]: def midpointnsum(f, a, b, n): # YOUR CODE HERE raise NotImplementedError() In (): assert_equal (midpointsum(lambda x: ***2 - 2 * X, 1, 4, 10), 5.9775) In [ ]: # YOUR CODE HERE raise Not ImplementedError() In [ ]: # YOUR CODE HERE raise Not ImplementedError() In [ ]: # YOUR CODE HERE raise Not ImplementedError()

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 Databases Questions!