Numerical computation of double integral

Click For Summary

Discussion Overview

The discussion revolves around the numerical computation of a double integral using the trapezium rule in MATLAB, specifically focusing on the inverse Fourier transform of a 2D Gaussian function. Participants explore the implementation details, results obtained, and potential issues with the algorithm and function definitions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to computing a double integral using the trapezium rule and presents their MATLAB code.
  • Another participant suggests there may be typographical errors in the formula, specifically regarding a missing factor and sign changes, which could affect the results.
  • A participant shares their code for the trapezium rule and expresses concern about the scaling of the results when computing the inverse Fourier transform.
  • One participant identifies that the algorithm was not the issue but rather the definition of the 2D Gaussian function, indicating a potential problem with scaling.
  • Another participant questions the normalization of the Gaussian function and suggests that a constant factor may be missing in the inverse transform calculation.
  • A participant asserts that their definition of the 2D Gaussian function is correct and that their constants are not off, referencing the expected Fourier transform results.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the algorithm, the definition of the Gaussian function, and the constants used in the calculations. There is no consensus on the source of the issues affecting the results.

Contextual Notes

Participants note potential limitations in the implementation, including assumptions about the function definitions and the need for careful attention to constants in the Fourier transform calculations.

hunt_mat
Homework Helper
Messages
1,816
Reaction score
33
Hi,

I wrote a piece of MATLAB code to compute a double integral of the form:
[tex] \int_{a}^{b}\int_{c}^{d}f(x,y)dxdy[/tex]
I went about it using the trapezium rule, so what I did was apply the rule to the [itex]x[/itex] variable first to obtain:
[tex] \int_{a}^{b}\int_{c}^{d}f(x,y)dxdy\approx\frac{\delta x}{2}\int_{c}^{d}2\sum_{i=1}^{N}f(x_{i},y)-f(a,y)-f(b,y)dy[/tex]
I then went on to apply the trapezium rule in the [itex]y[/itex] variable to obtain:
[tex] \begin{array}{rcl}<br /> \int_{a}^{b}\int_{c}^{d}f(x,y)dxdy & = & \left(\sum_{i=1}^{N}\sum_{j=1}^{M}f(x_{i},y_{j})-\frac{1}{2}\sum_{i=1}^{N}f(x_{i},c)-\frac{1}{2}\sum_{i=1}^{N}f(x_{i},d)\right)\delta x\delta y \\<br /> & - & \frac{1}{2}\left(\sum_{j=1}^{M}f(a,y_{j})-\sum_{j=1}^{M}f(b,y_{j})\right)\delta x\delta y \\<br /> & - & \frac{1}{2}(f(a,c)+f(b,c)+f(a,d)+f(b,d))\delta x\delta y<br /> \end{array}[/tex]
I tried using this to compute the inverse Fourier transform of the 2d Gaussian and I got some rather bad results.
I include the results. Not what I expected.
 
Last edited:
Physics news on Phys.org
The picture of the results, On one exis it seems to work whereas on the other it doesn't.

Thoughts?
 

Attachments

  • gauss_2d.png
    gauss_2d.png
    8.2 KB · Views: 561
There seems to be missing a factor (1/2) before sum f(xi,d).
And the minus sign before f(b,yj) should probably be a plus sign.
Should I assume those are just typos?

Beyond that, your formula is symmetrical in x and y.
So if it yields an asymmetric result, you probably made a typing mistake.
Perhaps typing an i somewhere where there should be a j, or something like that.
Or perhaps one of the typos I already mentioned.
 
There is the code I used:
function y=trap_2d(A,dx,dy)
N=length(A(:,1));
M=length(A(1,:));

a=A(1,1)+A(1,M)+A(N,1)+A(N,M);
b=sum(A(1,:))+sum(A(N,:));
c=sum(A(:,1))+sum(A(:,M));
u=zeros(1,N);
for i=1:N
u(i)=sum(A(i,:));
end

d=sum(u);

y=(d-0.5*c-0.5*b-0.25*a)*dx*dy;

I first input the integrand as a matrix and then just do the sums in the equation as I said. Another thing that I am worried about is that as I am taking the4 inverse Fourier transform of the Gaussian, it shouldn't be coming out to that high a number.
 
It turned out that it wasn't the algorithm for the 2d trapezium rule that was the problem, but the definition of the function itself. I am still off with the scaling though.
 

Attachments

  • gauss_2d.jpg
    gauss_2d.jpg
    9.7 KB · Views: 526
Sorry, but I've got no clue how to give a meaningful response to your last 2 replies.
 
The integral algorithm wasn't the problem as it turns out, but another problem with the definition, I defined the 2D Gauss function as
[tex] \pi e^{(k^{2}+l^{2})/4}[/tex]
The algorithm I used to compute the inverse Fourier transform is:
const=1/(4*pi^2);
%Now compute the inverse tranform.

for i=1:n
for j=1:m
A=gauss_2d(K,L).*exp(sqrt(-1)*(K*x(i)+L*y(j)));
eta(i,j)=const*real(trap_2d(A,dx,dy));
end
end

I think that there is a problem here.
 
It appears to be fine, I am taking the 2d Fourier transform of:
[tex] f(x,y)=e^{-(x^{2}+y^{2})}[/tex]
This has a Fourier transform of:
[tex] \hat{f}(k,l)=\pi e^{-(k^{2}+l^{2})/4}[/tex]
So I don't think my constants off.
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K