简体   繁体   中英

How to plot a function with one variable for several parameter values?

Good afternoon !

I have the following function of the variable n:

P(n) = 1-(1-p)^n 
  • p is a parameter in the interval [0, 1]

I'm wanting to plot this one dimentional function with several values of p, within the same plot:

p=seq(0 , 1 , 0.1 ) 

So i tried for p=0.1 and p=0.9:

p=0.1

n <- seq(0, 100, 8) ;  y <- 1-(1-p)^n 

plot(n , y ,pch= 21,type = "o", col = "red", xlab = "n-values", ylab = "P(n) values ", main = "P(n)= 1-(1-p)^n ")

p=0.9

lines(n , 1-(1-p)^n , type="o", pch=16 , col="blue") 

legend(60 , 0.4, c("p=0.1","p=0.9"), cex=0.7, col=c("red", "blue"),pch=c(21, 16))

This gives:

在此处输入图像描述

The problem:

  • I'm searching an elegant way to plot this function for all values of p=seq(0,1,0.1) within the same plot. I'm wanting to delete the extrapolation effect ( I'm wanting to delete the pch symbols at all, the graph sould contains the curves with different colors and without the effect of extrapoling segments ).

  • I wish my question is clear.

    Thank you for help in advance !

You can use outer and matplot , more x values and a suitable color scheme:

n <- seq(0, 100, length.out=200)
p <- seq(0.1, 1, 0.1)
f <- function(n, p) 1-(1-p)^n
M <- outer(n, p, "f")
col <- hcl.colors(length(p), "Spectral")
matplot(n, M, type = "l", col = col, lty = 1)
legend("bottomright", legend = p, col = col, lty = 1)

Another simply way is replacing

lines(n , 1-(1-p)^n , type="o", pch=16 , col="blue") 

with

p = seq(0,1,0.1)
invisible(lapply(p, function(i) {lines(n, 1-(1-i)^n, type="o", col=i*10)}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM