Reputation: 2180
I learned in this question calculating double integrals in R quickly how to do double integral.
My problem is when there is one more argument of the function. Please see the example.
z = 2
fun0 = function(x,y) { tan(x+y)*z }
fun01 = integrate(function(y) {
sapply(y, function(y) {
integrate(function(x) fun0(x,y), -.5, y)$value
})
}, 0, .5)$value
The result is as follows.
> fun01
[1] 0.1447771
I would like to make this 'fun01' to a function of z.
Upvotes: 2
Views: 2219
Reputation: 2180
Just after posting this question, I think I found the answer.
fun0 = function(x,y,z) { tan(x+y)*z }
fun01 = function(z) {
integrate(function(y,z) {
sapply(y, function(y) {
integrate(function(x,z) fun0(x,y,z), -.5, y, z=z)$value
})
}, 0, .5, z=z )$value
}
It works as follows.
> fun01(2)
[1] 0.1447771
> fun01(3)
[1] 0.2171656
Upvotes: 0
Reputation: 206546
How about this
fun0 = function(x,y,z) { tan(x+y)*z }
fun01 <- function(z) {
integrate(function(y) {
sapply(y, function(y) {
integrate(function(x) fun0(x,y,z), -.5, y)$value
})
}, 0, .5)$value
}
fun01(2)
Just take the z
out of the global environment and pass it thought where you need it.
Upvotes: 3