Plot function from R

7 visualizzazioni (ultimi 30 giorni)
Rebecca Lee
Rebecca Lee il 25 Apr 2021
Commentato: Rebecca Lee il 28 Apr 2021
Hi,
I want to plot the following function that I wrote in R. Since I am completely new to matlab, I am struggling with basically everything, for example how to get matlab to define and then use the variables p_star etc. The other variables, ulL, urL, urR, urL, r, p are fixed and I know that I need to include them with "global" in the function.
Any help is appreciated!
ExpRev <- function(v) { c=v[1]; lambda=v[2];
p_star = ((ulL * r + c) / (r * (ulL - ulR) + (urR - ulR) * lambda))
p_bar_star = ((ulL - urL) * lambda - urL * r - c) / (r * (urR - urL) + (ulL - urL) * lambda)
pstar = (ulL*r+c)/((urR*r+c)+(ulL*r+c))
p_ <- function(p) {
V_env <- function(p) ( -(c/r)*(1-p)+((urR*lambda-c)/(lambda+r))*p+((lambda*(c+ulL*r))/(r*(lambda+r)))*((((ulL * r + c) / (r * (ulL - ulR) + (urR - ulR) * lambda))/(1-((ulL * r + c) / (r * (ulL - ulR) + (urR - ulR) * lambda))))^(r/lambda))*(((1-p)/p)^(r/lambda))*(1-p) ) - ( -(c/r)*p+((ulL*lambda-c)/(lambda+r))*(1-p)+(lambda/(r*(2*r+lambda)))*((lambda*(urR*r+c))/(lambda+r))*((((1-((ulL*r+c)/((urR*r+c)+(ulL*r+c))))/((ulL*r+c)/((urR*r+c)+(ulL*r+c))))*(p/(1-p)))^(r/lambda))*p )
e <- try( d <- uniroot(V_env, c(0,1)), silent = TRUE )
if (class(e) == "try-error") {
return(1)
} else {
return(as.vector(unlist(uniroot(V_env, c(0,1))[1])))
}
}
p_ <- p_(p)
p_bar <- function(p) {
V_bar_env <- function(p) -(c/r)*(1-p)+((urR*lambda-c)/(lambda+r))*p + (lambda/(r*(2*r+lambda)))*((lambda*(ulL*r+c))/(lambda+r))*(((((ulL*r+c)/((urR*r+c)+(ulL*r+c)))/(1-((ulL*r+c)/((urR*r+c)+(ulL*r+c)))))*((1-p)/p))^(r/lambda))*(1-p) - (-(c/r)*p + ((ulL*lambda-c)/(lambda+r))*(1-p) + ((lambda*(c+urR*r))/(r*(lambda+r))) * (((1-(((ulL - urL) * lambda - urL * r - c) / (r * (urR - urL) + (ulL - urL) * lambda)))/(((ulL - urL) * lambda - urL * r - c) / (r * (urR - urL) + (ulL - urL) * lambda)))^(r/lambda)) * ((p/(1-p))^(r/lambda))*p)
e <- try( d <- uniroot(V_bar_env, c(0,1)), silent = TRUE )
if (class(e) == "try-error") {
return(0)
} else {
return(as.vector(unlist(uniroot(V_bar_env, c(0,1))[1])))
}
}
p_bar <- p_bar(p)
if (p<=p_star | p>=p_bar_star) {
ER = 0
} else if (p>p_star && p<p_ && p<pstar) {
ER = 1/lambda * ((p-p_star)/(1-p_star) + (1-p)*log(p/(1-p)*(1-p_star)/p_star)) * c
} else if (p>=p_ && p<pstar) {
ER = (1/lambda * ((pstar-p)/pstar + p*log(pstar/(1-pstar)*(1-p)/p)) + p/pstar * 2/lambda ) * c
} else if (p == pstar) {
ER = 2/lambda * c
} else if (p>pstar && p<= p_bar) {
ER = (1/lambda * ((p-pstar)/(1-pstar) + (1-p)*log(p/(1-p)*(1-pstar)/pstar)) + (1-p)/(1-pstar) * 2/lambda ) * c
} else if (p>p_bar && p<p_bar_star && p>pstar) {
ER = 1/lambda * ((p_bar_star-p)/p_bar_star + p*log(p_bar_star/(1-p_bar_star)*(1-p)/p)) * c
}
}

Risposte (1)

Shubham Khatri
Shubham Khatri il 28 Apr 2021

Categorie

Scopri di più su Graphics Objects in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by