Polar and Cartesian graphs not matching up

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 2K views
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: 468
  • 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: 512
Last edited:
Physics 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!