from scipy.integrate import dblquad result, error = dblquad(lambda y, x: x + y, 0, 1, lambda x: 0, lambda x: 1) print(round(result, 3))
The integral of f(x, y) = x + y over the unit square is calculated as:
ā«ā¹1 ā«ā¹1 (x + y) dy dx = ā«ā¹1 [x*y + y²/2]ā¹1 dx = ā«ā¹1 (x*1 + 1/2) dx = ā«ā¹1 (x + 0.5) dx = [x²/2 + 0.5x]ā¹1 = 0.5 + 0.5 = 1.0
The code uses the correct order lambda y, x: x + y for dblquad (inner y, outer x), so the result is 1.0.
from scipy.integrate import dblquad result, error = dblquad(lambda y, x: x*y, 0, 2, lambda x: 0, lambda x: 3) print(result)
scipy.integrate.dblquad uses adaptive quadrature methods internally and does not provide a direct count of points used to the user.
The dblquad function integrates first over the inner variable y, then over the outer variable x. So the function must accept y as the first argument and x as the second to match this order.
from scipy.integrate import dblquad result, error = dblquad(lambda x, y: x*y, 0, 1, lambda x: 0, lambda x: 1) print(result)
No error is raised because although the arguments are named x, y, dblquad calls func(y_val, x_val), so it computes y_val * x_val, which equals x * y. The code correctly computes the integral (1/4 = 0.25).
from scipy.integrate import dblquad import math result, error = dblquad(lambda y, x: math.sin(x) * math.cos(y), 0, math.pi, lambda x: 0, lambda x: math.pi/2) print(round(result, 3))
The integral separates as ā«ā^Ļ sin(x) dx * ā«ā^{Ļ/2} cos(y) dy = 2 * 1 = 2. But since dblquad integrates y first, the code integrates cos(y) from 0 to Ļ/2 (which is 1), then sin(x) from 0 to Ļ (which is 2). The product is 2.
However, the code's order of integration is y then x, so the result is 2.0.