I recently asked this question about solving a system of nonlinear equations, over on Math SE. I am now attempting to implement the accepted answer using SymPy
in Python
. I'll begin by briefly recreating the solution here.
Problem.
Suppose I have three receivers, with coordinates given by vectors p1
, p2
, and p3
. Further, I have a transmitter, emitting radio pulses at unknown times t1, t2, t3, t4, ...
with known speed c
, and which has unknown initial coordinates at time t = t1
given by the vector x0 = <x,y,z>
and is moving with an unknown constant velocity given by the vector v
. My data consists of the pulse arrival time at each station, denoted air
(for arrival of time of signal i
at receiver r
).
My goal is to determine this velocity. The solution given in the attached answer is as follows:
The position of the transmitter at time
t
will be given byxo = v(t - t1)
. Hence, the distance between the transmitter and therth
receiver at the time theith
pulse is emitted will be given by:
||(ti - t1)v + x0 - pr||
Therefore, the time of arrival at the
rth
receiver is given by:
ti + (1/c) * ||(ti - t1)v + x0 - pr||
Which we know to be
air
. Therefore:
c**2(air - ti)**2 = ||v||**2(ti - t1)**2 + 2(ti -t1)<v, c0 - pr>
(where
<a, b>
denotes the scalar/dot product of vectorsa
andb
).This results in a system of
# receivers x # pulses
equations.
(see the attached answer for far nicer formatting)
Implementation.
I'm most familiar with the SymPy
package, so I'm attempting to implement a solution using its solvers. I'm a bit stuck, however, on where to begin.
The number of pulses is not known before hand thus, as SymPy
is a symbolic solver, I must be able to dynamically define variables t1, t2, t3... tn
.
My rough outline for an implementation is as follows:
import sympy as sp
def find_transmitter(arrival_times):
equations = []
for receiver in arrival_times:
for pulse in arrival_times:
equations.append(sp.Eq(c**2(pulse - ti...
sp.solve(equations...
Where arrival_times
is a list, where each column corresponds to a receiver, and each row corresponds to a pulse. I.e.:
[[a11, a21, a31...],[a12, a22, a32...],[a13, a23, a33...]]
It doesn't occur to me that there's a nice alternative to this approach. I'm not sure, however, e.g. how to dynamically define enough ti
's and iterate through them. That is, e.g., I'll have an unknown number of equations looking like:
equations.append(sp.Eq(c**2(pulse - t1...
equations.append(sp.Eq(c**2(pulse - t2...
equations.append(sp.Eq(c**2(pulse - t3...
equations.append(sp.Eq(c**2(pulse - t4...
equations.append(sp.Eq(c**2(pulse - t5...
.
.
.
Is there a nice way to do this? Very likely, I'm overlooking some obvious solution.