2016-11-27 94 views
0

我試圖用牛頓方法來解決衛星導航問題,我也是相當新的編程。實現牛頓方法

我不斷收到以下錯誤:

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 
+0

請閱讀[本文](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。和[本文](http://stackoverflow.com/help/how-to-ask)。 –

+0

我看到在你的問題中定義'fA'的行與代碼中的相似行不匹配。請運行你在這裏顯示的實際代碼,並顯示你得到的完整和準確的追溯結果的錯誤。 –

+0

我剛剛重新安排了fA,所有人也忘了重新運行代碼以獲取正確的錯誤,現在全部追溯到操作系統。 – Ninjasoup

回答

0

x=float線條,它看起來像你想要做符號計算。具有「未知變量」在基本Python語法中是不可能的(並且在大部分程序設計語言afaik中),但您已經使用的sympy軟件包僅用於此目的(您應該查看教程頁面here) 。

這裏是你將如何創建符號變量(又名「不明變量」):

from sympy import symbols 
x, y, z, b = symbols('x y z b') 

但是,一旦這做,你會發現你的代碼進一步打破了一下在路上,當您嘗試使用np.linalg.solvenumpy模塊僅用於對名爲numpy數組的特殊對象進行操作,它們本質上是數字又名「非符號表達式」的N維矩陣。

sympy也有這個問題的解決方案:您可以創建包含符號表達式的矩陣,也可以用ot解出矩陣方程。我只是link you to the tutorial,所以你可以看到如何正確定義所述矩陣以及如何使用它們。

+0

這正是我所需要的,謝謝我知道我想要的數學,只是不知道如何讓Python做到這一點。 – Ninjasoup

-1

需要初始化xyz,並b實際浮點值一樣0.0,不float

>>> 0.0 # a floating-point number 
0.0 
>>> float # an object of class 'type' named 'float' 
<class 'float'> 
>>> x = float # initialize to an object of class 'type' 
>>> y = 0.0 # initialize to a floating number 
>>> x 
<class 'float'> 
>>> y 
0.0 
>>> type(x) 
<class 'type'> 
>>> type(y) 
<class 'float'>