Distance between 2 points in spherical coordinates

In summary, the conversation discussed the problem of finding the distance between two points in spherical coordinates while programming an algorithm on Matlab. The participants considered using the law of cosines to calculate the angle between the position vectors of the two points, as well as the geodesic and haversine formulas. They also discussed the importance of efficiency in programming and suggested analyzing the design and using both spherical and Cartesian representations for better results.
  • #1
TheDestroyer
402
1
Hello people,

I'm creating an algorithm on Matlab and need to find the distance between two points in spherical coordinates where I have (r1,theta1,phi1) for the first point and (r2,theta2,phi2) for the second point.

Of course, since I'm programming, I shouldn't use the dummy Cartesian coordinates distance rule because it'll consume the processor for some dummy math! I could think that the problem can be reduced to calculating the angle between the position vectors of these two points, then it'll become very simple because we can then use the law of cosines and find the distance between the 2 points with the modified Pythagorean.

Is there a way to calculate that angle? the angle between the two position vectors of the two points.

Any other proposals are welcome :-)

Thank you in advance, for anything, even just for reading :D
 
Physics news on Phys.org
  • #2
TheDestroyer said:
Of course, since I'm programming, I shouldn't use the dummy Cartesian coordinates distance rule because it'll consume the processor for some dummy math! ... Is there a way to calculate that angle? the angle between the two position vectors of the two points.
This looks a bit too much like homework, so I'm not going to give the answer outright.

First, a rant.
When you are programming, efficiency should usually be a low priority concern. In most computer programs, the vast majority of computing time is going to be isolated to a tiny, tiny fraction of the source code. Worry about efficiency for that tiny, tiny fraction. Don't worry about it elsewhere. The reason: Efficiency goes against almost every other software quality concern.
End rant mode.


The reason to avoid going through Cartesian coordinates is because that will add error. There is no way to avoid making several calls to the transcendental functions (sine and cosine). Getting accurate results is much more important than saving a few lines of code.

It is a fairly easy matter to compute the cosine of the angle between the vectors. Compute (on paper, that is) the unit vectors defined by these vectors and use these unit vectors to compute the cosine of the angle between the vectors.
 
  • #3
LoL! homework? I'm doing my master thesis! LOL! HOMEWORK! hahahahahahahahahaha! does it really seem like a homework?

I'm working on the subject called porous media and I'm building samples using stochastic conditions! and I'm using the spherical coordinates for some technical issues!

Please, if you have the answer just say and don't make me wander and waste time I don't have. I tried to search for it on the Internet but they always talk about the "Geodesic" on a sphere which is not what I want :-) I'm not that brilliant in geometry ;-)

Thank you
 
  • #4
And by the way, about the efficiency thing! my program keeps our cluster system busy for few days to give a result, our cluster system is equivalent to a supercomputer! so efficiency is not that negligible in my case, is it? ;)
 
  • #5
TheDestroyer said:
Please, if you have the answer just say and don't make me wander and waste time I don't have. I tried to search for it on the Internet but they always talk about the "Geodesic" on a sphere which is not what I want :-) I'm not that brilliant in geometry ;-)
Actually, the geodesic on a sphere is very close to what you want. The formulae used to compute the distance between two points on a spherical Earth contains the necessary calculations. You want the cosine of the angle between two lines. That geodesic calculation wants the angle itself.

First, some nomenclature. There are multiple ways to represent things in spherical coordinates. I'll use [itex]\phi[/itex] as the angle between the x-y plane and the vector in question ([itex]\phi=\pi/2[/itex] for the +z axis) and [itex]\theta[/itex] as the angle between the x axis and the projection of the vector onto the x-y plane.

A vector from the origin to a point [itex](1,\phi,\theta)[/itex] is a unit vector. In Cartesian coordinates, [itex]\hat u = \cos\phi\cos\theta \hat x + \cos\phi\sin\theta \hat y + \sin\phi \hat z[/itex]. The cosine of the angle [itex]\psi[/itex] between two such unit vectors defined by [itex](1,\phi_1,\theta_1)[/itex] and [itex](1,\phi_2,\theta_2)[/itex] is given by the inner product between these unit vectors:

[tex]\aligned
\cos\psi &= \cos\phi_1\cos\phi_2\cos\theta_1\cos\theta_2 + \cos\phi_1\cos\phi_2\sin\theta_1\sin\theta_2 + \sin\phi_1\sin\phi_2 \\
&= \cos\phi_1\cos\phi_2\cos(\theta_1-\theta_2) + \sin\phi_1\sin\phi_2
\endaligned[/tex]

The law of cosines gives the distance between two points as

[tex]d^2 = {r_1}^2 + {r_2}^2 + 2 r_1 r_2 \cos \psi[/tex]

You can use this, but you can do better. The above does not fare so well when points are nearby. The haversine formula (google that) will help in this regard.

Alternatively, you could just convert to Cartesian and compute the distance.
TheDestroyer said:
And by the way, about the efficiency thing! my program keeps our cluster system busy for few days to give a result, our cluster system is equivalent to a supercomputer! so efficiency is not that negligible in my case, is it? ;)
Well, how often are you going to be doing this calculation? If it is rarely, then it doesn't matter. Using most efficient and most inefficient algorithms possible won't make a dent in CPU time if you are calling it only rarely.

Have you profiled your program to see where which parts is sucking all the CPU? One program I worked with involved tens of thousands of lines of code. It turned out that six lines out of those tens of thousands were responsible for more than 90% of the CPU usage. Needless to say, we optimized the heck out of those six lines (and the code immediately around them).

Have you analyzed your design? A lot of times the best way to make a program more efficient is to find out where you designed things stupidly. (Lots of supposedly brilliant designs are in fact rather stupid designs after the fact.)

Why are you representing things in spherical coordinates? Would cartesian be a better choice? If you are using object oriented techniques, it might be worthwhile to calculate both the spherical and cartesian representations every time you update. Then you will have both representations available and be able to use the one that makes more sense.

When it comes to calculating distances between points, the cartesian representation makes a lot more sense than does the spherical representation.
 
  • #6
Thank you very much pal for the help :). About how often I'm going to do this, heh, veryyyyyyyyyyyy often, you can say up to billions ;). You've pointed some good idea in including both coordinates, I think this'll reduce the calculation, I'll do my math and consider it ;)

Thanks a lot pal!
 

1. What is the formula for calculating the distance between two points in spherical coordinates?

The Haversine formula is commonly used to calculate the distance between two points in spherical coordinates. It takes into account the latitude and longitude of the two points and uses the Earth's radius to calculate the distance.

2. How accurate is the distance calculated using spherical coordinates?

The accuracy of the distance calculation in spherical coordinates depends on the Earth's radius used in the formula. The average radius of the Earth is commonly used, but for more precise calculations, the local radius at the given latitude can be used.

3. Can the distance between two points be negative in spherical coordinates?

No, the distance between two points in spherical coordinates is always a positive value. The direction of the distance can be represented by the azimuth angle, but the distance itself is always positive.

4. How does the distance between two points change if one of the points is moved to a different latitude or longitude?

Moving one of the points to a different latitude or longitude will change the distance between the two points. This is because the distance in spherical coordinates is calculated using the curvature of the Earth's surface, so any change in latitude or longitude will result in a change in distance.

5. Are there any alternative methods for calculating the distance between two points in spherical coordinates?

Yes, there are alternative methods for calculating the distance between two points in spherical coordinates, such as the Vincenty formula or the Great Circle Distance formula. These methods may provide more accurate results, but they also require more complex calculations.

Similar threads

Replies
11
Views
5K
Replies
12
Views
2K
  • Electromagnetism
Replies
4
Views
786
  • Precalculus Mathematics Homework Help
Replies
20
Views
2K
Replies
8
Views
1K
  • Precalculus Mathematics Homework Help
Replies
7
Views
862
  • Calculus and Beyond Homework Help
Replies
2
Views
19K
Back
Top