I am looking for a fixed point x
when f(x)=x
of a function, ofcourse numerically, but I have no idea how to solve it with R, I am trying with fsolve
with following code, but possibly its not the right way to write this...I am not getting anything...Many thanks in advance
library(pracma)
p<- c(x=0.1, y=0.1)
f1 <- function(p) {
expPfLeft<- (160*p[1]) + ((1-p[1])*200)
expPfRight<- (10*p[1])+ ((1-p[1])*370)
expPfTop <- (200*p[2]) + ((1-p[2])*160)
expPfBottom <- (370*p[2]) + ((1-p[2])*10)
return(c (exp(2*expPfLeft)/((exp(2*expPfLeft)+exp(2*expPfRight))) ,
exp(2*expPfTop)/((exp(2*expPfTop))+(exp(2*expPfBottom))) ) )
}
fsolve(f1,p)
Assuming your function is defined correctly. You are looking for f(p[1], p[2]) = c(p[1], p[2])
You can create fix your answer by changing the return statement to:
return(c(exp(2*expPfLeft)/((exp(2*expPfLeft)+exp(2*expPfRight))), exp(2*expPfTop)/((exp(2*expPfTop))+(exp(2*expPfBottom)))) - p)
We can observe that your optimization function are actually two independent functions. p[1]
is used in Left and Right. p[2]
is used in Top and Bottom.
We can separate your functions.
f.x <- function(p) {
expPfLeft<- (160*p) + ((1-p)*200)
expPfRight<- (10*p)+ ((1-p)*370)
return(exp(2*expPfLeft)/((exp(2*expPfLeft)+exp(2*expPfRight))) - p)
}
f.y <- function(p) {
expPfTop <- (200*p) + ((1-p)*160)
expPfBottom <- (370*p) + ((1-p)*10)
return(exp(2*expPfTop)/((exp(2*expPfTop))+(exp(2*expPfBottom))) - p)
}
Simplifying your expression so we can cheat a little for the starting value
We can see that there is only one approximate approximate solution at x = 1.
fsolve(f.x, 1)
$x
[1] 1
$fval
[1] 0
And similarly for the second function, there is a solution at 0.4689.
fsolve(f.y, 0.1)
$x
[1] 0.4689443
$fval
[1] 4.266803e-09
Doing all this defeats the purpose of optimization, and leads me to believe that your optimization function is mis-defined.