Inconsistent values when integrating [Python]

In summary, the conversation discusses a 2D Gaussian function converted into polar coordinates and the integration of this function over certain regions. The code used to perform the integration is also provided. The values for the characteristics of the functions (including sigma_0) are manually specified, and there seems to be a slight discrepancy between the values obtained from the code and those from WolframAlpha. The conversation also mentions attempts to verify the steps in the conversion process and the use of specific input values to test the functions. Assistance or advice on identifying any errors in the integration process is requested.
  • #1
MathewsMD
433
7
I have a 2D Gaussian:

## f(x,y) = e^{-[(x-x_o)^2 + (y-y_o)^2]/(2*{sigma}^2)}##

which I converted into polar coordinates and got:

## g(r,θ) = e^{-[r^2 + r_o^2 - 2*r*r_o(cos(θ)cos(θ_o) + sin(θ)sin(θ_o))]/({2*{sigma}^2})} ##

The proof for how this was done is in the attached file, and it would be great if someone could verify my steps in case I messed up somewhere (although I have inputted values and it seems to work). I have plotted the functions, and they do seem comparable by visual inspection, too.

The characteristics of the functions (including ## sigma_o##) are manually specified by me, so the values for ##x_o##, ##y_o## match up with ##r_o## and ##θ_o##. Now, when I integrate these functions over the same regions, I don't always get the same answers. For example, when ##r = 0## or ##θ = 0##, I do get the same answers, but when ##θ \neq 0##, then the answers are slightly off. I have been trying to search for where I'm going wrong, and it may be completely obvious (likely associated with the sine and cosine terms), but I'm just not seeing it. The fact that the values from completing the integration on Python (in polar coordinates) and WolframAlpha (in cartesian coordinates) are relatively similar for the cases where ##θ \neq 0## seems a little odd to me. If anyone has any thoughts, it would be greatly appreciated!

Here is my code:

Code:
import numpy as np
from scipy import integrate

RIT = [] # Region Intensity (integration)
i = 0
N = 1
while i < N: #1 random beam generated
    i = i + 1
    sigma0 = 5 # just an example--arbitrary, I usually set it between 1 and 10
    r0 = #you can input any value here, I usually set it between 0 and 10
    theta0 = random.uniform(0,np.pi*2) #you can make it arbitrary instead of random if you wish
    def G(r,theta): #this is the Gaussian in polar coordinates
        return (np.e**(-((r**2 + r0**2 \
        - 2*r*r0*(np.cos(theta)*np.cos(theta0) + \
        np.sin(theta)*np.sin(theta0)))/(2*sigma0**2))))*r
    #this r is here because
    #the integrand includes dr and dtheta, NOT dx and dy any longer!
    RI = integrate.nquad(G, [[4,7],[0,0.5*np.pi]]) #this region is between r = 4 and 7 in the first quadrant
    RIT.append(RI)

print RIT

I've also been trying points in both f and g to see if they correspond, and they seem to work. For example, if:

##sigma = 1##
##x_o = 1##
##y_o = 2##

then ## f(1,1) = e^{-0.5} ##

This also corresponds to:

##r_o = \sqrt{26} ##
##θ_o = ~1.107##
then ##g(\sqrt{2},pi/4) = e^{-0.5}##

I am leaning towards there being an error in my actual integration, but do you see any?

If I use these values for the code:
##sigma0 = 5##
##r0 = 2**0.5##
##theta0 = 1.75*np.pi##

Then the value for the integral I get is: 1.33556147e+01
When I do this in WolframAlpha, I get: 12.7035

Any advice is welcome!
 

Attachments

  • Work.jpg
    Work.jpg
    33.9 KB · Views: 408
Last edited:
Technology news on Phys.org
  • #2
MathewsMD said:
I have a 2D Gaussian:

## f(x,y) = e^{-[(x-x_o)^2 + (y-y_o)^2]/(2*{sigma}^2)}##

which I converted into polar coordinates and got:

## g(r,θ) = e^{-[r^2 + r_o^2 - 2*r*r_o(cos(θ)cos(θ_o) + sin(θ)sin(θ_o))]/({2*{sigma}^2})} ##

The proof for how this was done is in the attached file, and it would be great if someone could verify my steps in case I messed up somewhere (although I have inputted values and it seems to work). I have plotted the functions, and they do seem comparable by visual inspection, too.

The characteristics of the functions (including ## sigma_o##) are manually specified by me, so the values for ##x_o##, ##y_o## match up with ##r_o## and ##θ_o##. Now, when I integrate these functions over the same regions, I don't always get the same answers. For example, when ##r = 0## or ##θ = 0##, I do get the same answers, but when ##θ \neq 0##, then the answers are slightly off. I have been trying to search for where I'm going wrong, and it may be completely obvious (likely associated with the sine and cosine terms), but I'm just not seeing it. The fact that the values from completing the integration on Python (in polar coordinates) and WolframAlpha (in cartesian coordinates) are relatively similar for the cases where ##θ \neq 0## seems a little odd to me. If anyone has any thoughts, it would be greatly appreciated!

Here is my code:

Code:
import numpy as np
from scipy import integrate

RIT = [] # Region Intensity (integration)
i = 0
N = 1
while i < N: #1 random beam generated
    i = i + 1
    sigma0 = 5 # just an example--arbitrary, I usually set it between 1 and 10
    r0 = #you can input any value here, I usually set it between 0 and 10
    theta0 = random.uniform(0,np.pi*2) #you can make it arbitrary instead of random if you wish
    def G(r,theta): #this is the Gaussian in polar coordinates
        return (np.e**(-((r**2 + r0**2 \
        - 2*r*r0*(np.cos(theta)*np.cos(theta0) + \
        np.sin(theta)*np.sin(theta0)))/(2*sigma0**2))))*r
    #this r is here because
    #the integrand includes dr and dtheta, NOT dx and dy any longer!
    RI = integrate.nquad(G, [[4,7],[0,0.5*np.pi]]) #this region is between r = 4 and 7 in the first quadrant
    RIT.append(RI)

print RIT

I've also been trying points in both f and g to see if they correspond, and they seem to work. For example, if:

##sigma = 1##
##x_o = 1##
##y_o = 2##

then ## f(1,1) = e^{-0.5} ##

This also corresponds to:

##r_o = \sqrt{26} ##
##θ_o = ~1.107##
then ##g(\sqrt{2},pi/4) = e^{-0.5}##

then ## g(\sqrt{2}, \frac{pi/4}) = e^{-0.5}##

I am leaning towards there being an error in my actual integration, but do you see any?

If I use these values for the code:
##sigma0 = 5##
##r0 = 2**0.5##
##theta0 = 1.75*np.pi##

Then the value for the integral I get is: 1.33556147e+01
When I do this in WolframAlpha, I get: 12.7035

Any advice is welcome!

Here is the WolframAlpha calculation in cartesian coordinates that should correspond to my polar coordinate integration in Python.
 

Attachments

  • Screen Shot 2015-06-15 at 2.32.53 PM.png
    Screen Shot 2015-06-15 at 2.32.53 PM.png
    1.6 KB · Views: 469
  • #3
I am not understanding why you have an imaginary part of your cartesian integral. You probably need to break that integral into two parts.
for x = 0 to 4 and for x=4 to 7.
 
  • Like
Likes DEvens and MathewsMD
  • #4
RUber said:
I am not understanding why you have an imaginary part of your cartesian integral. You probably need to break that integral into two parts.
for x = 0 to 4 and for x=4 to 7.

You're right. Let me check that out. Complete oversight by me haha. Thank you!
 
  • #5
It's a little difficult to understand what exactly you are integrating. It seems like you are integrating from ##r## 4 to 7, and ##\theta## from 0 to pi/2. So this is some chunk of a 2-D Gaussian. Without checking if you have done that correctly, you are worried that when you change ##r_0## and ##\theta_0## that you get slightly different values.

Well, yes. You are integrating a different part of the Gaussian when you do that. Or so it seems.

Also, I want to pile on what RUber asked. What is the integral you fed to Wolfram? What's with the square roots in the integrand?
 
  • #6
RUber said:
I am not understanding why you have an imaginary part of your cartesian integral. You probably need to break that integral into two parts.
for x = 0 to 4 and for x=4 to 7.

And it turns out my code is correct, I just messed up my integral! Sweet! Thank you for catching my mistake!
 
  • Like
Likes RUber

Related to Inconsistent values when integrating [Python]

1. What does it mean when I encounter inconsistent values when integrating Python?

Inconsistent values when integrating Python means that there are discrepancies or discrepancies in the data or results when using Python in a larger system or project. This can happen due to errors in the code, conflicting libraries or packages, or differences in data formatting.

2. How can I troubleshoot inconsistent values when integrating Python?

To troubleshoot inconsistent values when integrating Python, you can start by checking for any errors or bugs in your code. You can also try updating or removing any conflicting libraries or packages. Additionally, carefully reviewing the data formatting and making sure it is consistent can also help resolve the issue.

3. Can inconsistent values affect the accuracy of my project or analysis?

Yes, inconsistent values can significantly impact the accuracy and reliability of your project or analysis. If the data is not consistent, it can lead to incorrect conclusions or results. Therefore, it is essential to identify and resolve any inconsistent values when working with Python.

4. How can I prevent encountering inconsistent values when integrating Python?

To prevent encountering inconsistent values when integrating Python, it is crucial to have a good understanding of the code and data you are working with. It is also essential to follow coding standards and best practices, such as using descriptive variable names and commenting your code. Regularly checking for updates and conflicts with libraries or packages can also help prevent inconsistent values.

5. Are there any tools or resources that can help with identifying and resolving inconsistent values in Python?

Yes, there are various tools and resources that can assist with identifying and resolving inconsistent values in Python. Debugging tools like the Python debugger (pdb) can help pinpoint errors in your code. Online forums and communities, such as Stack Overflow, can also provide helpful insights and solutions from experienced developers. Additionally, documentation and tutorials on Python integration can also offer guidance on troubleshooting inconsistent values.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
943
Back
Top