Once again, CG and its successors aren't envisaged for 1D problems. Do you really want to perform brain surgery with a chain saw?
Note that production4 <- function(L) { - production3(L) } sjn2 <- optimize(production3, c(900, 1100)) sjn2 gives $minimum [1] 900.0001 $objective [1] 84.44156 Whether that is a good optimum I haven't checked. JN On 2020-03-13 6:47 a.m., Duncan Murdoch wrote: > On 12/03/2020 8:52 p.m., Abby Spurdle wrote: >>> L= 1006.536 >>> L= 1006.537 >>> L= 1006.535 >>> It appears to have chosen step size 0.001, not 0.00001. It should be >>> getting adequate accuracy in both 1st and 2nd derivatives. >>> Those little ripples you see in the plot are not relevant. >> >> I'm impressed. >> But you're still wrong. >> >> Try this: >> --------- >> #not good R code! >> v = numeric () >> >> production3 <- function(L){ >> #store in vector >> v <<- c (v, L) >> >> budget=100000 >> Lcost=12 >> Kcost=15 >> K=(budget-L*Lcost)/Kcost >> machines=0.05*L^(2/3)*K^(1/3) >> return(machines) >> } >> >> optim.sol <- optim (1001, production3 ,method="CG", control = >> list(fnscale=-1) ) >> >> n = length (v) >> print (n) >> >> plot (1:n ,v, type="l") >> --------- >> >> After 401 iterations (on my computer), the algorithm hasn't converged. >> And I note it's converging extremely slowly, so I don't see any >> argument for increasing the number of iterations. >> >> And try this: >> (The first 30 steps). >> --------- >> plot (1:30 ,v [1:30], type="l") >> --------- >> >> Little ripples aren't going anywhere... >> > > That's the bug. > > It is correctly signalling that it hasn't converged (look at > optim.sol$convergence, which "indicates that the iteration > limit maxit had been reached".) But CG should be taking bigger steps. On a > 1D quadratic objective function with no > errors in the derivatives, it should take one step to convergence. Here > we're not quadratic (though it's pretty close), > and we don't have exact derivatives (your ripples), so the fact that it is > sticking to one step size is a sign that it > is not working. If those ripples are big enough to matter (and I'm not > convinced of that), it should take highly > variable steps. > > The fact that it doesn't give a warning() when it knows it has failed to > converge is also a pretty serious design flaw. > > Duncan Murdoch > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.