0

Seawas

I would like to plot an integral of a function with a variable upper limit in R. However, the upper limit is not the 'pure' x-coordinate, but a calculation of it. I struggle to pass the calculated x-coordinate to my plot function. Using ggplot2, I have got this far – thanks to Thomas and mnel:

library(ggplot2)

#sample function
f_a   <- function(a) { 2 * a }

#set up integration
int_f <- Vectorize( function( f = f_a , lower = 0, upper , ... ){
            integrate( f , lower , upper , ...)[['value']] } , 'upper' )

#plot it
ggplot( data.frame( b = 0 ) , aes( x = b ) ) + 
    stat_function( fun = int_f , args = list( f = f_a , lower = 0 ) ) +
    xlim( 0 , 4 )

With this, I get a plot of the integrals with b as the upper limits. The thing is, my upper limits are a function of b, say: c(b). My attempts with mappings and other int_f setups have failed as much as my queries here on stackoverflow.

Would be amazing if someone brilliant of this community could show me how to get this thing solved.

1 Answers1

0

Found out myself. Embarrassingly cheap solution (with c() as the function for the upper limit):

...

int_f <- Vectorize( function( f = f_a , lower = 0, upper , ... ){
            integrate( f , lower , c(upper) , ...)[['value']] } , 'upper' )
...

Hope it helps someone!