[R] Intersection for two curves
David Winsemius
dwinsemius at comcast.net
Fri Apr 23 19:46:30 CEST 2010
On Apr 23, 2010, at 1:06 PM, Muhammad Rahiz wrote:
> Does anyone know of a method that I can get the intersection where
> the red and blue curves meet i.e. the value on the x-axis?
>
> x <- 1:10
> y <- 10:1
> plot(x,y)
> abline(lm(y~x),col="blue")
> abline(h=2.5,col="red")
Two ways :
> xy <- lm(y~x)
> xyf <- function(x) coef(xy)[1] +x*coef(xy)[2]
# absolute difference
> optimise(f=function(x) abs(xyf(x)-2.5), c(1,10) )
$minimum
[1] 8.49998
$objective
(Intercept)
1.932015e-05
#N minimize squared difference
> optimise(f=function(x) (xyf(x)-2.5)^2, c(1,10) )
$minimum
[1] 8.5
$objective
(Intercept)
3.155444e-30
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list