Probably this has been asked before, but I could not find an answer. I am trying to create an interactive applet using "Manipulate" that works with a user defined function. I would like to have an input field in the control part of "manipulate" and work with the expression the user enters in the input field. For example,

Manipulate[Plot[xx, {t, 0, 1}], {{xx, t^2, "x(t)="}}]

plots the graph of the expression entered in the input field, but I don't know how to modify this to graph the derivative. Any help would be appreciated.

  • $\begingroup$ Manipulate[Plot[Evaluate@{xx, D[xx, t]}, {t, 0, 1}], {{xx, t^2, "x(t)="}}] $\endgroup$
    – corey979
    Jan 3, 2017 at 11:38

3 Answers 3


It would be nice to have a "bullet-proof" Manipulate[] that does not depend on, or is not affected by, global values, such as one for t. There are a few problems, challenges and requirements:

  1. The use of Global`t in the Manipulate code means users can interfere with its operation by setting t equal to something.
  2. More significantly, InputField does not localize symbols. One has to do one's own localization.
  3. Another requirement I sought was to be able to display the localized t as t, in both StandardForm and TraditionalForm.
  4. A bigger challenge is that Manipulate rather aggressively rewrites the user's code to remap symbolic expressions in terms of localized expressions (e.g. in terms of $CellContext`t$$, which is later converted to FE`t$$nnn), while sometimes allowing global values of t to slip in. It seems bigger to me, because I have been unable to predict with 100% confidence when global t gets evaluated in the process of building the DynamicModule[] output of Manipulate[].

Well, with a bit of luck, I've got it down to the following:

t = 4;  (* to try to cause trouble *)

(* periphrasis needed, to get around Manipulate's remapping of Symbol["t"]
   Fails: PlotLabel -> ({f, df} /. HoldPattern[t] :> Symbol["t"]) *)
 With[{f = ReleaseHold[
     xx /. HoldPattern[tt_ /; MatchQ[tt, Symbol["t"]]] :> t
  With[{df = D[f, t]},                (* @lowriniak's idea *)
   (* other code as needed *)
   Plot[df, {t, 0, 1}, PlotLabel -> ({f, df})]
 {{xx, Unevaluated[t^2], "x(t)="},    (* Manipulate does not localize initial value *)
  InputField[#, Hold[Expression]] &}, (* @Nasser's String method can be adapted, too *)
 {{t, Unevaluated[t]},                (* Manipulate does not localize initial value *)
  ControlType -> None},
 Initialization :>
  (MakeBoxes[t, form_] := FormBox["t", form])  (* Otherwise get things like FE`t$$959 *)

Mathematica graphics


When you plot an expression, Plot substitutes in the value of the variable (in your case t) so you cannot do the derivative in the Plot (as t will no longer be a symbol, but the number substituted in). To get around this you can do the derivative outside the plot, like this:

    {der = D[xx, t]}, 
    Plot[der, {t, 0, 1}]
  {{xx, t^2, "x(t)="}}

Note With is used here to scope the variable der.


You can use InputField as string, then use ToExpression to convert to Mathematica expression, then use D to take the derivative.

enter image description here

 g = ToExpression[f];
 Plot[{g, Evaluate[D[g, x]]}, {x, -2 Pi, 2 Pi}, 
  PlotLegends -> {g, "its derivative"}],
 {f, Sin[x], InputField[#, String] &},
 Button["Do it", tick = Not[tick]],
 {{f, "Sin[x]"}, None},
 {{tick, False}, None},
 TrackedSymbols :> {tick}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.