【问题标题】:How to plot a function of integrals with upper limits as another function in R?如何绘制具有上限的积分函数作为 R 中的另一个函数?
【发布时间】:2020-07-31 05:13:41
【问题描述】:

海浪

我想在 R 中绘制一个具有可变上限的函数的积分。但是,上限不是“纯”x 坐标,而是它的计算。我很难将计算出的 x 坐标传递给我的绘图函数。使用 ggplot2,我已经走到了这一步——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 )

有了这个,我得到了以b 为上限的积分图。问题是,我的上限是b 的函数,比如:c(b)。我对映射和其他int_f 设置的尝试与我在stackoverflow 上的查询一样失败。

如果这个社区的杰出人士能告诉我如何解决这个问题,那就太棒了。

【问题讨论】:

    标签: r ggplot2 mapping integrate


    【解决方案1】:

    发现自己。令人尴尬的廉价解决方案(以c()作为上限的函数):

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

    希望对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2012-02-08
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2015-06-03
      • 2015-12-16
      • 2016-05-20
      相关资源
      最近更新 更多