MARYAM ZOLGHADR
2011-Jun-06 18:14 UTC
[R] Taking Integral and Optimization using Integrate, Optim and maxNR
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? 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. 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]]
Uwe Ligges
2011-Jun-07 15:38 UTC
[R] Taking Integral and Optimization using Integrate, Optim and maxNR
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.
Berend Hasselman
2011-Jun-07 18:40 UTC
[R] Taking Integral and Optimization using Integrate, Optim and maxNR
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? 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. /quote> The integral of dilog (should) exist (according to the books). dilog <- function(t) log(t)/(1-t) integrate(dilog,1,2) -0.822467 with absolute error < 9.1e-15 Now if you do: z <- integrate(dilog,1.000,2) str(z) you will see that z is a list: List of 5 $ value : num -0.822 $ abs.error : num 9.13e-15 $ subdivisions: int 1 $ message : chr "OK" $ call : language integrate(f = dilog, lower = 1, upper = 2) - attr(*, "class")= chr "integrate" If you need the result of integrate then it is standard R to use z$value to get the value of the integral. Berend -- View this message in context: http://r.789695.n4.nabble.com/Taking-Integral-and-Optimization-using-Integrate-Optim-and-maxNR-tp3577923p3580456.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- Question regarding to maxNR
- maxNR in maxLik package never stops
- maxNR - Error in p(a, b) : element 1 is empty; the part of the args list of '*' being evaluated was: (b, t)
- newton method
- Solving an integral in R gives the error “The integral is probably divergent”