I think what you need is ContinuousAction -> False
and TrackedSymbols -> vars
:
Foobar[] :=
Module[{f, vars, grad}, f = a x^3 - b x^2 + c y^2;
vars = {a, b, c};
With[{fman = f, controls = Apply[Sequence, {{#, 0}, -5, 5} & /@ vars]},
Manipulate[
g = fman/(x^2 + y^2 + 1);
grad = D[g, {{x, y}}];
Print[grad];
, controls, ContinuousAction -> False, TrackedSymbols -> vars]
]];
ContinuousAction -> False
is needed to keep from evaluating continuously while dragging the sliders. In the original version this happened and the Print
statements were sent to the Messages window.
TrackedSymbols -> vars
is needed to keep Mathematica from reevaluating when the values of g
or grad
change (which would be necessary if these were interdependent).