2

I am having problems calculating the coordinates for the for distance matrix D :

D :   R   D4    D6
R   [[0.  234.  150.]
D4  [234.   0.  231.]
D6  [150. 231.   0.]]

My python code is as follows:

import numpy
from numpy import linalg as LA
import matplotlib.pyplot as plt
import math 

def calc_points_and_plot():
    distance_matric = numpy.zeros((3, 3))

    weight_router_to_dongle_04 =  234
    weight_router_to_dongle_06 =  150
    weight_dongle_04_to_dongle_06 =  231

    distance_matric[0][1] = weight_router_to_dongle_04
    distance_matric[0][2] = weight_router_to_dongle_06
    distance_matric[1][0] = weight_router_to_dongle_04
    distance_matric[1][2] = weight_dongle_04_to_dongle_06
    distance_matric[2][0] = weight_router_to_dongle_06
    distance_matric[2][1] = weight_dongle_04_to_dongle_06

    print(f"dist matric \n {distance_matric}")
    m_matrix = numpy.zeros((3, 3))
    for i in range(3):
        for j in range(3):
            m_matrix[i][j] = 0.5 * ((distance_matric[1][j]**2) + (distance_matric[i][1]**2) -(distance_matric[i][j]**2))

    print(f"m_matrix \n {m_matrix}")
    eigvals, eigvecs = LA.eig(m_matrix)
    print(f"eigen vals  : \n {eigvals}")
    print(f"eigen vectors  : \n {eigvecs}")

    results = []
    for i in range(3):
        if eigvals[i] != 0:
            results.append(math.sqrt(eigvals[i])*eigvecs[i])

    print(f"results \n {results}")
    coords = numpy.reshape(results, (2,3)).T

    print(coords)
    X_vals = [coords[i][0] for i in range(3)]
    Y_vals = [coords[i][1] for i in range(3)]

    plt.annotate(f"Roouter",  xy=(X_vals[0], Y_vals[0]))
    plt.annotate(f"device 4", xy=(X_vals[1], Y_vals[1]))
    plt.annotate(f"device 6", xy=(X_vals[2], Y_vals[2]))

    plt.scatter(X_vals , Y_vals, s=230, c="black", marker="D")
    plt.scatter(X_vals , Y_vals, s=180, c="red", marker="D" )
    plt.plot([X_vals[0], X_vals[1]], [Y_vals[0], Y_vals[1]], c="red", linewidth=1, linestyle='--')
    plt.plot([X_vals[0], X_vals[2]], [Y_vals[0], Y_vals[2]], c="red", linewidth=1, linestyle='--')
    plt.plot([X_vals[1], X_vals[2]], [Y_vals[1], Y_vals[2]], c="red", linewidth=1, linestyle='--')

    # Verify distances to given distance matrix
    dist1 = math.sqrt((X_vals[0]-X_vals[1])**2 + (Y_vals[0]- Y_vals[1])**2)
    dist2 = math.sqrt((X_vals[0]-X_vals[2])**2 + (Y_vals[0]- Y_vals[2])**2)
    dist3 = math.sqrt((X_vals[1]-X_vals[2])**2 + (Y_vals[1]- Y_vals[2])**2)

    print(f"dist1 : {dist1}" )
    print(f"dist2 : {dist2}" )
    print(f"dist3 : {dist3}" )
    plt.show()


if __name__ == "__main__":

    calc_points_and_plot()

The following pictures are the actual output and the expected Actual output Expected output

I followed this link finding-the-coordinates-of-points-from-distance-matrix, but still didnt manage to get the correct (x,y) points.

  • Looking at the actual output, the distance from router to device4 has a point to point distance of 440, even though the given distance matrix says otherwise.
Anton Sihm
  • 21
  • 2
  • A distance matrix has less information than a list of points which has that distance matrix. There are infinitely many solutions (if there are any solutions at all). How do you intend to select one of those infinitely many solutions? The problem is under-specified. Also, for the special case of a 3x3 distance matrix (which is what your example shows), it is easy enough to do use some trig to find three points with the specified matrix – John Coleman Nov 27 '20 at 20:07
  • Hello John thanks for answering. I understoud it as, given 3 distances between 3 points, we can obtain one of the many solutions for a (x,y) set that satisfies the distances. The problem is that the output (x,y) coords does satisfy the given distances. Distances of output points: dist1 : 440.15 dist2 : 245.90 dist3 : 242.67 Given distances in Distance matrix D: dist1 = 234 dist2 = 150 dist3 = 231 – Anton Sihm Nov 27 '20 at 20:13
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. Off-site links and images of text are not acceptable, in keeping with the purpose of this site. – Prune Nov 27 '20 at 20:14
  • Your given code has a lot of overhead for superfluous plotting, as well as number-shuffling overhead. Reduce this to the coordinate computations, trace the math, and show where you're confused. It's not enough to hand us a block of repetitive code and say "gee, it got the wrong output". Those first debugging steps are still your job. – Prune Nov 27 '20 at 20:16
  • Also, for three points, the task is much simpler. Set one point arbitrarily to (0,0). Pick another point, and set it to a trivial location that matches that distance, such as (234, 0). Now you need to find either of the points at distance 150 from the origin and 231 from the second point; this is a simple quadratic solution. – Prune Nov 27 '20 at 20:19
  • @Prune I started with the simple distance problem of 3 objects, but the problem is that laster i will have more objects. I have also thought about the solution you mentioned with the first object in (0,0) then (0, p2dist) and from that calculating (x3,y3). The output coordinates from my proposed method, outputs (x,y) sets that does not match the given distance set. Btw I also updated the code so it is runnable – Anton Sihm Nov 27 '20 at 20:35
  • I'll wait for the MRE and trace, as requested / expected. – Prune Nov 27 '20 at 23:40

0 Answers0