Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

r - How to find root with more than one unknown

fff5=function(x)x*31*24 * (1/(31*24))*0.30  +  400*31*24 * (1/(31*24))*0.025   +       ( (10 * 31 * 24 - 100*31*24/20 )/(31*24)  * 6 ) - 200

fff5 function describes the cost of Amazon Elastic File System where x is the Gb of storage in Standard plan for 24hours per day 31 days, 400 is the gb of storage in EFS Infrequent Access with 24 hours per day 31 days and 10 is the MB/s throughput 24 hours per day 31 days, 200 is the maximum budget.

When i do:

uniroot(fff5, lower=0, upper=1, extendInt = "yes",maxiter = 10000)$root 
[1] 533.3333

I find the highest value of GB's that can be stored in the standard plan 24 hours a day 31 days plus the cost of 400gb in the Infrequent Access and plus the cost of 10mb in the throughput with a maximum budget of 200:

fff5(533.3333)
>[1] -0.00001
> fff5(533.3334)
[1] 0.00002

How to do the same for the other two unknowns (y, z)? How to find root with more than one unknown?? How to find all the combinations of value of x y z that makes this function positive.

fff6=function(x,y,z)x*31*24 * (1/(31*24))*0.30  +  y*31*24 * (1/(31*24))*0.025   +       ( (z* 31 * 24 - 100*31*24/20 )/(31*24)  * 6 ) - 200

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The equation you propose is of the type

ax + by + cz + d = 0

that's a plan. This means that your solutions are infinite and are all points belonging to the plane defined by the equation.

Since there are infinite solutions, the only thing you can do is try to narrow the space where to look for them as much as possible.

You can choose one unknown (for example x) and treat the other two as parameters

At this point, assign reasonable values to y and z. Unfortunately I don't know what those variables indicate, but I assume they have the same order of magnitude as x found in the previous point (~ 500)

yy <- seq(400, 600, 10)
zz <- seq(400, 600, 10)

These two variables must be recombined in order to obtain a grid:

df_grid <- expand.grid(y = yy, z = zz)

ATTENTION: the longer the vectors, the heavier the calculation will be.

Now you can find the x solutions via uniroot (passing the y and z as numbers) and the solutions of your problem (within the chosen range) will be all triples x, y, z

fff6=function(x,y,z) { x*31*24 * (1/(31*24))*0.30  + 
    y*31*24 * (1/(31*24))*0.025   + 
    ( (z* 31 * 24 - 100*31*24/20 )/(31*24)  * 6 ) - 200
}

x_sol <- NULL
for (i in 1:nrow(df_grid)) {
  xs <- uniroot(fff6, c(-10000, 10000), y = df_grid$y[i], z = df_grid$z[i] )$root
  x_sol <- c(x_sol, xs)
}

df_grid$x <- x_sol

NOTE1: There are more elegant ways to avoid writing the previous for loop. For example:

x_sol <- mapply(function(y, z) uniroot(fff6, interval = c(-10000,10000), 
                               y=y, z=z)$root, df_grid$y, df_grid$z))
df_grid$x <- x_sol

NOTE2: The range I have chosen shows negative solutions (which I suspect are not useful). A possible choice for obtaining positive solutions is:

yy <- seq(100, 300, 10)
zz <- seq(10, 30, 1)

Choose to search for solutions in an appropriate range!



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...