3
$\begingroup$

I'm new at Mathematica and am playing with some simple calculus to try to understand about using Manipulate.

Here's my code:

Manipulate[
 Plot[{a x^2 + b x + c, b + 2 a x, 2 a}, {x, -5, 5}, 
  PlotRange -> {{-10, 10}, {-10, 10}}, 
  PlotLegends -> "Expressions"], {a, -5, 5}, {b, -5, 5}, {c, -5, 5}]

When I execute the function, instead of getting the "a x^2 + b x + c" as an Expression label, I get FE`a$$295 x^2 +...

I get similar expression labels for the other two functions.

What does this indicate and how should i code this to get actual labels for PlotLegends and not this garbage?

Thanks!

$\endgroup$
  • $\begingroup$ Anything inside a DynamicModule-type construct (which Manipulate is) will scope all variables to protect them. This is how the FE does that. You can also supply PlotLegends -> {"a" x^2 + "b" x + c, "b" + 2 "a" x, 2 "a"} instead of Nasser's answer if you want to keep your variables protected / from messing with the global scope. $\endgroup$ – b3m2a1 Mar 2 '18 at 2:14
4
$\begingroup$

This seems due to localization. Here is a quick solution (I am sure there are other ways to handle this, but this seems the easiest now). Just add LocalizeVariables->False

Manipulate[
Plot[{a x^2+b x+c,b+2 a x,2 a},{x,-5,5},
   PlotRange->{{-10,10},{-10,10}},
      PlotLegends->"Expressions"],{a,-5,5},{b,-5,5},{c,-5,5},
LocalizeVariables->False]

Mathematica graphics

$\endgroup$
4
$\begingroup$

This will show the parametric formulas:

Manipulate[
 Block[{a = a0, b = b0, c = c0},
  Plot[{a x^2 + b x + c, b + 2 a x, 2 a}, {x, -5, 5}, 
   PlotRange -> {{-10, 10}, {-10, 10}}, PlotLegends -> "Expressions"]
  ],
 {{a0, -5, "a"}, -5, 5}, {{b0, -5, "b"}, -5, 5}, {{c0, -5, "c"}, -5, 5}]

Mathematica graphics

This will show the actual formulas:

Manipulate[
 Block[{x},
  Plot[#, {x, -5, 5}, PlotRange -> {{-10, 10}, {-10, 10}}, 
     PlotLegends -> "Expressions"] &@{a x^2 + b x + c, b + 2 a x, 2 a}
  ],
 {a, -5, 5}, {b, -5, 5}, {c, -5, 5}]

Mathematica graphics

Actually, I would normally do this one with Evaluate, but it was late and I was tired:

Manipulate[
 Block[{x},
  Plot[Evaluate@{a x^2 + b x + c, b + 2 a x, 2 a}, {x, -5, 5},
     PlotRange -> {{-10, 10}, {-10, 10}}, PlotLegends -> "Expressions"]
  ],
 {a, -5, 5}, {b, -5, 5}, {c, -5, 5}]

Some further explanation: As others have mentioned, the funny FE-$$ symbols are due to localization. The localization is accomplished first by Manipulate rewriting the literal instances of the Manipulate variables in the unevaluated code of the body (first argument) with symbols like $CellContext`a$$ for the user's a and so forth. Usually, instances of a etc. in other arguments are rewritten. Basic data and code for the Manipulate are stored in an output cell. The second step is when the output cell is displayed by the Front End ("typeset" in FE jargon). The local instances of the variables are actually created: The $CellContext is changed to FE and a unique number is added after the $$ to create a (hopefully) unique symbol for the variable. If you copy and paste it, the cell expression is copied. When the new cell is typeset, this second step is repeated and new, unique localized variables are created, which let the two demos operate independently (unless variable localization has been turned off).

$\endgroup$
  • $\begingroup$ why does the second example show the numbers while the first do not? what is the difference? $\endgroup$ – Alucard Mar 2 '18 at 4:13
  • $\begingroup$ @Alucard The expressions are evaluated in the second one before Plot sees them, but not in the first....Should probably protect x in the second one. -- updated $\endgroup$ – Michael E2 Mar 2 '18 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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