Skip to main content
fixed
Source Link
Nasser
  • 156.1k
  • 12
  • 173
  • 396

Updated to handle $ showing from using module variable.

Add TrackedSymbols :> {c} and add Module

ClearAll[x,y,c]; y[x_, c_] := -x^2 + cx^2+c Manipulate[  Grid[Module[{ymax,x},   {ymax = FindMaximum[ymax=FindMaximum[{y[x, c], -11<=x<=1},x];  <= x <=Grid[  1}, x] { {str[ymax]},   {Plot[y[x, c], {x, -1, 1}]} }   ] ] ,  {c, -0.5, 0.5},  TrackedSymbols :> {c}  ] (*this function to handle $ in local variable names *) str[expr_]:=Module[{}, StringReplace[ToString[expr, FormatType->TraditionalForm], c:LetterCharacter~~"$"~~DigitCharacter..:>c]]; 

Now

Mathematica graphicsMathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things safe. Also use a Module inside Manipulate for more robust codeto add internal context. Like this:

Manipulate[ Module[{ymax,x}, Grid[{ {ymax=FindMaximum[{y[x,c],-1<=x<=1},x]}, {Plot[y[x,c],{x,-1,1}]}} ] ], {c,-0.5,0.5} ] 

And now you do not even need TrackedSymbols, but I would still use TrackedSymbols.

So rules of thumbs

  1. Use Module inside Manipulate to hide any non-control variables used inside Manipulate. (in your example, these would be x and ymax.

  2. Use TrackedSymbols to explicitly list the symbols being tracked. In your case c.

This should eliminate most of the problems.

ps. the $ showing up attached to symbols, when printing/displaying expressions from inside module is common in Mathematica. See print-expressions-using-local-variables-in-module-without-dollar-sign-is-that-p

Add TrackedSymbols :> {c}

y[x_, c_] := -x^2 + c Manipulate[  Grid[{   {ymax = FindMaximum[{y[x, c], -1 <= x <= 1}, x]}, {Plot[y[x, c], {x, -1, 1}]}} ],  {c, -0.5, 0.5},  TrackedSymbols :> {c}  ] 

Now

Mathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things safe. Also use a Module inside Manipulate for more robust code. Like this:

Manipulate[ Module[{ymax,x}, Grid[{ {ymax=FindMaximum[{y[x,c],-1<=x<=1},x]}, {Plot[y[x,c],{x,-1,1}]}} ] ], {c,-0.5,0.5} ] 

And now you do not even need TrackedSymbols, but I would still use TrackedSymbols.

So rules of thumbs

  1. Use Module inside Manipulate to hide any non-control variables used inside Manipulate. (in your example, these would be x and ymax.

  2. Use TrackedSymbols to explicitly list the symbols being tracked. In your case c.

This should eliminate most of the problems.

Updated to handle $ showing from using module variable.

Add TrackedSymbols :> {c} and add Module

ClearAll[x,y,c]; y[x_,c_]:=-x^2+c Manipulate[ Module[{ymax,x}, ymax=FindMaximum[{y[x,c],-1<=x<=1},x];  Grid[   { {str[ymax]},   {Plot[y[x,c],{x,-1,1}]} }   ] ] , {c,-0.5,0.5}, TrackedSymbols:>{c} ] (*this function to handle $ in local variable names *) str[expr_]:=Module[{}, StringReplace[ToString[expr, FormatType->TraditionalForm], c:LetterCharacter~~"$"~~DigitCharacter..:>c]]; 

Now

Mathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things safe. Also use a Module inside Manipulate to add internal context. Like this:

So rules of thumbs

  1. Use Module inside Manipulate to hide any non-control variables used inside Manipulate. (in your example, these would be x and ymax.

  2. Use TrackedSymbols to explicitly list the symbols being tracked. In your case c.

This should eliminate most of the problems.

ps. the $ showing up attached to symbols, when printing/displaying expressions from inside module is common in Mathematica. See print-expressions-using-local-variables-in-module-without-dollar-sign-is-that-p

clarify
Source Link
Nasser
  • 156.1k
  • 12
  • 173
  • 396

Add TrackedSymbols :> {c}

y[x_, c_] := -x^2 + c Manipulate[ Grid[{ {ymax = FindMaximum[{y[x, c], -1 <= x <= 1}, x]}, {Plot[y[x, c], {x, -1, 1}]}} ], {c, -0.5, 0.5}, TrackedSymbols :> {c} ] 

Now

Mathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things savesafe. Also use a ModuleModule inside ManipulateManipulate for more robust code. Like this:

Manipulate[ Module[{ymax,x}, Grid[{ {ymax=FindMaximum[{y[x,c],-1<=x<=1},x]}, {Plot[y[x,c],{x,-1,1}]}} ] ], {c,-0.5,0.5} ] 

And now you do not even need TrackedSymbols, but I would still use TrackedSymbols.

So rules of thumbs

  1. Use Module inside Manipulate to hide any non-control variables used inside Manipulate. (in your example, these would be x and ymax.

  2. Use TrackedSymbols to explicitly list the symbols being tracked. In your case c.

This should eliminate most of the problems.

Add TrackedSymbols :> {c}

y[x_, c_] := -x^2 + c Manipulate[ Grid[{ {ymax = FindMaximum[{y[x, c], -1 <= x <= 1}, x]}, {Plot[y[x, c], {x, -1, 1}]}} ], {c, -0.5, 0.5}, TrackedSymbols :> {c} ] 

Now

Mathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things save. Also use a Module inside Manipulate for more robust code. Like this:

Add TrackedSymbols :> {c}

y[x_, c_] := -x^2 + c Manipulate[ Grid[{ {ymax = FindMaximum[{y[x, c], -1 <= x <= 1}, x]}, {Plot[y[x, c], {x, -1, 1}]}} ], {c, -0.5, 0.5}, TrackedSymbols :> {c} ] 

Now

Mathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things safe. Also use a Module inside Manipulate for more robust code. Like this:

Manipulate[ Module[{ymax,x}, Grid[{ {ymax=FindMaximum[{y[x,c],-1<=x<=1},x]}, {Plot[y[x,c],{x,-1,1}]}} ] ], {c,-0.5,0.5} ] 

And now you do not even need TrackedSymbols, but I would still use TrackedSymbols.

So rules of thumbs

  1. Use Module inside Manipulate to hide any non-control variables used inside Manipulate. (in your example, these would be x and ymax.

  2. Use TrackedSymbols to explicitly list the symbols being tracked. In your case c.

This should eliminate most of the problems.

Source Link
Nasser
  • 156.1k
  • 12
  • 173
  • 396

Add TrackedSymbols :> {c}

y[x_, c_] := -x^2 + c Manipulate[ Grid[{ {ymax = FindMaximum[{y[x, c], -1 <= x <= 1}, x]}, {Plot[y[x, c], {x, -1, 1}]}} ], {c, -0.5, 0.5}, TrackedSymbols :> {c} ] 

Now

Mathematica graphics

Manipulate by default will track all symbols that shows inside it. So when you typed ymax, frontend tracked this symbols and Manipulate went and re-evaulated its expression again, and that is why you got 0.5.

As a rule of thumb, I always use TrackedSymbols. This keep things save. Also use a Module inside Manipulate for more robust code. Like this: