Skip to content

Instantly share code, notes, and snippets.

@tuupola
Last active May 31, 2017 08:09
Show Gist options
  • Save tuupola/0df4934758fa04a3f07c96d55cd31bb1 to your computer and use it in GitHub Desktop.
Save tuupola/0df4934758fa04a3f07c96d55cd31bb1 to your computer and use it in GitHub Desktop.
Nonlinear least squares trilateration with n points in R: https://appelsiini.net/2017/trilateration-with-n-points/
library(geosphere)
locations <- data.frame(
latitude = c(
59.42606837, 59.42610146, 59.42654852, 59.42609108,
59.42603039, 59.42666361
),
longitude = c(
24.72553151, 24.72552969, 24.72467492, 24.72555759,
24.72565661, 24.72449149
),
distance = c(8, 8, 9, 9, 9, 14)
)
# Use average as the starting point
fit <- nls(
distance ~ distm(
data.frame(longitude, latitude),
c(fitLongitude, fitLatitude)
),
data = locations,
start = list(
fitLongitude=mean(locations$longitude),
fitLatitude=mean(locations$latitude)
),
control = list(maxiter = 1000, tol = 1e-02)
)
# Result
latitude <- summary(fit)$coefficients[2]
longitude <- summary(fit)$coefficients[1]
paste(latitude, longitude, sep=",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment