Polar and Cartesian graphs not matching up

Click For Summary

Discussion Overview

The discussion revolves around discrepancies encountered when integrating a function converted from Cartesian to cylindrical coordinates in Python. Participants explore the integration process, code implementation, and the resulting values, comparing them with outputs from WolframAlpha.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • The original function ##z_{xy}## was converted to cylindrical coordinates ##z_{rθ}##, and the integration was performed using Python's SciPy library.
  • The participant reports different integral results based on whether the integrand G is multiplied by r, yielding values of 100.530965 and 31.49992199 respectively.
  • WolframAlpha provided a different result of 39.5559 for the same integration in Cartesian coordinates, leading to confusion about the correctness of the Python implementation.
  • One participant suggests simplifying the function by eliminating unnecessary variables to check for calculation errors.
  • Concerns are raised about the necessity of a while loop in the code, as it does not affect the integration process.
  • Another participant notes that the result of 100.53 is twice the area of the circle with radius 4, which may indicate a potential issue with the integration setup.
  • There is speculation about possible version discrepancies in Python or libraries between different computers affecting the results.
  • Participants discuss the importance of ensuring the same code is run on both computers to rule out "pilot error." The variable i was initially undefined, which could lead to confusion.

Areas of Agreement / Disagreement

Participants express differing results from the integration, indicating a lack of consensus on the correct output. There are multiple competing views regarding the cause of the discrepancies, including potential coding errors, environmental factors, and mathematical assumptions.

Contextual Notes

There are unresolved issues regarding the assumptions made during the conversion from Cartesian to cylindrical coordinates, the handling of the integrand, and the impact of the integration order on the results.

MathewsMD
Messages
430
Reaction score
7
I have an original function ##z_{xy}## that I converted into cylindrical coordinates, now denoted ##z_{rθ}##. I have shown the steps I took to get here in the image file posted named "Work."

Now, I have taken that work and converted it into code to plot in Python. I plotted it in another piece of code not shown here and it looks fine (I can post that here too, if necessary) but am now trying to integrate this but am running into an odd error. Here is the code for the integration:

Code:
import numpy as np
from scipy import integrate

while i < 1: #run once
    i = i + 1
    sigma0 = 4
    r0 = 0
    theta0 = 0
    def G(r,theta):
         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 #NOTE: # before r is also removed in attempt
    R1I = integrate.nquad(G, [[0,4],[0,2*np.pi]])

print R1I

Now, since I am integrating in polar coordinates, which is in drdtheta, I multiplied G by r when running the code to get the appropriate unit of area since the Python integration subroutine does not recognize this (I assume). Thus, I multiplied G by r (i.e. I removed the # at the end of the function in the code) but I also did not multiply G by r to get the result for a separate run as well.

When I did this, the values I got for integration were:

Integral on G*r = 100.530965
Integral on G = 31.49992199

(Just to clarify, I made these values constant to ensure I had a mean on the origin and integrated over a circle of radius 4.)

I also did this integration on WolframAlpha in Cartesian coordinates as shown in the second image attached. As you can see, the value for this integration from WolframAlpha is 39.5559, which does not match either of the integral values I got. I've been looking through this code for a while now and can't seem to figure out what's causing the mistake. Is there a problem in my early substitution when I converted the equation from Cartesian to Cylindrical coordinates? Am I forgetting to change any variables in the integrand?

Also something else I found odd was that if I transpose the indexes for the integration (i.e. :

Code:
R1I = integrate.nquad(G, [[0,4],[0,2*np.pi]])

becomes

Code:
R1I = integrate.nquad(G, [[0,2*np.pi],[0,4]])

I still get the same result. That doesn't seem right...

Any help with regards to where I am going wrong and advice would be greatly appreciated!
 

Attachments

  • Work.jpg
    Work.jpg
    33.9 KB · Views: 457
  • Screen Shot 2015-05-22 at 4.50.52 PM.png
    Screen Shot 2015-05-22 at 4.50.52 PM.png
    1.2 KB · Views: 500
Last edited:
Technology news on Phys.org
hhhmmm...I copied and pasted your code, run it and got 39.55585244...oh, uncommenting r, by the way
 
By the way, for r0=0 and theta0=0, you basically end up just with r**2...try eliminating everything else and see if you get a different result...maybe something is corrupting your calculations.
 
gsal said:
hhhmmm...I copied and pasted your code, run it and got 39.55585244...oh, uncommenting r, by the way

Thank you for checking! Wow, I tried this on a separate computer and got the same answer as you. Yet on my other computer (the original 1 i tried this on), I keep getting 100.530965, which is very odd. I'll try to go through it a bit more carefully but thank you for confirming this!
 
Yeah, I always say "baby steps, baby steps"...as you build your program and see when it is you notice the corruption.

By the way, at this time, your while loop is totally unnecessary as nothing in that block depends on the loop variable " i "...maybe you have future plans for it; for the time being, though, " i " is not even defined going into the loop for it to evaluated against the " 1 " ...

Another by the way, I noticed the result of 100.53 happens to be twice the area of the circle with radius 4.

I don't know if you are evaluating your program in some kind of environment where some values may persist or whether you have a script that you run from the command line with a brand new runtime every time, like : > python myscript.py ...kind of thing.

gsal
 
MathewsMD said:
Yet on my other computer (the original 1 i tried this on), I keep getting 100.530965, which is very odd.
Sounds like a version problem to me. What versions of python, numpy, and scipy do you have on those two computers?

Another possibility is pilot error. I say this because the code you posted in the original post does not run as-is; the variable i is undefined. Are 100% sure that you running the exact same code on the two computers?
 
D H said:
Sounds like a version problem to me. What versions of python, numpy, and scipy do you have on those two computers?

Another possibility is pilot error. I say this because the code you posted in the original post does not run as-is; the variable i is undefined. Are 100% sure that you running the exact same code on the two computers?


Sorry about that. There was some code in between I uncommented and I thought I only pasted the necessary code. My apologies, i is set to 0. I don't have access to the original computer currently, but I will certainly check it asap.
 
gsal said:
Yeah, I always say "baby steps, baby steps"...as you build your program and see when it is you notice the corruption.

By the way, at this time, your while loop is totally unnecessary as nothing in that block depends on the loop variable " i "...maybe you have future plans for it; for the time being, though, " i " is not even defined going into the loop for it to evaluated against the " 1 " ...

Another by the way, I noticed the result of 100.53 happens to be twice the area of the circle with radius 4.

I don't know if you are evaluating your program in some kind of environment where some values may persist or whether you have a script that you run from the command line with a brand new runtime every time, like : > python myscript.py ...kind of thing.

gsal

Yes, the while loop is for a future function. Very interesting observation. I'll certainly take a look at it. Thank you!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
24
Views
4K
Replies
1
Views
1K