[R] How to calculate a double integral ...?

cjosephlu@seed.net.tw cjosephlu at seed.net.tw
Sat Oct 23 02:57:39 CEST 2004


Gabor Grothendieck <ggrothendieck at myway.com> writes:
> cjlu <cjlu <at> mail.ncku.edu.tw> writes:
> : How can I calculate a double integral like
> : 
> :   \int_a^b \int_c^y g(x, y) dx dy
> : 
> : where a, b, c are constants, g(x, y), e.g.,
> : g(x, y) = tan(x + y).
> :  
> Integrate
> 
>    g2 <- function(x, y) g(x, y) * (x > y)
> 
> over (a,b) x (a,c).
> 
Thanks Gabor for your prompt and helpful response!

Later on, I realized that such an integration can be done by
the following way, provided function g(x, y) takes vector input, e.g.,
g(x, y) = tan(x + y)

> integrate(function(y) {
+   sapply(y, function(y) {
+     integrate(function(x) tan(x + y), -.5, y)$value
+   })
+ }, 0, .5)
0.07238855 with absolute error < 1.1e-15

if g(x, y) can only take scalar input, we can do

> integrate(function(y) {
+   sapply(y, function(y) {
+     integrate(function(x) {
+       sapply(x, function(x) tan(x + y))
+     }, -.5, y)$value
+   })
+ }, 0, .5)
0.07238855 with absolute error < 1.1e-15

Best regards,

C. Joseph Lu
Department of Statistics
National Cheng-Kung University
Tainan
Taiwan, ROC

-------------------------------------------------
This mail sent through Seednet Webmail
http://webmail.seed.net.tw




More information about the R-help mailing list