Dear list,

Here is a small example code that use optim and optimize in order to fit
two functions.
Is it possible to fit two functions (like those two for example) at the
same time using optim ... or another function in R ?

Thanks

Arnaud

######################################################################
## function 1
x1 <- 1:100
y1 <- 5.468 * x + 3 # + rnorm(100,0, 10)
dfxy <- cbind(x1,y1)

# Objective function
optfunc <- function(x, dfxy){
  a <- x[1]
  b <- x[2]
  xtest <- dfxy[,1]
  yobs <- dfxy[,2]
  ysim <- a*xtest + b
  sum((ysim - yobs)^2)
}

out<- optim(par=c(0.2,5), fn=function(x){optfunc(x, dfxy)}, method =
"Nelder-Mead", hessian = F)


## function 2

x2 <- seq(0.01, 0.1, length=100)
y2 <- exp(30*x2)
dfxy2 <- cbind(x2,y2)

# objective function
optfunc2 <- function(x, dfxy){
  a <- x[1]
  xtest <- dfxy[,1]
  yobs <- dfxy[,2]
  ysim <- exp(a*xtest)
  sum((ysim - yobs)^2)
}

out<- optimize(f=function(x){optfunc2(x, dfxy2)}, interval=c(0,500))

######################################################################

        [[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.

Reply via email to