lhobbs.f<-function(xl,y){ # # Scaled Hobbs weeds problem -- function
    if (abs(12*exp(xl[3])) > 50) { # check computability
       fbad<-.Machine$double.xmax
       return(fbad)
    }
    res<-lhobbs.res(xl,y)
    f<-sum(res*res)
}


#y0 <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 38.558, 50.156, 62.948,
#        75.995, 91.972)

lhobbs.res<-function(xl,y){ # log scaled Hobbs weeds problem -- residual
# base parameters on log(x)
    x<-exp(xl)
# This variant uses looping
    if (abs(12*x[3]) > 50) { # check computability
       rbad<-rep(.Machine$double.xmax, length(x))
       return(rbad)
    }
    if(length(x) != 3) stop("hobbs.res -- parameter vector n!=3")
    t<-1:length(y)
    res<-x[1]/(1+x[2]*exp(-x[3]*t)) - y
}

lhobbs.jac<-function(xl, y) { # scaled Hobbs weeds problem -- Jacobian
    x<-exp(xl)
    jj<-matrix(0.0, 12, 3)
    t<-1:12
    yy<-exp(-x[3]*t)
    zz<-1/(1+x[2]*yy)
    jj[t,1] <- zz*exp(xl[1])
    jj[t,2] <- -x[1]*zz*zz*yy*exp(xl[2])
    jj[t,3] <- x[1]*zz*zz*yy*x[2]*t*exp(xl[3])
    return(jj)
}


lhobbs.g <- function(xl,y) { # scaled Hobbs weeds problem -- gradient
   shj<-lhobbs.jac(xl,y)
   shres<-lhobbs.res(xl,y)
   shg<-as.vector(2.0* (shres %*% shj))
   # can we use crossprod??
   return(shg)
}
