我正在尝试使用牛顿方法来解决卫星导航问题,我也是编程新手。
我一直收到以下错误:
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 :(得分:0)
从x=float
行,您似乎想要进行符号计算。拥有&#34;未知变量&#34;使用基本的python语法(以及大部分的编程语言afaik)是不可能的,但是你已经使用的sympy
包只是为了那个(你应该查看教程)第here页}。
以下是如何创建符号变量(又名&#34;未知变量&#34;):
from sympy import symbols
x, y, z, b = symbols('x y z b')
但是一旦完成,您会注意到当您尝试使用np.linalg.solve
时,您的代码会更进一步。 numpy
模块仅用于对称为numpy数组的特殊对象进行操作,它们本质上是数字的
但sympy
也有解决这个问题的方法:你可以用ot创建包含符号表达式的矩阵并解决矩阵方程。我只是link you to the tutorial所以你可以看到如何正确定义所说的矩阵以及如何使用它们。
答案 1 :(得分:-1)
您需要将x
,y
,z
和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'>