2016-11-27 73 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'>