Challenge - 5 Problems
Triple Integral Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of a triple integral using tplquad
What is the output of this code that calculates the triple integral of f(x,y,z) = x + y + z over the cube where x, y, z range from 0 to 1?
SciPy
from scipy.integrate import tplquad func = lambda z, y, x: x + y + z result, error = tplquad(func, 0, 1, lambda x: 0, lambda x: 1, lambda x, y: 0, lambda x, y: 1) print(round(result, 3))
Attempts:
2 left
š” Hint
Remember the integral of x, y, or z from 0 to 1 is 0.5 each, and the integral sums over all three variables.
ā Incorrect
The integral of x from 0 to 1 is 0.5, same for y and z. Summing these gives 1.5 as the total integral over the unit cube.
ā data_output
intermediateNumber of items in the result tuple from tplquad
After running tplquad, how many items are in the returned result tuple?
SciPy
from scipy.integrate import tplquad func = lambda z, y, x: x*y*z result = tplquad(func, 0, 1, lambda x: 0, lambda x: 1, lambda x, y: 0, lambda x, y: 1) print(len(result))
Attempts:
2 left
š” Hint
tplquad returns the integral value and an estimate of the error.
ā Incorrect
tplquad returns a tuple with two items: the integral result and the estimated error.
š§ Debug
advancedIdentify the error in this tplquad usage
What error does this code raise when trying to compute the triple integral of f(x,y,z) = x*y*z with incorrect bounds?
SciPy
from scipy.integrate import tplquad func = lambda z, y, x: x*y*z result = tplquad(func, 0, 1, lambda x: 1, lambda x: 0, lambda x, y: 0, lambda x, y: 1) print(result[0])
Attempts:
2 left
š” Hint
Check the order of the lower and upper bounds in the lambda functions.
ā Incorrect
The inner integral bounds must be in ascending order. Here, the lower bound lambda returns 1 and upper bound returns 0, causing a ValueError.
š§ Conceptual
advancedUnderstanding the order of variables in tplquad
In the function passed to tplquad, what is the order of variables for the integrand function?
Attempts:
2 left
š” Hint
Check the scipy documentation for tplquad variable order in the integrand.
ā Incorrect
The integrand function for tplquad receives variables in the order z, y, x, where x is the outermost variable.
š Application
expertCalculate the volume of a tetrahedron using tplquad
Use tplquad to calculate the volume of the tetrahedron bounded by x, y, z ℠0 and x + y + z ⤠1. What is the integral result?
SciPy
from scipy.integrate import tplquad func = lambda z, y, x: 1 result, error = tplquad(func, 0, 1, lambda x: 0, lambda x: 1 - x, lambda x, y: 0, lambda x, y: 1 - x - y) print(round(result, 3))
Attempts:
2 left
š” Hint
The volume of this tetrahedron is 1/6, which is approximately 0.167.
ā Incorrect
The volume of the tetrahedron defined by x,y,zā„0 and x+y+zā¤1 is 1/6. The integral of 1 over this region equals this volume.
