I have a function:
f(x1, x2) = (x2-x1)/(c-x1),
where 0<x1,x2<1 and c = 0, 1
Now I need to optimize the function in this way where f(x1, x2) will stay in the range [-1, 1]. I am trying to solve this using the following R code.
require("stats")
# c=0
f <- function(x) { (x[2] - x[1]) / (0 - x[1]) }
initial_x <- c(0.1, 0.1)
x_optimal <- optim(initial_x, f, method="CG")
x_min <- x_optimal$par
x_min
x_optimal$value
# c=1
f <- function(x) { (x[2] - x[1]) / (1 - x[1]) }
initial_x <- c(0.1, 0.1)
x_optimal <- optim(initial_x, f, method="CG")
x_min <- x_optimal$par
x_min
x_optimal$value
But it is not working. Could anyone help me to solve this? Thanks in advance.