Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Polar and Cartesian graphs not matching up

  1. May 22, 2015 #1
    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 (Text):

    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 (Text):

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


    Code (Text):

    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 Im going wrong and advice would be greatly appreciated!

    Attached Files:

    Last edited: May 22, 2015
  2. jcsd
  3. May 22, 2015 #2
    hhhmmm...I copied and pasted your code, run it and got 39.55585244...oh, uncommenting r, by the way
  4. May 22, 2015 #3
    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.
  5. May 23, 2015 #4
    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!
  6. May 23, 2015 #5
    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.

  7. May 23, 2015 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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?
  8. May 23, 2015 #7
    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.
  9. May 23, 2015 #8
    Yes, the while loop is for a future function. Very interesting observation. I'll certainly take a look at it. Thank you!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook