Skip to content
Back to formatted view

Raw Message

Message-ID: <20101210094715.GB17431@cs.cas.cz>
Date: 2010-12-10T09:47:15Z
From: Petr Savicky
Subject: Minimization of the distance
In-Reply-To: <1291952632814-3081345.post@n4.nabble.com>

On Thu, Dec 09, 2010 at 07:43:52PM -0800, bluesky wrote:
> 
> I just contect R,and still learn how to write the code.
> I have a problem with argmin sum d(pi,p)/n 
> for example I have 3 points (a1,b1)(a2,b2)(a3,b3) ,then I want to find
> p(x,y) make sure that 
> (sqrt((a1-x)^2+(b1-y)^2)+sqrt((a2-x)^2+(b2-y)^2)+sqrt((a3-x)^2+(b3-y)^2))/3
> is the minimum.

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.