Skip to content
Prev 244528 / 398502 Next

Minimization of the distance

On Thu, Dec 09, 2010 at 07:43:52PM -0800, bluesky wrote:
The following code solves the example as i understand it.

  # rows of matrix "a" are three points in the plane
  a <- rbind(
  c(1, 1),
  c(2.3, 1),
  c(3, 3))

  d <- function(x, a) mean(sqrt(rowSums((a - rep(x, each=nrow(a)))^2)))

  xinit <- colMeans(a)
  x <- optim(xinit, d, a=a)$par
 
  plot(a)
  points(rbind(x), col=2)

Is this, what you mean?

Function optim() has further parameters, which influence efficiency
and accuracy, and there are also other optimization functions.

Petr Savicky.