Distance between 2 points in spherical coordinates


by TheDestroyer
Tags: coordinates, distance, points, spherical
TheDestroyer
TheDestroyer is offline
#1
Oct12-09, 05:18 AM
P: 390
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
Phys.Org News Partner Science news on Phys.org
Lemurs match scent of a friend to sound of her voice
Repeated self-healing now possible in composite materials
'Heartbleed' fix may slow Web performance
D H
D H is online now
#2
Oct12-09, 07:55 AM
Mentor
P: 14,431
Quote Quote by TheDestroyer View Post
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.
TheDestroyer
TheDestroyer is offline
#3
Oct12-09, 09:04 AM
P: 390
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

TheDestroyer
TheDestroyer is offline
#4
Oct12-09, 09:06 AM
P: 390

Distance between 2 points in spherical coordinates


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? ;)
D H
D H is online now
#5
Oct12-09, 09:58 AM
Mentor
P: 14,431
Quote Quote by TheDestroyer View Post
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.


Quote Quote by TheDestroyer View Post
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.
TheDestroyer
TheDestroyer is offline
#6
Oct12-09, 02:05 PM
P: 390
Thank you very much pal for the help :). About how often I'm gonna 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!


Register to reply

Related Discussions
Spherical Coordinates Precalculus Mathematics Homework 2
Spherical Coordinates Calculus & Beyond Homework 4
Spherical Coordinates to Rectangular Coordinates Advanced Physics Homework 7
spherical coordinates? Calculus & Beyond Homework 2
spherical coordinates Calculus & Beyond Homework 2