# Trilateration algorithm for n amount of points in R?

I read the page Trilateration algorithm for n amount of points and I have to do similar kind of trilateration in R.

I am not that expert in R, can anyone help, if there is similar function/package like mathematica in R?

• Isn't any one aware of the mulilateration problem? How to solve in R Apr 20 '14 at 12:02
• I am using R and I have a data set like > data_ref x y r 1 -1.90 -0.70 2.04 2 -0.90 -0.50 0.90 3 0.63 -0.65 0.72 4 0.31 -0.01 0.30 5 0.04 1.25 1.20 where x,y are the location of any antena, r is the distance of a device from there antenna. I am trying to minimize the residual as follows function(x,y,x0,y0,r) (sqrt(sum((x-x0)^2+(y-y0)^2))-r+sqrt(sum((x-x0)^2+(y-y0)^2))-r) nlm(norm_vec,c(mean(data_ref\$x),mean(data_ref\$y)),x0=data_ref\$x,y0=data_ref\$y,r=data_ref\$r) where x0, y0 is the seed point for iteration. but its not working, Can someone help me in understanding the flaw in my concept? Man Apr 20 '14 at 12:09

As I didn't find the existing answers to this problem on StackExchange to be satisfying, I will add my own solution here. This uses `geosphere` package to calculate distance between two polar (latitude, longitude) coordinates.

For a data frame:

``````> head(coordinates)
lat      lng distance
21 51.73832 10.72805     6000
31 51.76656 10.85404     6000
64 51.67559 10.82135     5000
70 51.75592 10.85369     5000
80 51.70379 10.79743     2000
89 51.68976 10.88211     6000
``````

use

``````n <- nls( distance ~ distm(data.frame(lng, lat), c(lng_solution, lat_solution), fun=distHaversine),
data = coordinates, start=list( lng_solution=10.9278778, lat_solution=51.6675738 ) )
``````

Substitute the coordinates in the last line with your start-point estimate and make sure the unit of `distance` equals the unit of the dist-function (this is dependent on whatever dist-function you use, such as `distHaversine`, `distRhumb`, `distMeeus`, etc).

(Note that the `geosphere` package uses the uncommon convention of writing longitude before latitude.)

Using the `destPoint` function of `geosphere` we can plot the arcs of our measured radii

``````plot(coordinates[, c("lng", "lat")])
apply(coordinates[, c("lng", "lat", "distance")], 1, function (x) polygon(destPoint(c(x, x), b=1:365, d=x)))
`````` Use the following code to plot the confidence ellipse:

``````c <- confidenceEllipse(n, levels=0.95)
ellipse_line <- c[1, ]
ellipse_line <- rbind(ellipse_line, coef(n))
lines(ellipse_line)
text(x = mean(ellipse_line[, 1]), y = mean(ellipse_line[, 2]),
labels=format(distm(ellipse_line[1,], ellipse_line[2,]), nsmall=1))
`````` I used the following and it is working now.

``````norm_vec <- function(x) sqrt(sum((x-data_ref\$x)^2+(x-data_ref\$x)^2))-sum(data_ref\$r)

nlm(norm_vec,c(mean(data_ref\$x),mean(data_ref\$y)))
``````

Thanks for sharing your code. Shouldn't it be:

``````norm_vec <- function(x) sqrt(sum((x-data_ref\$x)^2+(x-data_ref\$y)^2))-     sum(data_ref\$r)
``````
• Adding some explanation to your code is a more acceptable answer. Try to add some explanation on why the other answer is incorrect and explain what the code does for other readers. Sep 21 '15 at 19:57