Distance between 2 points in spherical coordinates

Click For Summary

Discussion Overview

The discussion revolves around calculating the distance between two points in spherical coordinates, specifically focusing on the mathematical approach and programming considerations in Matlab. Participants explore different methods to compute this distance without converting to Cartesian coordinates, while addressing efficiency concerns in computational algorithms.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using the law of cosines to find the distance between two points by first calculating the angle between their position vectors.
  • Another participant expresses skepticism about the efficiency concerns raised, arguing that efficiency should not be a primary concern in most programming contexts.
  • A participant emphasizes the importance of accuracy over efficiency, noting that avoiding Cartesian coordinates can reduce error.
  • One participant clarifies the relationship between the cosine of the angle and the distance calculation, providing a formula for the cosine based on spherical coordinates.
  • There is a suggestion that the geodesic distance on a sphere is relevant to the problem, as it involves similar calculations for angles and distances.
  • Another participant proposes that converting to Cartesian coordinates might simplify the distance calculation, despite initial resistance to this approach.
  • One participant mentions the potential for high-frequency calculations, indicating that efficiency may be more critical in their specific case.

Areas of Agreement / Disagreement

Participants express differing views on the importance of computational efficiency versus accuracy, with no consensus reached on the best approach to calculate the distance in spherical coordinates. Multiple methods and perspectives are presented without agreement on a single solution.

Contextual Notes

Participants highlight the complexity of the problem, including the need for accurate calculations and the potential for high computational demands. There are unresolved considerations regarding the best coordinate system to use for efficiency and accuracy in calculations.

TheDestroyer
Messages
401
Reaction score
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
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.
 
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
 
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? ;)
 
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<br /> \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 \\<br /> &= \cos\phi_1\cos\phi_2\cos(\theta_1-\theta_2) + \sin\phi_1\sin\phi_2<br /> \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.
 
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!
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
22K
Replies
20
Views
4K