Update Manipulate [] 'd when changing parameters

I struggled with the Mathematica Manipulate feature over the last few days for a project.

I am working on customizing the assumptions and boundary conditions that go into the physical model. To do this, I want to be able to build various equations and adjust parameters and update graphs on the fly. Manipulation seems like the perfect tool for the job - except that I can't get it to work. Charts will not be updated when parameters are changed.

Basic example:

a =.; b =.; c =.; func1[x_] := a*x; func2[x_] := a*x^2 + b*x + c; funcNamesList := {"Linear", "Quadratic"}; funcList := {func1[x], func2[x]} Manipulate[ Plot[function, {x, -5, 5}], {function,MapThread[Function[#1 -> #2], {funcList, funcNamesList}]}, {a, -5, 5}, {b, -5, 5}, {c, -5, 5}, LocalizeVariables -> False ] 

I can get, for example, func1 to update by pressing func1 , adjusting a , then pressing func1 again, but I hope it refreshes when I adjust a , because the real functions that I use are pretty temperamental to their parameters.

-Because I will deal with long functions that have different parameters, it is useful to use a list of functions.

EDIT:

In case it gives any ideas for everyone, here are some working examples of the individual components of what I want to do (from the Wolfram documentation):

Graph graphs and update them when changing parameters:

 Manipulate[ Plot[Sin[ax + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4}, {{b, 0, "Phase Parameter"}, 0, 10} ] 

Note. This is interrupted when the function is taken externally:

 func[x] := Sin[ax + b]; Manipulate[ Plot[func[x], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4}, {{b, 0, "Phase Parameter"}, 0, 10}, LocalizeVariables -> False ] 

Example of changing the displayed function:

 Manipulate[ Plot[f[x], {x, 0, 2 Pi}], {f, {Sin -> "sine", Cos -> "cosine", Tan -> "tangent"}} ] 

Edit 2 Func2 has been changed from a*x^2 to a*x^2 + b*x + c to reflect the fact that functions can have different parameters.

Edit 3 Added a tidbit that I use to get nice names on function buttons.

+6
source share
2 answers

There are two problems that prevent your Manipulate statement from working.

First, while the Manipulate a variable is global due to the setting of LocalizeVariables -> False , the Plot x variable is not. x is local to the Plot expression.

The second problem is that Manipulate , by default, accepts TrackedSymbols -> Full . This means that only characters explicitly displayed in the managed expression are tracked. Note that a not displayed in the expression, so it is not tracked.

Thus, we can fix both problems:

 a =.; function =.; func1[x_] := a*x; func2[x_] := a*x^2; funcList := {func1, func2} Manipulate[ Plot[function[x], {x, -5, 5}], {function, funcList}, {a, -5, 5}, LocalizeVariables -> False, TrackedSymbols :> {a, function} ] 

Changes:

  • funcList been changed to {func1, func2}
  • The Plot expression has been changed to function[x] , thereby referring to the local variable x .
  • Added Manipulate TrackedSymbols :> {a, function} option TrackedSymbols :> {a, function} .
  • function not initially set.
+7
source

I would do it a little differently:

 func1[x_, a_] := a*x; func2[x_, a_] := a*x^2; funcList = {func1, func2}; Manipulate[ Plot[Evaluate[function[x, b]], {x, -5, 5}, PlotLabel \[Rule] funcList ], {function, funcList}, {b, -5, 5} ] 

but it may not be suitable for what you want. Do your functions have different signatures?

EDIT: I renamed the parameter to b to make it clearer, it is just a passed parameter, unlike the global variable, as you used it.

+2
source

Source: https://habr.com/ru/post/895445/


All Articles