[R] Taking Integral and Optimization using Integrate, Optim and maxNR

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Jun 7 17:38:15 CEST 2011



On 06.06.2011 20:14, MARYAM ZOLGHADR wrote:
> Dear All, Hello!
>
> I have some questoins in R programming as follows:
>
> Question 1- How to take the integral of this function with respect to y, such that x would appear in the output after taking integral.
>
> f(x,y)=(0.1766*exp(-exp(y+lnx))*-exp(y+lnx))/(1-exp(-exp(y+lnx))) y in (-6.907,-1.246)
>
> It is doable in maple but not in R. At least I could not find the way.
>
> p.s: result from maple is:
>
> g(x)=dilog*exp(0.001000755564*x)+0.5*ln(exp(0.001000755564*x))^2-dilog*(exp(0.2876531111*x))-0.5*ln(exp(0.2876531111*x))^2
>
> Where dilog=integral(log(t)/(1-t)) for t in (1,x)
>
>
>
> Question 2- Then I want to optimize (maximize) the answer of the above integral for x. Assum x in (0,100)
>
> The answer should be something between 26 and 27.
>
>
>
> What I did is: got answer of integral(f(x,y)) from maple which is g(x), and then applied it in R. The code is as follows:
>
> ##In the case n=1
>
> library(stats)
> require(graphics)
> integrand=function(t){(log(t))/(1-t)}
> #dilog=integrate(integrand, lower=1, upper=x)
> fr<- function(x){(integrate(integrand, lower=1, upper=x)$value)*(exp(0.001000755564*x))+0.5*log(exp(0.001000755564*x))^2-(integrate(integrand, lower=1, upper=x)$value)*(exp(0.2876531111*x))-0.5*log(exp(0.2876531111*x))^2}
>
> optim(20, fr, NULL, method = "BFGS")
>
>
>
> Question 2-1-Default by optim is to minimize a function, how I can use it to maximization?


If you want to maximize f, either minimize -f or read ?optim and find 
that it tells us:
"By default this function performs minimization, but it will maximize if 
control$fnscale is negative. "


> Question 2-2- The above code faced with errors, and did not work. What I guess is there is something wrong with taking integral. The output of integrate function in R is some sort of thing. I had to somehow tell it, just take the value and forget about the others. But I guess it is still something wrong with it. I also tried maxNR, but is didn't work either.

If you integrate  log(t)/(1-t) from t=1 (as you specified in your 
code!), then at t=1 the value is undefined ...


Uwe Ligges



> Question 2-3- Thoes above are the easiest case of my problem. Assume the case that I have summation of f(x1,y)+f(x2,y)+...+f(x12,y)
>
>
>
> The article have done it by E04JAF-NAG Fortran Library Routine Document. But I want to do it in R.
>
> Thanx all.
>
>
>
> Cheers,
>
> Maryam
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list