# Why DerivativeCheck would be allowed to be on while there is no GradObj?

1 view (last 30 days)

Show older comments

When GradObj is off, and DerivativeCheck is on, fmincon starts to run without any error or warning. Why would this be the case? That is, if there is no gradient supplied, there cannot be derivattive check. So why would MATLAB allow the DerivativeCheck to be on if there is no gradient available?

options = optimset(...

'MaxFunEvals',10000,...

'TolFun',1e-4,...

'TolX',1e-4,...

'Algorithm','sqp',...

'GradObj','off',...

'DerivativeCheck','on',...

'Display','iter',...

'FunValCheck','off');

##### 0 Comments

### Accepted Answer

Matt J
on 29 Jan 2023

Here's a reason why you might want to allow DerivativeCheck to be on even when the analytical gradient computations are all off. Suppose the user has coded 6 different implementations of the gradient calculation and wants to compare them all for speed. Suppose also that she wants to run the optimization with finite difference gradient computations as a baseline for the comparison.

In this situation, you would want to have DerivativeCheck='on' for 6 different runs. To run the finite differencing baseline, however, your proposal would force the user to set both SpecifyObjectiveGradient=false and DerivativeCheck='off'. Some people would prefer just to set SpecifyObjectiveGradient=false and not to have to fuss with additional settings for just that one case.

### More Answers (3)

John D'Errico
on 27 Jan 2023

Moved: Walter Roberson
on 27 Jan 2023

Matt J
on 28 Jan 2023

Edited: Matt J
on 28 Jan 2023

I would say that no warning is strictly necessary because one of two cases are possible:

(1) DerivativeCheck='on' was intentional, but GradObj='off' was unintentional. If so, you will be warned that something is wrong by the fact that no derivative check output appears.

(2) GradObj='off' was intentional, but DerivativeCheck='on' was unintentional. If so, it would be your wish that the optimization proceed without a derivative check and, indeed, that is what happens.

That said, I do agree that an explicit warning message in case (1) would be more aesthetic.

##### 6 Comments

Matt J
on 29 Jan 2023

Edited: Matt J
on 29 Jan 2023

The user sees that things still run fine with nonsense settings.

It might be overstating things slightly to say that the settings are "nonsense". The main purpose of DerivativeCheck is to prevent the optimization from using analytical gradient calculations that are faulty. However, if SpecifyObjectiveGradient is turned off, the user is not supplying the gradient, and so there is no danger this will occur. So, arguably, the purpose of DerivativeCheck has been fulfilled and the optimization should just proceed care-free.

As I mention in my other answer, it would be nice if Matlab could warn you when you shut off analytical gradient calculations accidentally, or forget to turn them back on. But that is simply not always something Matlab can detect, and in any case is not something DerivativeCheck was primarily designed to protect against.

Matt J
on 28 Jan 2023

Edited: Matt J
on 28 Jan 2023

So why would MATLAB allow the DerivativeCheck to be on if there is no gradient available?

Because maybe SpecifyConstraintGradient=true, even if SpecyObjectiveGradient=false. In your example, this is not the case. In general however, there are 4 possible combinations of settings for SpecifyObjectiveGradient and SpecifyConstraintGradient and only one of these combinations is suspicious when DerivativeCheck='on'. Therefore, there is very limited protection a warning message could provide against users accidentally turning off analytical gradient computation.

### See Also

### Categories

### Products

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!