There have been many other questions like this one but they involve 3 points instead of four. I have code that will find a transmitter (red-$X$) in between 3 different points and it works great. I was hoping to add another receiver or in other words a 4th outside point. Is there any ideas of how I can modify this function to find the red-$X$ ($x,y$ form) in the plot with respect to four blue points instead of just three? (see plot or plot link below) Even just the Mathematics behind finding a point (having unknown coordinates) with respect to four other fixed points (known coordinates) would be great! Thank you very much.
Solving nonlinear equations with fsolve
% pylab inline
import pylab
from scipy.optimize import fsolve
def equations(p):
# Define these outside of the function before calling this function.
global gamma01,x0,y0,gamma12,x1,y1,x2,y2,gamma10
x,y = p
# The returned equations are from Power ~ 1/r**2, so
# the power ratio gammajk = Pj/Pk = rk**2/rj**2.
return ( gamma01*(x1-x)**2+gamma01*(y1-y)**2-(x0-x)**2-(y0-y)**2,
gamma12*(x2-x)**2+gamma12*(y2-y)**2-(x1-x)**2-(y1-y)**2 )
gamma01 = 1.0 # Received power antenna 1 over received power antenna 0
gamma12 = 1.0 # Received power antenna 2 over received power antenna 1
x0,y0 = 0.0, 1000.0 # Position receive antenna 0
x1,y1 = 1000.0, 0.0 # Position receive antenna 1
x2,y2 = 0.0, -1000.0 # Position receive antenna 2
# Numerically solve our nonlinear system of equations
# (1.0,1.0) is the initial guessed position
x, y = fsolve(equations, (1.0, 1.0))
print('answer x y (m)',x,y)
pylab.figure()
pylab.plot([x0,x1,x2],[y0,y1,y2],'bo',markersize=8.0,label='Receive Antenna')
pylab.plot([x],[y],'rx',markersize=8.0,label='Transmitter')
pylab.axis('equal')
pylab.xlabel('x (m)')
pylab.ylabel('y (m)')
pylab.title('All Power Ratios = 1.0')
pylab.legend()
pylab.grid()
pylab.show()
Let us make the problem general with $n$ receivers of known coordinates $(x_i,y_i)$ and a a transmitter of unknown coordinates $(X,Y)$ and let $d_i$ to be the distances.
So the real problem is to minimize with respect to $X$ and $Y$ the function $$F=\frac 12\sum_{i=1}^n \left(\sqrt{(X-x_i)^2+(Y-y_i)^2}-d_i\right)^2$$ and this will require good initial estimates.
To get these estimates, consider in a preliminary step the $n$ equations $$f_i=(X-x_i)^2+(Y-y_i)^2-d^2_i=0$$ and build the $\color{red}{\frac {n(n-1)}2}$ equations $$g_{ij}=f_i-f_j=2(x_j-x_i)X+2(y_j-y_i)Y+\Big[(x_i^2+y_i^2-d_i^2)-(x_j^2+y_j^2-d_j^2)\Big]=0$$ that you can easily solve using multilinear regression with no intercept (or matrix calculations). You could also notice that the minimum of $$\sum _{k=1}^n ( a_k X+ b_k Y+c_k)^2$$ is obtained solving $$X \sum _{k=1}^n a_k^2 + Y \sum _{k=1}^n a_kb_k+\sum _{k=1}^n a_kc_k=0$$ $$X \sum _{k=1}^n a_kb_k + Y \sum _{k=1}^n b_k^2+\sum _{k=1}^n b_kc_k=0$$
This will give you the estimates of $X$ and $Y$.
If you want to polish the solution, going back to $F$, you need to solve the equations $$\frac{\partial F}{\partial X}=\sum_{i=1}^n \frac{(X-x_i) \left(\sqrt{(X-x_i)^2+(Y-y_i)^2}-d_i\right)}{\sqrt{(X-x_i)^2+(Y-y_i)^2}}=0$$ $$\frac{\partial F}{\partial Y}=\sum_{i=1}^n \frac{(Y-y_i) \left(\sqrt{(X-x_i)^2+(Y-y_i)^2}-d_i\right)}{\sqrt{(X-x_i)^2+(Y-y_i)^2}}=0$$ which will be solved using Newton-Raphson method. If you are lazy as I am, do not waste time establishing the required derivatives $\frac{\partial^2 F}{\partial X^2}$,$\frac{\partial^2 F}{\partial X\partial Y}$, $\frac{\partial^2 F}{\partial Y^2}$ and just use central differences to get them numerically. Since the preliminary step will give you good estimates, this would converge very fast.