Dear List, I know this is not the first post on this topic, but I need basic help I guess. Assuming the simple case of two lines with one intercept, how can I make R calculate this intercept, NOT using locator(). par (xaxs="i", yaxs="i") plot( 1, bty="n" ,xlim=c(0,300) , ylim=c(0,300) , xlab="X", ylab="Y") curve(100-0.5*x, -50,250, add=T, col="blue") curve(150- x , -50,250, add=T, col="red") I want R to come up with the coordinates X=100, Y=50. Thank you for your help! -- View this message in context: http://r.789695.n4.nabble.com/Intercept-between-two-lines-tp4587343p4587343.html Sent from the R help mailing list archive at Nabble.com.
Hi pannigh, The following might get you started:> x0 <- uniroot(function(x) 100-0.5*x - (150- x), c(0, 150))$root > x0[1] 100> 100- 0.5*x0[1] 50 HTH, Jorge.- On Wed, Apr 25, 2012 at 1:01 PM, pannigh <> wrote:> Dear List, > I know this is not the first post on this topic, but I need basic help I > guess. Assuming the simple case of two lines with one intercept, how can I > make R calculate this intercept, NOT using locator(). > > par (xaxs="i", yaxs="i") > plot( 1, bty="n" ,xlim=c(0,300) , ylim=c(0,300) , xlab="X", ylab="Y") > curve(100-0.5*x, -50,250, add=T, col="blue") > curve(150- x , -50,250, add=T, col="red") > > I want R to come up with the coordinates X=100, Y=50. > > Thank you for your help! > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Intercept-between-two-lines-tp4587343p4587343.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thank you Jorge, this did get me started and who else might be interested in the topic, a possible code could be something like: # Define the functions f1 <- function(x) 100-0.5*x f2 <- function(x) 150- x # Plot the functions par (xaxs="i", yaxs="i") plot( 1, bty="n" ,xlim=c(0,300) , ylim=c(0,300) , xlab="X", ylab="Y") curve(100-0.5*x, -50,250, add=T, col="blue") curve(150- x , -50,250, add=T, col="red") # Calculate the intercept x0 <- uniroot(function(x) 100-0.5*x - (150- x), c(0, 250))$root y0 <- f2(x0) # f1(x0) gives the same result # Add the result to the plot points(x0,y0, pch=19, col=3, cex=2) text(x0+10,y0, adj=0, paste("S(",round(x0,2),",",round(y0,2),")", sep="")) -- View this message in context: http://r.789695.n4.nabble.com/Intercept-between-two-lines-tp4587343p4589304.html Sent from the R help mailing list archive at Nabble.com.
You could also use polyroot. Do x0=Re( polyroot( c(100,-0.5) - c(150,-1) ) ) In your code. -- View this message in context: http://r.789695.n4.nabble.com/Intercept-between-two-lines-tp4587343p4589349.html Sent from the R help mailing list archive at Nabble.com.