Ok, we eventually found the bug. The culprit was minval being used inside the objective function. One of our developers was using the second argument as the length of the array. This is hard to detect. The second argument of minval is the dimension not the size. Thanks all for your input.