Problem with using Manipulate in Mathematica - wolfram-mathematica

I tried the following example, but nothing shows up.
f = a*b*c*x
Manipulate[Plot[f, {x, 0, 1}], {a, 0, 1}, {b, 0, 1}, {c, 0, 1}]
However, if I replace f in the second line by a*b*c*x directly, it works. Could anyone explain why this happens, please?
(In fact, my f could be obtained by some tedious calculation and it is a long expression, so it is inappropriate to copy-paste it into the second line.)

Thanks for Bill's detailed explanation. I think of a way to work around this with his suggestion.
g = a*b*c*x;
f[a_, b_, c_, x_] := Evaluate[g];
Manipulate[ Plot[Evaluate[g], {x, 0, 1}], {a, 0, 1}, {b, 0, 1}, {c, 0, 1}]

Related

Extending ListPointPlot3D with a 3rd variable?

I am trying to extend a function like this: (a 4x4 square)
ListPointPlot3D[Table[{x, y, 0}, {x, 0, 4, 1}, {y, 0, 4, 1}]]
into something like this: (a 4x4x4 cube)
ListPointPlot3D[Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}]]
by adding a 3rd dimension.
However, the dimensions of the latter seem to be incorrect. It seems to form a 2x2 matrix of 3d points rather than a list.
Any ideas how to fix this?
If you look a bit more closely you'll see that the expression
Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}]
returns a structure with 5x5x5 triplets. That is exactly what the expression is supposed to return. You can see this if you apply the Dimensions[] function to the returned structure.
There are several ways to turn the table into a list of 125 triplets, one is to use Flatten like this
Flatten[Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}], 2]
Or you could simply generate your list of triplets directly; for your example one alternative would be
Tuples[Range[0, 4], 3]

mathematica Plot with Manipulate shows no output

I was initially attempting visualize a 4 parameter function with Plot3D and Manipulate sliders (with two params controlled by sliders and the other vary in the "x-y" plane). However, I'm not getting any output when my non-plotted parameters are Manipulate controlled?
The following 1d plot example replicates what I'm seeing in the more complex plot attempt:
Clear[g, mu]
g[ x_] = (x Sin[mu])^2
Manipulate[ Plot[ g[x], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}]
Plot[ g[x] /. mu -> 1, {x, -10, 10}]
The Plot with a fixed value of mu has the expected parabolic output in the {0,70} automatically selected plotrange, whereas the Manipulate plot is blank in the {0, 1} range.
I was suspecting that the PlotRange wasn't selected with good defaults when the mu slider control was used, but adding in a PlotRange manually also shows no output:
Manipulate[ Plot[ g[x], {x, -10, 10}, PlotRange -> {0, 70}], {{mu, 1}, 0, 2 \[Pi]}]
This is because the Manipulate parameters are local.
The mu in Manipulate[ Plot[ g[x], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}] is different from the global mu you clear on the previous line.
I suggest using
g[x_, mu_] := (x Sin[mu])^2
Manipulate[Plot[g[x, mu], {x, -10, 10}], {{mu, 1}, 0, 2 \[Pi]}]
The following works too, but it keeps changing the value of a global variable, which may cause surprises later unless you pay attention, so I don't recommend it:
g[x_] := (x Sin[mu])^2
Manipulate[
mu = mu2;
Plot[g[x], {x, -10, 10}],
{{mu2, 1}, 0, 2 \[Pi]}
]
It may happen that you Clear[mu], but find that it gets a value the moment the Manipulate object is scrolled into view.
Another way to overcome Manipulate's localization is to bring the function inside the Manipulate[]:
Manipulate[Module[{x,g},
g[x_]=(x Sin[mu])^2;
Plot[g[x], {x, -10, 10}]], {{mu, 1}, 0, 2 \[Pi]}]
or even
Manipulate[Module[{x,g},
g=(x Sin[mu])^2;
Plot[g, {x, -10, 10}]], {{mu, 1}, 0, 2 \[Pi]}]
Both of which give
Module[{x,g},...] prevents unwanted side-effects from the global context. This enables a simple definition of g: I've had Manipulate[]ed plots with dozens of adjustable parameters, which can be cumbersome when passing all those parameters as arguments to the function.

Evaluation of Expressions inside Manipulate Statements

I have problems getting Manipulate to work with code assigned to variables that should be evaluated inside the Manipulate statement. Here is how it goes ...
test1={a,b,c};
Manipulate[test1,{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
So {a, b, c} are not updated. Ok, whatever, let's enforce the evaluation of test1
Manipulate[Evaluate[test1],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
Now it works. But if I want to plot the list of manipulated elements, like this
Manipulate[ListPlot[Evaluate[test1]],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
Manipulate[Evaluate[ListPlot[test1]],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
I end up with
in both chases.
I am aware of 'Evaluate Expressions inside Dynamic or Manipulate' in Mathematica's documentation, but I am pretty sure that it does not provide a solution to my problem.
So the problem is that test1 is defined in terms of global variable Global`a,
but the a defined in the manipulate is created by a DynamicModule and is thus local. This is what acl showed with his Hold[a] example.
Maybe the easiest way to fix this is to use With to insert test1 into the manipulate:
Clear[a, b, c]
test1 = {a, b, c};
With[{test1 = test1},
Manipulate[test1, {a, 0, 10, .1}, {b, 0, 10, .1}, {c, 0, 10, .1}]]
This way the Manipulate never actually sees test1, all it sees is {a,b,c} which it then goes on to correctly localize. Although, this will run into problems if a,b,c have been given a value before the Manipulate is run - thus the Clear[a,b,c] command.
I think that the best practice is to make all local variables completely explicit in the manipulate. So you should do something like
Clear[a, b, c, test1]
test1[a_, b_, c_] := {a, b, c};
Manipulate[test1[a, b, c], {a, 0, 10, .1}, {b, 0, 10, .1}, {c, 0, 10, .1}]
This avoids problems with the global vs local variables that you were having. It also makes it easier for you when you have to come back and read your own code again.
Edit to answer the question in the comments "I really would like to understand why Evaluate does not work with the somewhat nested ListPlot?". IANLS (I am not Leonid Shifrin) and so I don't have a perfect Mathematica (non)standard evaluation sequence running in my brain, but I'll try to explain what's going on.
Ok, so unlike Plot, ListPlot does not need to localize any variables, so it does not have the Attribute HoldAll.
Let's define something similar to your example:
ClearAll[a, test]
test = {a, a + 1};
The final example you gave is like
Manipulate[Evaluate[ListPlot[test]], {a, 0, 1}]
By looking at the Trace, you see that this first evaluates the first argument which is ListPlot[test] ~> ListPlot[{a,a+1}]
and since a is not yet localized, it produces an empty list plot. To see this, simply run
ListPlot[{a, a + 1}]//InputForm
to get the empty graphics object
Graphics[{}, {AspectRatio -> GoldenRatio^(-1), Axes -> True, AxesOrigin -> {0, 0}, PlotRange -> {{0., 0.}, {0., 0.}}, PlotRangeClipping -> True, PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}]
As the symbolic values a have been thrown out, they can not get localized by the Manipulate and so not much else happens.
This could be fixed by still evaluating the first argument, but not calling ListPlot until after Manipulate has localized the variables. For example, both of the following work
Manipulate[Evaluate[listPlot[test]], {a, 0, 1}] /. listPlot -> ListPlot
Manipulate[Evaluate[Hold[ListPlot][test]], {a, 0, 1}] // ReleaseHold
The fact that ListPlot throws away non-numeric values without even the slightest complaint, is probably a feature, but can lead to some annoyingly hard to track bugs (like the one this question pertains to). Maybe a more consistent (but less useful?) behaviour would be to return an unevaluated ListPlot if the plot values are non-numeric... Or to at least issue a warning that some non-numeric points have been discarded.
The penultimate example you gave is (more?) interesting, it looks like
Manipulate[ListPlot[Evaluate[test]], {a, 0, 1}]
Now since Manipulate has the attribute HoldAll, the first thing it does is wrap the arguments in Hold, so if you look at the Trace, you'll see Hold[ListPlot[Evaluate[test]]] being carried around. The Evaluate is not seen, since as described in the Possible Issues section, "Evaluate works only on the first level, directly inside a held function". This means that test is not evaluated until after the variables have been localized and so they are taken to be the global a and not the local (DynamicModule) a.
It's worth thinking about how the following variations work
ClearAll[a, test, f, g]
SetAttributes[g, HoldAll];
test = {a, a + 1};
Grid[{
{Manipulate[test, {a, 0, 1}], Manipulate[Evaluate[test], {a, 0, 1}]},
{Manipulate[f[test], {a, 0, 1}],
Manipulate[f[Evaluate[test]], {a, 0, 1}]},
{Manipulate[g[test], {a, 0, 1}],
Manipulate[g[Evaluate[test]], {a, 0, 1}]}
}]
Here is why it doesn't work:
Manipulate[
{
Hold[a]
},
{a, 0, 10, .1},
{b, 0, 10, .1},
{c, 0, 10, .1}
]
One may fix this in various ways. One is to simply define test1 with the localized variables, like so:
ClearAll[test1, a, b, c];
Manipulate[
test1 = {a, b, c};
{
test1
},
{a, 0, 10, .1},
{b, 0, 10, .1},
{c, 0, 10, .1}
]
and then eg
ClearAll[test1, a, b, c];
Manipulate[
test1 = {a, b, c};
ListPlot#test1,
{a, 0, 10, .1},
{b, 0, 10, .1},
{c, 0, 10, .1}
]
works.
If you prefer to define test1 globally, this
ClearAll[test1, a, b, c];
test1 = {a, b, c};
Manipulate[
test1,
{a, 0, 10, .1},
{b, 0, 10, .1},
{c, 0, 10, .1},
LocalizeVariables -> False,
TrackedSymbols -> test1
]
works.

Update Manipulate[]'d plots when parameters change

I've been fighting with Mathematica's Manipulate function for the last few days for a project.
I'm working on tweaking assumptions and boundary conditions that go into a physical model. For this, I want to be able to plot different equations and adjust the parameters and have the graphs update on the fly. Manipulate seems to be the perfect tool for the job -- except that I can't get it to work. The plots won't update when the 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 refresh by clicking func1, adjusting a, and then clickingfunc1 again, but I'm hoping to have it update when I adjust a because the real functions I'm using are rather temperamental with respect to their parameters.
-Because I'll be dealing with long functions that have different parameters, using a list of functions is useful.
EDIT:
In case it produces any ideas for anyone, here are some working examples of the individual components of what I want to do (from the Wolfram documentation):
Plot graphs and have them update when parameters are changed:
Manipulate[
Plot[Sin[a x + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
{{b, 0, "Phase Parameter"}, 0, 10}
]
Note: This breaks when the function is taken outside:
func[x] := Sin[a x + 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 function being plotted:
Manipulate[
Plot[f[x], {x, 0, 2 Pi}], {f, {Sin -> "sine", Cos -> "cosine", Tan -> "tangent"}}
]
Edit 2
Changed func2 from a*x^2 to a*x^2 + b*x + c to reflect the fact that the functions may have different parameters.
Edit 3 Added the tidbit I use to get nice names on the function buttons.
There are two problems that prevent your Manipulate statement from working.
First, while the Manipulate variable a is global due to the LocalizeVariables -> False setting, the Plot variable x is not. x is local to the Plot expression.
The second problem is that Manipulate, by default, assumes TrackedSymbols -> Full. This means that only symbols that explicitly appear in the manipulated expression are tracked. Note that a does not appear in the expression, so it is not tracked.
We can correct both problems thus:
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}
]
The changes are:
funcList was changed to {func1, func2}
The Plot expression was changed to function[x], thereby referencing the local x variable.
The Manipulate option TrackedSymbols :> {a, function} was added.
function is initially unset.
I'd do this in a slightly different way:
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 this may be unsuitable for what you want. Do your functions have different signatures?
EDIT: I've renamed the parameter to b to make it clearer that is it just a parameter being passed, as opposed to a global variable as you were using it.

Setting all points of a given ListPlot with a given color in Mathematica

How can I make it such that plotting the following function
ListPointPlot3D[points, PlotStyle -> PointSize[0.05]];
the points I see are green or yellow, for instance, instead of the typical dark blue ones?
Thanks
Use Directive to combine styles, ie
ListPointPlot3D[points, PlotStyle -> Directive[{PointSize[0.05], Green}]]
Edit I give you below two possible solutions in a context related to your previous question. Nevertheless, please note that #Yaroslav's code is much better.
f[x_, y_] := x^2 + y^2;
t = Graphics3D[{PointSize[Large], Red, Point#
Flatten[Table[{x, y, f[x, y]}, {x, 0, 10, 1}, {y, 1, 2, 1}], 1]}];
b = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10},
ColorFunction -> "MintColors"];
Show[{b, t}]
Or
f[x_, y_] := x^2 + y^2;
points = Flatten[Table[{x, y, f[x, y]}, {x, 0, 10, 1}, {y, 1, 2, 1}],
1];
a = ListPointPlot3D[points,
PlotStyle -> Table[{Red, PointSize[0.05]}, {Length#t}]];
b = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10},
ColorFunction -> "MintColors"];
Show[{b, a}]
Sometimes I find the following approach useful, as it allows me to
manipulate the plot symbol (PlotMarkers does not seem to work with ListPointPlot3D,
at least in Mathematica 7) [originally suggested by Jens-Peer Kuska]:
ListPointPlot3D[{{1,1,1},{2,2,2},{3,3,3}}]/.Point[xy_]:>(Style[Text["\[FilledUpTriangle]",#],Red,FontSize-> 20]&/#xy)

Resources