简体   繁体   English

使用r在曲线下方或上方的点

[英]points below or above the curve using r

I try to figure out, if my point is below or above a simple curve and struggling with my math at the moment, I guess... 我试图弄清楚,如果我的点低于或高于一条简单曲线,并且目前正在与数学作斗争,我想...

I prepared a working example, so but the math first. 我准备了一个有效的示例,因此首先进行数学运算。

I have some points and I want to check if they are above or below a curve... The curve has the function y=1/(x-.5) So I thought I will set the function to 0 and get 0=1/(x-.5)-y 我有一些要点,我想检查它们是否在曲线上方或下方...曲线具有函数y=1/(x-.5)所以我想我将函数设置为0并得到0=1/(x-.5)-y

Afterwards I will get negative values if the point is on one side of the curve, and positive values on the other side... Hope this is right so far... 然后,如果该点在曲线的一侧,则我将得到负值,而在另一侧,则将是正值。希望到目前为止,这是正确的...

I realised a problem, if the x values is smaller then .5, then the part below 1/ gets negative and all my values are also negative... 我意识到一个问题,如果x值小于.5,则低于1 /的部分变为负数,而我所有的值也均为负数...

I added a special point (5) which gives the expected positive value, but how about the other ones, how should I test those? 我添加了一个特殊点(5),该点给出了预期的正值,但是其他点如何处理,我应该如何测试呢? Thanks for help! 感谢帮助!

points <- data.frame(
  x=c(-3.6030515,-0.2791478,10.2045860,-0.7457344,1,0.4037591,0.1555678,
      6.1525442,1.9831603),
  y=c(0.95715140,0.18139107,2.87456154,0.17190597,0.5,0.09778570,0.02708183,
      2.69455955,1.09943870)
)
curves <- data.frame(x=c(seq(.1,10,.1)))
curves$y <- 1/(curves$x-.5)

plot(points$x,points$y)
lines(curves$x,curves$y)
lines(-curves$x,curves$y)

1/(points$x-.5)-points$y >= 0

Thanks in advance! 提前致谢!

Unless I've misunderstood the question, you should be able to just evaluate the function at your points' x values, and compare the outcome (ie the y value according to the function) to your points' y values. 除非我对这个问题有误解,否则您应该只可以按点的x值评估函数,然后将结果(即根据函数的y值)与点的y值进行比较。

f <- function(x) 1 / (x-0.5)
f(points$x) < points$y

# [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE

With the way I've structured the inequality, TRUE indicates that the curve is below the corresponding y value in the points vector. 用我构造不等式的方式, TRUE表示曲线低于points向量中的相应y值。 In other words, all but the fifth point are above the curve. 换句话说,除第五点外,所有点都在曲线上方。

To count the number below the curve : 要计算曲线下方的数字:

## count the number below the curve 
sum(points$y<1/(points$x-0.5) )

To show it graphically : 以图形方式显示:

## plot it using plot and curve
plot(points$x,points$y,col=ifelse(points$y<1/(points$x-0.5) ,'blue','red'),pch=20)
curve(1/(x-.5),-4,10,add=TRUE,col='green',lwd=2)

在此处输入图片说明

discontinuity part : 不连续部分:

To show the discontinuity part graphically you should use curve : 要以图形方式显示不连续部分,应使用curve

curve(1/(x-.5),0,1,col='green',lwd=2)
abline(v=0.5,lwd=3)

在此处输入图片说明 ` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM