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?
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?
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[1], x[2]), b=1:365, d=x[3])))
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))
Thanks for sharing your code. Shouldn't it be:
norm_vec <- function(x) sqrt(sum((x[1]-data_ref$x)^2+(x[2]-data_ref$y)^2))- sum(data_ref$r)