1

Given is a function F1:

F1 <- function(C1,C2,C3,...,x,u_target) {  
# a lot of equations follow   
...  
u_actual - u_target
}

F1 returns the result of the very last equation

u_actual - u_target

I want to determine the value for the parameter x in a way that the result of the last equation converges to zero. With

nlm(f=F1,p=c(0),C1=C1,C2=C2,...,stepmax=0.001,ndigit=8)

I get a result, but not a satisfying one:

u_actual = 0.1316566  
u_target = 0.1

I played a lot with the arguments of the nlm command (gradtol,stepmax,iterlim etc.), but I was not able to get a better result. I also tried optim, optimize and uniroot, but was not able to get them run at all.

u and x show a negative exponential development. With decreasing x, u increases exponential. If x is zero, u results in a finite value. x also has an upper boundary, which is unknown. So I guessed it would be promising if the iteration starts at the lower boundary (zero) and increases step by step. However, whether I decrease or increase the value of stepmax, the result is not getting better.

I would appreciate any hint from the r-community.

Thank you very much.

PS: in matlab a colleague uses fsolve(@(x) F1(x,u_target,C1,C2,...),0), and it works fine.

akr
  • 11
  • 2
  • Or should I go with a simple while loop? But then the benefit of using a fast solver gets lost, don't it? – akr Sep 24 '17 at 06:35
  • I was able to get very good results with the function "uniroot". Two problems, I was not aware of: (1) in my case it is a maximization and NOT a minimization problem, but the function "nlm" searches for a minimum (sorry!); (2) the result of my function "F1" in its original form never changed its sign regardless of the value of x; however, such a change of sign is inevitable for most of the solvers I read about; so I modified "F1" to some extent, and now it works fine with "uniroot"; – akr Sep 26 '17 at 13:31

0 Answers0