'numerical double integration in Python using dblquad

I am new to Python and was trying to compare the result of numerical double integration in Python with that of Matlab/Octave. For that I followed this exercise: https://de.mathworks.com/help/matlab/ref/integral2.html

The results which I have got for the integration in cartesian and polar coordinates do match with each other in Octave but not in Python. The results should be actually identical as it is the same integrand. In the integration in cartesian coordinates, I got in Python (0.285398163397451, 1.506509336979889e-08) which matches with that from Octave 0.2854. However I polar coordinates I get in Python somehow a wrong value which is (0.2385105222594962, 2.3128864101717994e-09).
In octave I get again the same result as for the integration in cartesian coordinates. So I am wondering what I might be doing wrong in Python. Hier is my code:

import os
import numpy as np
import math
import cmath
import quadpy
import scipy
from scipy import integrate

fun = lambda x, y: 1/( np.sqrt(x + y) * (1 + x + y)**2 )
ymax = lambda x: 1 - x
q = integrate.dblquad(fun, 0, 1, 0, ymax)

print("f(x, y): The integration result equals = ", q)
# f(x, y): The integration result equals = (0.285398163397451, 1.506509336979889e-08)


polarfun = lambda theta, r: fun(r*np.cos(theta), r*np.sin(theta))*r
# r/( np.sqrt(r*np.cos(theta) + r*np.sin(theta)) * (1 + r*np.cos(theta) + r*np.sin(theta))**2 )
rmax = lambda theta: 1/(np.sin(theta) + np.cos(theta))
q2 = integrate.dblquad(polarfun, 0, np.pi/2, 0, rmax )

print("f(theta, r): The integration result equals = ", q2)
# f(theta, r): The integration result equals = (0.2385105222594962, 2.3128864101717994e-09)

Any help is appreciated. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source