我正在python中开发一个函数,它使用GPS坐标,使用Vincenty的公式计算距离。第一步是使用while循环迭代一组方程直到收敛。”lam”是我输出并反馈到while循环中的变量。while循环第一次运行,之后每次迭代的输出与第一次完全相同。循环每次都使用lam的初始值。它应该以lam输出作为新的输入,对吗?在
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)
实际上,它是使用新值
lam
的。您可以通过在循环的开始和结束处打印id(lam)
来检查自己。在问题是,由于某些原因,您的函数稳定在
lam=0.0027964244626017456
的值上,因此循环永远不会退出。在在一个SO问题中调试这个算法太难了,但是请尝试查找该算法并检查您是否在某个地方键入了错误。在
正如Erik所写的,它确实是在循环中每次使用新计算的lam(bda)值。在重新计算后,这个值(几乎)是相同的值,这一事实是确切地说是“收敛于某个值”。在
所以你做的正是你想要的,就是(在这个例子中,非常迅速地)“迭代一组方程直到收敛”。但您的检查不应该是lambda接近零值,而是应该在lambda中的更改接近零时停止循环。或者更确切地说,λ的变化幅度。在
还有两点:
math.radians
进行此操作。在math.atan2
的确切定义。你可能会发现你的参数颠倒了。在相关问题 更多 >
编程相关推荐