# [R] optim

Remigijus Lapinskas remigijus.lapinskas at maf.vu.lt
Fri Feb 28 09:55:03 CET 2003

```Dear all,

I have a function MYFUN which depends on 3 positive parameters TETA[1],
TETA[2], and TETA[3]; x belongs to [0,1].
I integrate the function over [0,0.1], [0.1,0.2] and
[0.2,0.3] and want to choose the three parameters so that
these three integrals are as close to, resp., 2300, 4600 and 5800 as
possible. As I have three equations with three unknowns, I expect the
exact fit, i.e., the SS (see below) to be zero. However, the optim
function never gives me what I expect, the minimal SS value(=res\$value)
never comes close to zero, the estimates of the parameters, res\$par,
wildly depends on init etc.
I would be grateful for any comments on this miserable situation.

aa <- c(2300,4600,5800)
init <- c(2.5,8000,0.84) # initial values of parameters
print(init)
###################
myfun <- function(x,TETA) TETA[2]*(((1-x)^(-1/TETA[3]))-
1)^(1/TETA[1])
###################
x <- seq(0,0.3,by=0.01)
plot(x,myfun(x,init),type="l")
###################
LSS <- function(teta,aa)
{
integr <- numeric(3)
for(i in 1:3)
{integr[i] <- 10*integrate(myfun,
lower=(i-1)/10,upper=i/10,TETA=teta)\$value
}
SS <- sum((integr-aa)^2) # SS=Sum of Squares
SS
}
####################
res <- optim(init,LSS,aa=aa,
method = "L-BFGS-B",lower=c(0,0,0.5))
print(res\$par)
print(res\$value)

> source("C:/Program Files/R/integral.R")
[1]    2.50      7000.00         0.84        # initial
[1]    2.3487221 6999.9999823    0.5623628   # final
[1] 75613.05                                 # minSS
> source("C:/Program Files/R/integral.R")
[1]     2.5      15000            0.84       # initial
[1]     2.125804 14999.999747     2.241179   # final
[1] 50066.35                                 # minSS

Best regards,
Remigijus                          mailto:remigijus.lapinskas at maf.vu.lt

```