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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
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
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
(Note that the
geosphere package uses the uncommon convention of writing longitude before latitude.)
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)