Is My 2D Gaussian Quadrature Algorithm Accurate?

In summary: As far as I understand it, I'm using the correct weights and points. After that, it's a fairly straightforward algorithm.
  • #1
Shackleford
1,656
2
Homework Statement
Approximate the following integral using two-point Gaussian Quadrature for double integrals.
Relevant Equations
## I = \sum_{i=0}^{n}\sum_{j=0}^{n} w_i w_j f(x_i, y_j)
##
## \int_{-1}^{1} \int_{-1}^{1} e^{-(x^2 + y^2)} cos(2π (x^2 + y^2)\,dx\,dy ##

## I = \int_{-1}^{1} \int_{-1}^{1}f(x,y) \,dx\,dy \approx \sum_{i=0}^{n}\sum_{j=0}^{n} w_i w_j f(x_i, y_j) ##

## = w_0 w_0 f(x_0, y_0) + w_0 w_1 f(x_0, y_1) + w_1 w_0 f(x_1, y_0) + w_1 w_1 f(x_1, y_1) ##

## w_0 = 1, w_1 = 1 ##
## x_0 = 1/\sqrt 3, x_1 = -1/\sqrt 3, y_0 = 1/\sqrt 3, y_1 = 1/\sqrt 3 ##

Of course, the grid points are

## (1/\sqrt 3, 1/\sqrt 3), (1/\sqrt 3, -1/\sqrt 3), (-1/\sqrt 3, 1/\sqrt 3), (-1/\sqrt 3, -1/\sqrt 3). ##

Wolfram's answer is 0.111328. My crude code in Python is -1.02683...

def GaussQuad2D(x_point_dict, y_point_dict, weight_dict, num_points):
summation = []
for e in np.arange(num_points):
w = weight_dict[e]
x = x_point_dict[e]
for j in np.arange(num_points):
y = y_point_dict[j]
f = w * np.exp(-(x**2 + y**2)) * np.cos(2*np.pi*(x**2 + y**2))
summation.append(f)
return sum(summation)

x_point_dict = {0:(-1/np.sqrt(3)), 1:(1/np.sqrt(3))}
y_point_dict = {0:(-1/np.sqrt(3)), 1:(1/np.sqrt(3))}
weight_dict = {0:(1), 1:(1)}
answer = GaussQuad2D(x_point_dict, y_point_dict, weight_dict, 2)

There's a nested loop in there.
Screen Shot 2019-09-20 at 10.11.31 PM.png
 
Physics news on Phys.org
  • #2
If I understand your formulas, here is what I got with Maple:
maplesnip.JPG

As you can see, it agrees with your Python. Much simpler in Maple though.
 
  • #3
LCKurtz said:
If I understand your formulas, here is what I got with Maple:
View attachment 249926
As you can see, it agrees with your Python. Much simpler in Maple though.

Thanks for the reply. I've included a screenshot of the problem so that there's no ambiguity. The analytical/graphical approach certainly has its advantages.

prob.png
 
  • #4
Just so you know, I didn't look up the theory of 2D Gaussian quadrature, so I am just assuming you know your formula is correct. What I mean is about your choices for the points and the weights. So, if your setup happens to be wrong, so is mine.
 
  • #5
LCKurtz said:
Just so you know, I didn't look up the theory of 2D Gaussian quadrature, so I am just assuming you know your formula is correct. What I mean is about your choices for the points and the weights. So, if your setup happens to be wrong, so is mine.

As far as I understand it, I'm using the correct weights and points. After that, it's a fairly straightforward algorithm. Someone with a bit of expertise might notice a subtle detail that I'm overlooking.
 

1. What is Gaussian Quadrature 2D?

Gaussian Quadrature 2D is a numerical integration method used to approximate the value of a double integral over a specified region in two-dimensional space. It is based on the use of Gaussian quadrature weights and nodes to accurately calculate the integral.

2. How does Gaussian Quadrature 2D differ from other numerical integration methods?

Gaussian Quadrature 2D is different from other numerical integration methods in that it uses a specific set of points and weights that are optimized for the particular region being integrated over. This results in a more accurate approximation compared to other methods that use evenly spaced points.

3. What are the advantages of using Gaussian Quadrature 2D?

One of the main advantages of Gaussian Quadrature 2D is its high accuracy, especially for integrals over complicated regions. It also requires fewer function evaluations compared to other methods, making it more efficient.

4. Are there any limitations to using Gaussian Quadrature 2D?

While Gaussian Quadrature 2D is a powerful integration method, it does have some limitations. It can only be used for double integrals and is not suitable for integrals over infinite regions. It also requires knowledge of the specific region being integrated over, which can be challenging to determine in some cases.

5. How is the accuracy of Gaussian Quadrature 2D determined?

The accuracy of Gaussian Quadrature 2D is determined by the number of points and weights used, as well as the choice of quadrature rule. Generally, using more points and a higher-order quadrature rule will result in a more accurate approximation.

Similar threads

  • Calculus and Beyond Homework Help
Replies
2
Views
501
  • Calculus and Beyond Homework Help
Replies
4
Views
681
  • Calculus and Beyond Homework Help
Replies
10
Views
418
  • Calculus and Beyond Homework Help
Replies
21
Views
826
  • Calculus and Beyond Homework Help
Replies
6
Views
839
  • Calculus and Beyond Homework Help
Replies
2
Views
531
  • Calculus and Beyond Homework Help
Replies
9
Views
535
  • Calculus and Beyond Homework Help
Replies
5
Views
754
  • Calculus and Beyond Homework Help
Replies
5
Views
269
Replies
9
Views
1K
Back
Top