Polar and Cartesian graphs not matching up

In summary: 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.
  • #1
MathewsMD
433
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: 379
  • 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: 397
Last edited:
Technology news on Phys.org
  • #2
hhhmmm...I copied and pasted your code, run it and got 39.55585244...oh, uncommenting r, by the way
 
  • #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.
 
  • #4
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!
 
  • #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.

gsal
 
  • #6
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?
 
  • #7
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.
 
  • #8
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!
 

1. Why do polar and cartesian graphs not match up?

Polar and cartesian graphs use different coordinate systems to represent points in a plane. Cartesian graphs use x and y coordinates, while polar graphs use angles and distances from the origin. This fundamental difference in coordinate systems means that the same points will be represented differently on each type of graph, leading to a mismatch between the two.

2. How do I convert a polar graph to a cartesian graph?

To convert a polar graph to a cartesian graph, you can use the following formulas:

x = r * cos(theta)

y = r * sin(theta)

Where r is the distance from the origin and theta is the angle in radians.

3. Can I plot polar and cartesian points on the same graph?

Yes, you can plot polar and cartesian points on the same graph, but you will need to convert the polar points to cartesian coordinates first using the formulas mentioned above.

4. Why are polar graphs used in some situations instead of cartesian graphs?

Polar graphs are often used in situations where the data has a circular or symmetric pattern. This is because polar coordinates are better suited for representing circular or symmetric shapes, making them a more efficient choice for certain types of data.

5. Are there any real-world applications for polar graphs?

Yes, polar graphs have many real-world applications. They are commonly used in navigation and mapping, as well as in engineering and physics for analyzing rotational and circular motion. They are also used in astronomy for plotting the positions of stars and planets in the sky.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
1
Views
2K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
Replies
13
Views
2K
Back
Top