Plot a function with several arguments in R

前端 未结 4 2136
萌比男神i
萌比男神i 2020-12-19 22:47

Suppose I want to plot an R function:

weibull <- function(ALPHA, LAMBDA, T){
ALPHA*LAMBDA*(T^(ALPHA-1))
}

So the function takes the arg

4条回答
  •  太阳男子
    2020-12-19 23:34

    If you use curve, you can specify an expression with a free variable x that will get replaced by the range of values specified in your from=/to= parameters. For example you can do

    weibull <- function(ALPHA, LAMBDA, T){
        ALPHA*LAMBDA*(T^(ALPHA-1))
    }
    
    lambda<-c(1, 2, 4, 8, 16)
    col<-rainbow(length(lambda))
    layout(matrix(1:2, nrow=1))
    for(i in seq_along(lambda)) {
        curve(weibull(.5, lambda[i], x), from=0, to=2, add=i!=1, col=col[i], ylim=c(0,50), main="alpha=.5")
    }
    legend(1,50,lambda, col=col, lty=1)
    for(i in seq_along(lambda)) {
        curve(weibull(1, lambda[i], x), from=0, to=2, add=i!=1, col=col[i], ylim=c(0,20), main="alpha=1")
    }
    

    which will produce a plot like

    enter image description here

提交回复
热议问题