简体   繁体   中英

How to plot function with 2 variables and involving factorials in R

I could not find a viable solution to this problem (and I am a beginner in R).

I have an equation as shown below

在此输入图像描述

where n and K are constants. a and b are the variables.

How do I generate a 2-dimensional plot for the above in R?

Thanks in advance.

factorialfunction <-function(a,b, n, K){
K*(b^a)*((2+b)^(n+a))
}


Klist = c(1,5,10,50,100,200)
nlist = c(1,5,10,50,100,200)
#note that the n and K values are recycled, make them whatever you wish, they are constants, 
#while a and b take on any values, here 100 values between zero and one
res <- mapply(factorialfunction,a = seq(.01,1,by=.01),
b=seq(.01,1,by=.01), n = rep(nlist,each = 100), K=rep(Klist, each=100))

#Then you can plot this six times.

#allow six plots on the panel if you want
par(mfrow = c(3,2))
#loop through different plots
for (i in 1:6)
plot(1:100,res[1:100 + (i-1)*100])

Note In this code I chose a and b to be between zero and one, I am not familiar with this function but It looks like some type of Beta.

You can generate more than 6 plots by changing klist and nlist and your par and for loop parameters.

Here is what you get, note this code is customizable to produce the plots for the values of n, K, a, and b that you want. 在此输入图像描述

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