Home > Back-end >  How to plot an equation with no analytical solution in R?
How to plot an equation with no analytical solution in R?

Time:01-29

I have been trying to plot an equation in R. The variables are fixed at (for this instance) a=0.06, b=-0.01, i=0, t=0.005, r=0.0025, s=0.015 while I intend to vary variable e. Some of the functions in the main equations are,

functions

The equation looks like this,

enter image description here

I'd ideally like a plot between Q and x as e increases.

t = 0.0005
A = 0.06
B = -0.01
r = 0.00025
s = 0.015
i=0


M= function(e) e*A - i*B
N= function(e) t/(1 t*e-t*i)
P= function(e) (s/t)*(1 N(e)*(r t))

I actually don't know how to proceed. I'd like to maybe create a list of (e, Q, x) where x is the root of the equation for a given e and then maybe use interpolation and then plot (Q, x) in R.

Is there a more direct way of plotting x vs Q? If not, could someone please help me out with this? Also, if it's easier to do with mathematica or MATLAB, although I have limited experience with both, please let me know.

CodePudding user response:

t <- 0.0005
A <- 0.06
B <- -0.01
r <- 0.00025
s <- 0.015
i<-0

Q <- function(e){
  N <-  t/(1 t*e-t*i)
  M <- e*A - i*B
  P <- (s/t)*(1 N*(r t))
  M/P
}


RHS <- function(x, e){
  N <-  t/(1 t*e-t*i)
  M <- e*A - i*B
  P <- (s/t)*(1 N*(r t))
  Q <- M/P
  V <- exp(-(r 2*N)/x/r/N)
  exp(-1/(x*N))    V* (0.5-Q)   Q*V^2
}

LHS <- function(e){
  1 - Q(e)
}


x <- function(e){
  suppressWarnings(sapply(e, \(u)
         optimise(\(x) (RHS(x,u) - LHS(u))^2, 
                  c(-100000, 100000))$min))
}


# assume 
e <- 0.1
LHS(e)
RHS(x(e), e)


#plot:

e <- seq(0, 1000)
plot(x(e), Q(e), ty='l')
  •  Tags:  
  • Related