Home > other >  How to solve the following 3-dimensional system of non-linear inequalities in R?
How to solve the following 3-dimensional system of non-linear inequalities in R?

Time:12-09

I need to solve the following system of non-linear inequalities:

x*y >= 420
x*z >= 14
x*y*z < 5000

I have tried to find a similar question/solution or package that helps, but I struggle to apply it to that specific case.

The expected outcome should be a list of tuples (x,y,z). In the end, a 3-dimensional plot would be awesome but not really necessary (though should be easy to do it as soon as the solution list exists).

CodePudding user response:

Here is a way.
Reverse the >= inequalities and use the nleqslv solver.

library(nleqslv)

# x.y >= 420
# x.z >= 14
# x.y.z < 5000

fun <- function(x) {
  y <- numeric(3)
  y[1] <- 420 - x[1] * x[2]
  y[2] <- 14 - x[1] * x[3]
  y[3] <- prod(x) - 5000
  y
}

xstart <- c(1, 1, 1)
sol <- nleqslv(xstart, fun, method = "Newton")
cbind(x = sol$x, y = sol$fvec)
#>              x             y
#> [1,]   1.17600  8.154757e-10
#> [2,] 357.14286  2.486900e-14
#> [3,]  11.90476 -8.772076e-09

Created on 2022-12-08 with reprex v2.0.2

The second column of the results matrix above shows how close the solutions are from the conditions.

sol <- nleqslv(xstart, fun, method = "Newton")

x <- sol$x[1]
y <- sol$x[2]
z <- sol$x[3]

x*y - 420
#> [1] -8.154757e-10
x*z - 14
#> [1] -2.4869e-14
x*y*z - 5000
#> [1] -8.772076e-09

Created on 2022-12-08 with reprex v2.0.2

CodePudding user response:

Z = 1.19047619

x= 11.76

y=35.71

those result are correct you can check the equations i did that on a piece of paper and could synthticaly explain you how to reproduce those calculs using rstudio if you still active on this forum

NB: i think it is 500 not 5000 it change npthing to the exercice but make more sens in the number for a college exercice

  • Related