I get the failure message. To be specific:
adcomp.git>R CMD BATCH --quiet test_nlminb.R
adcomp.git>cat test_nlminb.Rout
> f <- function(x) sum( log(diff(x)^2+.01) + (x[1]-1)^2 )
> opt <- nlminb(rep(0, 10), f, lower=-1, upper=3)
> xhat <- rep(1, 10)
> abs( opt$objective - f(xhat) ) < 1e-4? ## Must be TRUE
[1] FALSE
My system is described by:
adcomp.git>uname -a
Linux localhost.localdomain 4.17.7-200.fc28.x86_64 #1 SMP Tue Jul 17 16:28:31 UTC 2018 x86_64 x86_64
x86_64 GNU/Linux
My version of R is described by:
Source?????? : R-3.5.2-2.fc28.src.rpm
I have tried passing in the gradient and turning on the trace and it gives nearly the exact same
trace with and without the gradient.
Here is the output of a very similar case with the gradient:
> n??? <- 10
> f??? <- function(x) {
+?????? result <- 0.0
+?????? for( i in 2 : n ) {
+?????????????? result <- result + log( (x[i] - x[i-1])^2 + 0.01 ) + (x[1] - 1.0)^2
+?????? }
+?????? result
+ }
> g??? <- function(x) {
+?????? result <- rep(0.0, n)
+?????? for( i in 2 : n ) {
+?????????????? result[1]?? <- result[1] + 2.0 * (x[1] - 1.0)
+?????????????? log_arg???? <- ( x[i] - x[i-1] )^2 + 0.01
+?????????????? log_arg_i?? <- 2.0 * (x[i] - x[i-1])
+?????????????? result[i]?? <- result[i]?? + log_arg_i / log_arg
+?????????????? result[i-1] <- result[i-1] - log_arg_i / log_arg
+?????? }
+?????? result
+ }
> xstart <- rep(0.0, n)
> opt??? <- nlminb(
+?????? xstart,
+?????? objective=f ,
+?????? gradient=g,
+?????? lower=-3,
+?????? upper=3,
+?????? control=list(trace=1)
+ )
? 0:??? -32.446532:? 0.00000? 0.00000? 0.00000? 0.00000? 0.00000 0.00000? 0.00000? 0.00000?
0.00000? 0.00000
... snip ...
150:??? -37.750217: 0.796764 0.303221 0.285377 0.271175 0.257584 0.248540 0.239230 0.234184 0.229395
0.227872
> opt$par
?[1] 0.7967636 0.3032208 0.2853775 0.2711747 0.2575838 0.2485403 0.2392304
?[8] 0.2341841 0.2293946 0.2278722
> g(opt$par)
?[1]? 0.23427575 -0.43398577 -0.67415314 -0.11550223 -0.87486481 0.05194325
?[7] -0.83926642 -0.05100054 -0.65128392 -0.30441806
>