我試圖用牛頓方法來解決衛星導航問題,我也是相當新的編程。實現牛頓方法
我不斷收到以下錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Ninjasoup\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "C:\Users\Ninjasoup\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Ninjasoup/Desktop/MSc Space Science/SatNav AssignmentCode/SatNavCode1.3.py", line 72, in <module>
fA = np.sqrt((x-xA)**2 + (y-yA)**2 + (z-zA)**2) - (dA-b)
TypeError: unsupported operand type(s) for -: 'type' and 'float'
我試圖改變未知變量,以不同類型的聲明,但我不斷收到同樣的錯誤。
任何幫助將不勝感激。
import math
import numpy as np
from sympy import diff
#Constants
R = 6371
SD = 20200
c = 299792458
#Given Data
latA = 47.074834081442773
lonA = 18.487157448324282
latB = 17.949919573189003
lonB = 17.786195009535710
latC = 48.196896294687626
lonC = -67.929788607990332
latD = 77.374761092966111
lonD = -25.681600844602748
tofA = 0.070745909570054
tofB = 0.075407082536252
tofC = 0.074696101874954
tofD = 0.071921760657713
#Pseudo Range error
dA = c*tofA
dB = c*tofB
dC = c*tofC
dD = c*tofD
#Unknown Variables
x =float
y =float
z =float
b =float
#Coversion of Shperical to Cartesian Co-ordinates
xA = (R+SD) * math.cos(math.radians(latA)) * math.cos(math.radians(lonA))
yA = (R+SD) * math.cos(math.radians(latA)) * math.sin(math.radians(lonA))
zA = (R+SD) *math.sin(math.radians(latA))
xB = (R+SD) * math.cos(math.radians(latB)) * math.cos(math.radians(lonB))
yB = (R+SD) * math.cos(math.radians(latB)) * math.sin(math.radians(lonB))
zB = (R+SD) *math.sin(math.radians(latB))
xC = (R+SD) * math.cos(math.radians(latC)) * math.cos(math.radians(lonC))
yC = (R+SD) * math.cos(math.radians(latC)) * math.sin(math.radians(lonC))
zC = (R+SD) *math.sin(math.radians(latC))
xD = (R+SD) * math.cos(math.radians(latD)) * math.cos(math.radians(lonD))
yD = (R+SD) * math.cos(math.radians(latD)) * math.sin(math.radians(lonD))
zD = (R+SD) *math.sin(math.radians(latD))
#P1 = np.array([xA,yA,zA])
#P2 = np.array([xB,yB,zB])
#P3 = np.array([xC,yC,zC])
#P4 = np.array([xD,yD,zD])
#print(P1,P2,P3,P4)
fA = np.sqrt((x-xA)**2 + (y-yA)**2 + (z-zA)**2) - (dA-b)
dfA1 = diff(fA, x)
dfA2 = diff(fA, y)
dfA3 = diff(fA, z)
dfA4 = diff(fA, b)
fB = np.sqrt((x-xB)**2 + (y-yB)**2 + (z-zB)**2) - (dB-b)
dfB1 = diff(fB, x)
dfB2 = diff(fB, y)
dfB3 = diff(fB, z)
dfB4 = diff(fB, b)
fC = np.sqrt((x-xC)**2 + (y-yC)**2 + (z-zC)**2) - (dC-b)
dfC1 = diff(fC, x)
dfC2 = diff(fC, y)
dfC3 = diff(fC, z)
dfC4 = diff(fC, b)
fD = np.sqrt((x-xD)**2 + (y-yD)**2 + (z-zD)**2) - (dD-b)
dfD1 = diff(fD, x)
dfD2 = diff(fD, y)
dfD3 = diff(fD, z)
dfD4 = diff(fD, b)
#Matrix of Partial derivatives (Jacobian)
J = [[dfA1,dfA2,dfA3,dfA4],
[dfB1,dfB2,dfB3,dfB4],
[dfC1,dfC2,dfC3,dfC4],
[dfD1,dfD2,dfD3,dfD4]]
print(J)
#Matrix of functions
F = [[fA],
[fB],
[fC],
[fD]]
print(F)
#Guess Values
U = [[1],
[1],
[1],
[1]]
#Evaluated values
x,y,z,b = U - np.linalg.solve(J,F)
#Iteration 2..will do more iterations later.
U1 = [[x],
[y],
[z],
[b]]
x1,y1,z1,b1 = U1 - np.linalg.solve(J,F)
#Convert x,y,z back to spherical constants once code is working
請閱讀[本文](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。和[本文](http://stackoverflow.com/help/how-to-ask)。 –
我看到在你的問題中定義'fA'的行與代碼中的相似行不匹配。請運行你在這裏顯示的實際代碼,並顯示你得到的完整和準確的追溯結果的錯誤。 –
我剛剛重新安排了fA,所有人也忘了重新運行代碼以獲取正確的錯誤,現在全部追溯到操作系統。 – Ninjasoup