Lambda function doesn't take arguments in the order of their positions

Viewed 58

I want to integrate a * b**2 with the limits a = [0, 2] and b = [0, 1].

from scipy import integrate

f1 = lambda a, b: a * b ** 2      
print((integrate.dblquad(f1, 0, 2, 0, 1))[0])

Output: 1.333...

Expected result: 0.666...

What's wrong?: The integration is being done with the limits a = [0, 1] and b = [0, 2].

Question: Why is lambda not reading the arguments in the order of their position?

1 Answers

From the documentation (https://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html) The order is to first give the range of the last measure in the integral then the first. That means:

from scipy.integrate import dblquad

print(dblquad(lambda a, b: a * b ** 2, 0, 2, lambda b: 0, lambda b: 1)[0])
print(dblquad(lambda a, b: a * b ** 2, 0, 1, lambda a: 0, lambda a: 2)[0])

Evaluates as:

1.3333333333333335
0.6666666666666667

So, you need to flip the order to obtain the desired result.

Related