Riemann sums
Area and Riemann sums (lesson 2.5.1)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Antiderivatives
Antiderivatives (lesson 2.5.2)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
The fundamental theorem
The fundamental theorem of calculus (lesson 2.5.3)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
Riemann sums with a choice of rule
Write riemann(f, a, b, n, rule="mid") that returns the Riemann sum of f on [a, b] with n strips. It takes heights at the "left" end, the "right" end or the "mid"point of each strip. Raise ValueError for any other rule.
f accepts NumPy arrays, so you can evaluate all the heights at once. Use as it stands. Then a curve below the axis gives a negative sum, and so does a reversed interval with a > b (lesson 2.5.3).
See the tests
import numpy as np
def test_left_four_strips():
"""Left sum of x² on [0, 1] with 4 strips is 0.21875"""
expect_close(riemann(np.square, 0, 1, 4, "left"), 0.21875, what='riemann(x², 0, 1, 4, "left")')
def test_right_four_strips():
"""Right sum of x² on [0, 1] with 4 strips is 0.46875"""
expect_close(riemann(np.square, 0, 1, 4, "right"), 0.46875, what='riemann(x², 0, 1, 4, "right")')
def test_midpoint_converges():
"""The midpoint sum of x² on [0, 1] with 1000 strips is within 1e-6 of 1/3"""
expect_close(riemann(np.square, 0, 1, 1000), 1 / 3, what="midpoint sum, 1000 strips", abs_tol=1e-6, rel=0)
def test_other_interval():
"""Midpoint sum of eˣ on [0, 2] with 2000 strips is close to e² − 1"""
expect_close(riemann(np.exp, 0, 2, 2000), np.e**2 - 1, what="midpoint sum of eˣ on [0, 2]", rel=1e-6)
def test_bad_rule():
"""An unknown rule raises ValueError"""
try:
riemann(np.square, 0, 1, 10, "top")
except ValueError:
return
raise AssertionError('expected ValueError for rule="top"')
def test_below_axis():
"""A curve below the axis gives a negative sum: x − 1 on [0, 1] gives −0.5"""
expect_close(riemann(lambda x: x - 1, 0, 1, 10), -0.5, what="midpoint sum of x − 1 on [0, 1]")
def test_reversed_interval():
"""Running from 1 back to 0 flips the sign: x² gives about −1/3"""
expect_close(riemann(np.square, 1, 0, 1000), -1 / 3, what="riemann(x², 1, 0, 1000)", abs_tol=1e-6, rel=0)