Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions