# [R] Relatively Simple Maximization Using Optim Doesnt Optimize

Abby Spurdle @purd|e@@ @end|ng |rom gm@||@com
Sat Mar 14 05:09:26 CET 2020

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

Hi Duncan,

I need to apologize.
The problem has nothing to do with little ripples.

I tried approximating the function with a cubic Hermite spline.
(Essentially smoothing the function).

However, the optim function still returns the wrong result.
Which surprised me...

Then I tried changing the max number of iterations, and found
something quite interesting:
---------
production.wr <- function(L){
cat (L, "\n")
budget=100000
Lcost=12
Kcost=15
K=(budget-L*Lcost)/Kcost
machines=0.05*L^(2/3)*K^(1/3)
return(machines)
}

S1=optim(1001,production.wr,method="CG",control=list(fnscale=-1, maxit=1))
S1=optim(1001,production.wr,method="CG",control=list(fnscale=-1, maxit=2))
S1=optim(1001,production.wr,method="CG",control=list(fnscale=-1, maxit=3))
S1=optim(1001,production.wr,method="CG",control=list(fnscale=-1, maxit=4))
---------

The first iteration calls the function (3 + 2) times.
Subsequent iterations call the function (2 + 2) times.
In the subset-of-3, the step size is exactly 0.001.
And in subsequent leading (but not trailing) subsets-of-2, the step
size is exactly 0.002.

I was wondering (hypothetically) if the first iteration is
approximating the second derivative, and subsequent iterations are
not...???

```