R - 如何找到 2 个函数的交集

R - How to find the Intersection fo 2 Functions

我有 2 个函数,我想找到它们相交的值并将其绘制在图表上。我怎样才能最好地实现这一目标?有人知道 R 函数吗?

# First Function
  func6 <- function(x)
    {
    x * log(x) - sqrt(x)
  }

  # Second Function
  func7 <- function(x)
    {
    x
  }

  x <- seq(0,10,length=100)
  plot(x, func6(x), col="blue", lwd="1", main="Graph #1")
  lines(x, func7(x), col="red")

您可以使用uniroot搜索路口;这相当于寻找函数差的零。

rt <- uniroot(function(x)  func6(x) - func7(x)  , c(.01,10), tol=1e-8)     
# or explicitly
# rt <- uniroot(function(x)  x * log(x) - sqrt(x) - x , c(.01,10), tol=1e-8)  

# check
all.equal(func6(rt$root), func7(rt$root))  
# [1] TRUE

然后画出来

x <- seq(0, 10, length=100)
plot(x, func6(x), col="blue", lwd="1", main="Graph #1", type="l")
lines(x, func7(x), col="red")
points(rt$root, rt$root, pch=16, cex=2, col="red") 

正如K.Troy所指出的,在一般情况下,y坐标应该被转换:"The second argument to the points function should be to invoke either func6 or func7 using rt$root"

points(rt$root, func6(rt$root), pch=16, cex=2, col="red")