Error ,when use integrate() to calculate area under curve

56 views
Skip to first unread message

guke su

unread,
Aug 6, 2017, 12:26:11 PM8/6/17
to ggplot2
set.seed(12345)
x <- seq(from = 0.001, to = 0.3, by = 0.001)
y <- runif(300,min=1,max = 126)
y <- sort(y)
A <- data.frame(x,y)
plot(A,type = "l")
f <- approxfun(x,y)
integrate(f, x[1], x[length(x)])  # 这组数积分报错, subdivisions 取1000也报错




(f, x[1], x[length(x)]) : 
  maximum number of subdivisions reached

Brandon Hurr

unread,
Aug 6, 2017, 5:45:02 PM8/6/17
to guke su, ggplot2
Guke, 

Firstly, this is a ggplot2 help list and your problem does not appear to involved ggplot2 whatsoever. 

Secondly, I don't get the error when using a slightly different seed than you, which indicates it is something to do with your 12345 seed. 

> set.seed(12346)
> x <- seq(from = 0.001, to = 0.3, by = 0.001)
> y <- runif(300,min=1,max = 126)
> y <- sort(y)
> A <- data.frame(x,y)
> plot(A,type = "l")
> f <- approxfun(x,y)
> integrate(f, x[1], x[length(x)])
19.11654 with absolute error < 0.00048
> set.seed(12347)
> x <- seq(from = 0.001, to = 0.3, by = 0.001)
> y <- runif(300,min=1,max = 126)
> y <- sort(y)
> A <- data.frame(x,y)
> plot(A,type = "l")
> f <- approxfun(x,y)
> integrate(f, x[1], x[length(x)])
18.02765 with absolute error < 0.0012

A quick google finds a SO post that suggests the lack of convergence has to do with many things. 

Increasing subdivisions gets an answer... 

> set.seed(12345)
> x <- seq(from = 0.001, to = 0.3, by = 0.001)
> y <- runif(300,min=1,max = 126)
> y <- sort(y)
> A <- data.frame(x,y)
> plot(A,type = "l")
> f <- approxfun(x,y)
> integrate(f, x[1], x[length(x)], subdivisions=2000)
20.20644 with absolute error < 0.0013

It never hurts to google "R [name of function] [error message]" to find out if anyone has had the problem before you. 

HTH, 
B

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guke su

unread,
Aug 7, 2017, 12:28:39 AM8/7/17
to ggplot2, lzax...@gmail.com
☆⌒(*^-゜)v,Thankyou very much.

在 2017年8月7日星期一 UTC+8上午5:45:02,Brandon Hurr写道:
To unsubscribe: email ggplot2+u...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages