Ninjasoup - 10 months ago 67

Python Question

I'm trying to use Newtons method to solve a Satellite Navigation problem, also I'm fairly new to programming.

I keep getting the following error:

`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'

I've tried changing the declaration of the unkown variables to different types but I keep getting the same error.

Any help would be greatly appreciated.

`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

Answer Source

From the `x=float`

lines, it looks like you want to do **symbolic calculation**. Having "unknown variables" isn't possible with the basic python syntax (and in a large portion of progamming languages afaik) but the `sympy`

package you're already using is meant just for that (you should probably check out the tutorial page here).

Here is how you would create symbolic variables (aka "unknow variables") :

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

But once that's is done you will notice that your code break a bit further down the road when you try to use `np.linalg.solve`

. The `numpy`

module is only meant to operate on special objects called numpy arrays ,they are essentially N-dimentionnal matrices of numbers *aka "not symbolic expressions"*.

But `sympy`

also has a solution to that problem : you can create matrices containing symbolic expressions and solve matricial equations too with ot . I'll just link you to the tutorial so you can see how to properly define said matrices and how to use them.