Essentially I agree with Ernesto.
In addition there is more work that you can save.
The gradient has a lot of computation in common with the function evaluation in your problem.
You can save a lot of work if you use the following principle: The gradient is only evaluated at points in which the function have
been just evaluated.
In fact the principle is not exactly true. It is true when (a) you use the Hessians (code evalh)) and (b) The last functional value
is the smallest one so far obtained.
You can pass this information by common betwen evalf and evalg.
Moreover, the same information, namely, the indexes at which ai^t x is out of the interval, can be saved in evalf and passed
by common to evalh.
The information about the smallest value of f can be stored internally to evalf. I mean that, in evalf you can always compare
the new functional value com;puted with the best one so far computed and send, by common to evalg, the information
that the new is the best or not.
Essentially, if you save all this work, the computer time will be halved.
Regards,
Mario