Question: Write a function midpoint(f, a, b, M) taking the arguments 'f' (a function), 'a' (a number), 'b' (a number) and 'M' (a positive integer) that
Write a function midpoint(f, a, b, M) taking the arguments 'f' (a function), 'a' (a number), 'b' (a number) and 'M' (a positive integer) that implements the composite midpoint rule for a generic function f on the closed interval [a,b], using M sub-intervals, and returns the value of the approximated integral.
For example:
| Test | Result |
|---|---|
def f(x): return x I = midpoint(f, 0, 1, 100) print(f'{I:.6f}') | 0.500000 |
import numpy as np def f(x): y = np.sin(x) return y I = midpoint(f, 0, np.pi/2, 10) print(f'{abs(I):.6f}') | 1.001029 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
