[R] PS to Taking Integral and Optimization using Integrate() and Optim()

2011-06-08 Thread MARYAM ZOLGHADR
Hello again.
Thank you for the comments. I have written these codes.

iy=function(x)
{
res=NULL
ress=0
for (i in (1:2))
{
for (xx in x[i])
{
fy=function(y) 
(exp(-exp(y+log(xx)))*(-exp(y+log(xx)))^2)/(1-exp(-exp(y+log(xx
res=c(res,integrate(fy,-6.907,-1.246)$value)
ress=ress+res
}
}
return(ress)
}
iy(c(1,1))
integrate(fy,-6.907,-1.246)$value

In 1D optimize() works perfectly on iy(). However the problem is in 2D and 
more, optimize() does not work and I need to apply optim(). I could not apply 
optim() on iy().

Beside, I tried to use Ryacas, I faced with this error:
[1] Starting Yacas!
Error in socketConnection(host = 127.0.0.1, port = 9734, server = FALSE, :
cannot open the connection
In addition: Warning message:
In socketConnection(host = 127.0.0.1, port = 9734, server = FALSE, :
127.0.0.1:9734 cannot be opened

Cheers,
Maryam

[[alternative HTML version deleted]]

__
R-help@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.


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

2011-06-06 Thread MARYAM ZOLGHADR
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.287653*x))-0.5*ln(exp(0.287653*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.287653*x))-0.5*log(exp(0.287653*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]]

__
R-help@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.