#   rm(list=ls()) ## Often useful to clean things out when starting, but not in vignette
   cat("nls tests on Hobbs weeds problem\n")
   tdata<-read.table("../DATA/weeds.dat",header=TRUE)
   t<-tdata$t
   y<-tdata$y
   rm(tdata) # cleanup

   cat("Test with original 3-parameter model, unscaled parameters -- good starting parameters\n")
   xx<-c(200,50,.3)
   q<-try(ansxx<-nls(y~x1/(1+x2*exp(-x3*t)), data=list(y,t), start=list(x1=xx[1],x2=xx[2],x3=xx[3])))
   if (class(q)  == "try-error") cat("Failed\n") else print(ansxx)

   cat("Same problem, but a very crude starting point\n")
   xy<-c(1,1,1)
   q<-try(ansxy<-nls(y~x1/(1+x2*exp(-x3*t)), data=list(y,t), start=list(x1=xy[1],x2=xy[2],x3=xy[3])))
   if (class(q)  == "try-error") cat("Failed\n") else  print(ansxy)

   cat("Scaled problem, and a crude but not random starting point\n")
   xx<-c(2,1,1)
   q<-try(ansxxs<-nls(y~100*x1/(1+10*x2*exp(-0.1*x3*t)), data=list(y,t), start=list(x1=xx[1],x2=xx[2],x3=xx[3])))
   if (class(q)  == "try-error") cat("Failed\n") else  print(ansxxs)

# ========== loss functions ===============
   hobbs.f<-function(x){ # Unscaled Hobbs weeds problem -- function
    if (abs(12*x[3]) > 50) { # check computability
       fbad<-.Machine$double.xmax
       return(fbad)
    }
    res<-hobbs.res(x)
    f<-sum(res*res)
   }
hobbs.res<-function(x){ # Unscaled Hobbs weeds problem -- residual
    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")
    y<-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)
    t<-1:12
    res<-x[1]/(1+x[2]*exp(-1*x[3]*t)) - y
}
   hobbsl.f<-function(xlog){ # Unscaled Hobbs weeds problem -- function
    x<-exp(xlog) # exponentiate parameters
    if (abs(12*x[3]) > 50) { # check computability
       fbad<-.Machine$double.xmax
       return(fbad)
    }
    res<-hobbs.res(x)
    f<-sum(res*res)
   }
# ========== end loss functions ===============
   # Change trace=0 to trace=1 for progress monitoring
   cat("For comparison, try Nelder-Mead on the failed case\n")
   fval<-hobbs.f(c(1,1,1))
   cat("Initial hobbs.f function at c(1,1,1) =",fval,"\n")
    anm1<-optim(c(1,1,1), hobbs.f, control=list(trace=0))
    anm1

   cat("And again, try Nelder-Mead on the failed case but in log form\n")
   fval<-hobbsl.f(c(0,0,0))
   cat("Initial log form hobbsl.f function at c(0,0,0) =",fval,"\n")
    anml1<-optim(c(0,0,0), hobbsl.f, control=list(trace=0))
    anml1
    print(exp(anml1$par))

   cat("And again, try Nelder-Mead on the failed case but scaled start\n")
   fval<-hobbs.f(c(100,10,0.1))
   cat("Initial hobbs.f function at c(100,10,0.1) =",fval,"\n")
    anm2<-optim(c(100,10,0.1), hobbs.f, control=list(trace=0))
    anm2
