1

I'm working on a function in python that uses GPS coordinates to calculate distance using Vincenty's formulae. The first step is to iterate a set of equations until convergence using a while loop. "lam" is the variable I'm outputting and feeding back into the while loop. The while loop runs the first time, then the output every iteration after that is the exact same output as the first time. The loop is using the initial value of lam every time. It's supposed to take the lam output and use it as the new input, correct?

import math
location=[-83.55176667, -83.548975, 40.30421944, 49.30228889]

def distance(points):
    """ points is a list containing 2 latitude/longitude points
    in this order: [lon1, lon2, lat1, lat2]. This function determines
    distance between those 2 points."""
    L1, L2, theta1, theta2=points[0],points[1],points[2],points[3]
    f=1/298.257223563
    L=L2-L1
    lam=L
    outs=[]
    U1=math.atan((1-f)*math.tan(theta1))
    U2=math.atan((1-f)*math.tan(theta2))

    while lam > .001:
        sin_sigma=((math.cos(U2)*math.sin(lam))**2+
            (math.cos(U1)*math.sin(U2)-
             math.sin(U1)*math.cos(U2)*math.cos(lam))**2)**0.5
        cos_sigma=math.sin(U1)*math.sin(U2)+math.cos(U1)*
        math.cos(U2)*math.cos(lam)            
        sigma=math.atan2(sin_sigma,cos_sigma)
        sin_alpha=(math.cos(U1)*math.cos(U2)*math.sin(lam))/sin_sigma
        cos2_alpha=1-(sin_alpha)**2
        cos2sigm=cos_sigma-((2*math.sin(U1)*math.sin(U2))/cos2_alpha)
        C=(f/16)*cos2_alpha*(4+f*(4-3*cos2_alpha))
        lam=L+(1-C)*f*sin_alpha*(sigma+C*sin_sigma*
             (cos2sigm+C*cos_sigma*(-1+2*(cos2sigm)**2)))
        outs.append(lam)
        print(lam)

    print('')
    return outs

outs=distance(location)
4
  • So the variable 'outs' has multiple values of lam that are the same? Commented Mar 9, 2018 at 14:55
  • If you step through the code you will see that the first 5 values do change, at which point I assume the differences are in one of the decimal points which are not being shown as so looks like nothing is changing Commented Mar 9, 2018 at 14:57
  • Have you considered that perhaps the precision of standard floating point numbers is insufficient? They have a limited number of significant digits. For a simple example, try typing 1e-20 + 1 in the interpreter. Try to see if you have any numbers interacting that differ in size by many orders of magnitude. Commented Mar 9, 2018 at 15:12
  • Thank you, the output is changing very far beyond the decimal point. Problem solved! Commented Mar 9, 2018 at 15:19

2 Answers 2

2

Actually, it is using the new value of lam. You can check yourself by printing id(lam) in the beginning and end of your loop.

The problem is that, for some reason, your function is stabilizing on a value of lam=0.0027964244626017456, so your loop never exits.

Debugging the algorithm in a SO question is too much to ask, but try looking up the algorithm and checking if you made a typo somewhere.

Sign up to request clarification or add additional context in comments.

Comments

1

As Erik wrote, it is indeed using your new calculated value of lam(bda) each time around the loop. The fact that this value is (almost) the same value after recalculation is exactly what is meant by "converging on a value".

So you're doing exactly what you want, which is (in this case very rapidly) "iterate a set of equations until convergence". But your check should be not that lambda reaches a near-zero value, but you should stop looping when the change in lambda reaches near-zero. Or rather, the magnitude of change of lambda.

Two other points:

  • use radians for your angle values, not degrees. You can use math.radians for this.
  • check out the exact definition of math.atan2. You may find that you've got your parameters upside down.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.