mathematica Plot with Manipulate shows no output - wolfram-mathematica

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.

Related

Working with implicit functions in Mathematica

Can I plot and deal with implicit functions in Mathematica?
for example :-
x^3 + y^3 = 6xy
Can I plot a function like this?
ContourPlot[x^3 + y^3 == 6*x*y, {x, -2.7, 5.7}, {y, -7.5, 5}]
Two comments:
Note the double equals sign and the multiplication symbols.
You can find this exact input via the WolframAlpha interface. This interface is more forgiving and accepts your input almost exactly - although, I did need to specify that I wanted some type of plot.
Yes, using ContourPlot.
And it's even possible to plot the text x^3 + y^3 = 6xy along its own curve, by replacing the Line primitive with several Text primitives:
ContourPlot[x^3 + y^3 == 6 x y, {x, -4, 4}, {y, -4, 4},
Background -> Black, PlotPoints -> 7, MaxRecursion -> 1, ImageSize -> 500] /.
{
Line[s_] :>
Map[
Text[Style["x^3+y^3 = 6xy", 16, Hue[RandomReal[]]], #, {0, 0}, {1, 1}] &,
s]
}
Or you can animate the equation along the curve, like so:
res = Table[ Normal[
ContourPlot[x^3 + y^3 == 6 x y, {x, -4, 4}, {y, -4, 4},
Background -> Black,
ImageSize -> 600]] /.
{Line[s_] :> {Line[s],
Text[Style["x^3+y^3 = 6xy", 16, Red], s[[k]], {0, 0},
s[[k + 1]] - s[[k]]]}},
{k, 1, 448, 3}];
ListAnimate[res]
I'm guessing this is what you need:
http://reference.wolfram.com/mathematica/Compatibility/tutorial/Graphics/ImplicitPlot.html
ContourPlot[x^3 + y^3 == 6 x*y, {x, -10, 10}, {y, -10, 10}]

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.

Arrows for the axes

How to get arrows for the axes when using the command Plot in Mathematica?
Thanks for any helpful answers.
For 2D plots such as generated by Plot the following works great:
Plot[Sin[x], {x, 0, 10}, AxesStyle -> Arrowheads[0.07]]
or with custom arrow heads:
h = Graphics[Line[{{-1, 1/2}, {0, 0}, {-1, -1/2}}]];
Plot[Sin[x], {x, 0, 10},
AxesStyle -> Arrowheads[{{Automatic, Automatic, h}}]]
Building on Sjoerd's answer,
a plot such as
may be obtained as follows (for example):
Plot[Sin[x], {x, -2\[Pi], 2 \[Pi]},
AxesStyle-> {
Directive[{Red,
Arrowheads[{{-0.06,0(*Xleft*),{Graphics[{
Polygon[
{{-1,0.5`},{0,0},{-1,-0.5`}}]}],0.98`}},
{0.03,.9(*Xright*),{Graphics[{
Polygon[
{{-1,0.5`},{0,0},{-1,-0.5`}}]}],0.98`}}}]}],
Directive[{Blue,
Arrowheads[{{-0.05,0(*Ydown*),{Graphics[{
Polygon[
{{-1,0.5`},{0,0},{-1,-0.5`}}]}],0.98`}},{0.03,.8(*Yup*),{Graphics[{
Polygon[
{{-1,0.5`},{0,0},{-1,-0.5`}}]}],0.98`}}}]}
]}]
There are nice examples of arrowheads given in Drawings Tools and Graphics Inspector. There are probably much better ways of getting the info but I annotate a plot with an arrow that I like and then abstract (using a suggestion from Simon):
Cases["Paste-Graphic_Here", Arrowheads[___], Infinity]
To give another example:
The code is as follows
Plot[Sin[x], {x, -2\[Pi],2 \[Pi]},
AxesStyle-> { Directive[{Red,
Arrowheads[{{-0.06,0.1(*Xleft*),
{Graphics[{arrowhead}]/.arrowhead-> arrowhead2,0.98`}},
{0.05,0.95(*Xright*),
{Graphics[{arrowhead}],0.98`}}}]/.arrowhead-> arrowhead4}],
Directive[{Blue,
Arrowheads[{{-0.05,0(*Ydown*),
{Graphics[{arrowhead}]/.arrowhead-> arrowhead3,0.98`}},{0.03,.8(*Yup*),
{Graphics[{arrowhead}]/.arrowhead-> arrowhead1,0.98`}}}]}
]}]
where
arrowhead1=Polygon[{{-1,0.5`},{0,0},{-1,-0.5`}}];
arrowhead2=Polygon[{{-1.5833333333333333`,0.4166666666666667`},{-1.5410500000000003`,0.369283333333333`},{-1.448333333333333`,0.255583333333333`},{-1.3991000000000005`,0.18721666666666673`},{-1.3564666666666663`,0.11826666666666673`},{-1.3268499999999999`,0.05408333333333341`},{-1.3166666666666667`,0.`},{-1.3268499999999999`,-0.048950000000000195`},{-1.3564666666666663`,-0.11228333333333372`},{-1.3991000000000005`,-0.18353333333333333`},{-1.448333333333333`,-0.2562833333333335`},{-1.5410500000000003`,-0.38048333333333345`},{-1.5833333333333333`,-0.43333333333333335`},{0.`,0.`},{-1.5833333333333333`,0.4166666666666667`},{-1.5833333333333333`,0.4166666666666667`}}];
arrowhead3=Polygon[{{-1,0.5`},{0,0},{-1,-0.5`},{-0.6`,0},{-1,0.5`}}];
arrowhead4={{FaceForm[GrayLevel[1]],Polygon[{{-0.6`,0},{-1.`,0.5`},{0.`,0},{-1.`,-0.5`},{-0.6`,0}}],Line[{{-0.6`,0},{-1.`,0.5`},{0.`,0},{-1.`,-0.5`},{-0.6`,0}}]}};
arrowhead5=Polygon[{{-0.6582278481012658`,-0.43037974683544306`},{0.`,0.`},{0.`,0.`},{0.`,0.`},{0.`,0.`},{0.`,0.`},{-0.6455696202531646`,0.43037974683544306`},{-0.4810126582278481`,0.`},{-0.6582278481012658`,-0.43037974683544306`},{-0.6582278481012658`,-0.43037974683544306`}}];
A list of arrowheads 1 to 5:
Here you have a solution posted in https://math.stackexchange.com/
As the solution in the reference is for Plot3D, here I modified (but not improved) it for Plot[ ]:
axes[x_, y_, f_, a_] :=
Graphics[Join[{Arrowheads[a]},
Arrow[{{0, 0}, #}] & /# {{x, 0}, {0, y}},
{Text[Style["x", FontSize -> Scaled[f]], {0.9*x, 0.1*y}],
Text[Style["y", FontSize -> Scaled[f]], {0.1 x, 0.95*y}]
}]]
Show[Plot[Exp[-x^2], {x, -2, 2},
Axes -> None,
PlotRange -> {{-2.1, 2.1}, {-.1, 1.1}}],
axes[2, 1, 0.05, 0.02]
]

Mathematica: Asynchronous incremental generation of dynamical graphics

What is the simplest way to asynchronously apply consecutive improvements to a Graphics object in a dynamic setting (and abort the evaluation of the unneeded results if input changes while they are being computed)?
As a simple example, consider this:
speed[r_] := Graphics#{Red, Circle[{0, 0}, r]}
qualityA[r_] := (Pause[1]; Graphics#{Red, Disk[{0, 0}, r]})
qualityB[r_] := (Pause[1]; Graphics#{Black, Circle[{0, 0}, r]})
Manipulate[Show[
ControlActive[speed[r], {qualityA[r], qualityB[r]}],
PlotRange -> {{-1, 1}, {-1, 1}}
], {{r, .5}, 0, 1}]
How can I evaluate qualityA and qualityB consecutively, and append their output to the display when it is ready?
Bonus points for Abort'ing the evaluation of unneeded results, and for allowing a part of the result to be calculated multiple times, so that after releasing the control I would see e.g. {qualityA[r]} then {qualityA[r],qualityB[r]}, and finally {qualityA2[r],qualityB[r]}.
My colleague Lou, an expert on Dynamic, suggested this neat answer:
Manipulate[
ControlActive[
Graphics[{LightRed, Circle[{0, 0}, r]},
PlotRange -> {{-1, 1}, {-1, 1}}],
DynamicModule[{exprs = {Red, Circle[{0, 0}, r]}, rr = r},
Graphics[Dynamic[exprs], PlotRange -> {{-1, 1}, {-1, 1}}],
Initialization :> (Pause[1];
AppendTo[exprs, {Red, Disk[{0, 0}, rr]}]; Pause[1];
AppendTo[exprs, {Black, Circle[{0, 0}, rr]}]),
SynchronousInitialization -> False]], {{r, 0.5}, 0, 1}]
How it works:
When not ControlActive, the result of the dynamic expression is a DynamicModule. The code for refining the graphics is contained in the Initialization option of this DynamicModule. The SynchronousInitialization -> False makes this initialization run asynchronously.
Renaming rr = r in the DynamicModule serves two purposes. First, it makes the result always depend on the Manipulate variable r. Second, you can check rr != r to decide whether the user has moved the slider during initialization, and abort early, saving computation time:
Manipulate[
ControlActive[
Graphics[{LightRed, Circle[{0, 0}, r]},
PlotRange -> {{-1, 1}, {-1, 1}}],
DynamicModule[{exprs = {Red, Circle[{0, 0}, r]}, rr = r},
Graphics[Dynamic[exprs], PlotRange -> {{-1, 1}, {-1, 1}}],
Initialization :> (If[rr =!= r, Abort[]]; Pause[1];
AppendTo[exprs, {Red, Disk[{0, 0}, rr]}]; If[rr =!= r, Abort[]];
Pause[1]; AppendTo[exprs, {Black, Circle[{0, 0}, rr]}]),
SynchronousInitialization -> False]], {{r, 0.5}, 0, 1}]
I hope this helps.
Really good question.
I may be overlooking a simpler way. There often is one when it comes to Dynamic... But here is my suggestion:
DynamicModule[{quality = 0, exprs = {}},
Manipulate[
Show[
ControlActive[
exprs = {}; quality = 0; Graphics#{Red, Circle[{0, 0}, r]},
Switch[quality,
0, Pause[1]; quality = 1;
AppendTo[exprs, Graphics#{Red, Disk[{0, 0}, r]}],
1, Pause[1]; quality = 2;
AppendTo[exprs, Graphics#{Black, Circle[{0, 0}, r]}],
_, r];
exprs
],
PlotRange -> {{-1, 1}, {-1, 1}}],
{{r, .5}, 0, 1}
]
]
First we define some variables controlling increasingly high quality graphics: quality (ranging to 0 to the maximum quality, 2 in this case), and exprs (a list of expressions to Show, just as in your example).
Now note what happens in the two cases of ControlActive:
When ControlActive, the result is the same as yours, except we take the opportunity to reset quality and exprs relating to the "high quality" graphics.
When not ControlActive, the Dynamic expression evaluates to
code; exprs
This expression has the following key properties.
It returns the list exprs every time.
Each time code is evaluated, it improves the graphics by appending something to exprs.
Each time code is evaluated, at least one of the variables lexically contained in code; exprs (such as quality) is changed. This means Dynamic will go ahead and evaluate our dynamic expression again, and again, and again, until ...
Eventually code evaluates without any of the variables lexically contained in code; exprs changing. This means Dynamic will stop re-evaluating.
The final evaluation lexically contains r. (Via the otherwise useless default case in the Switch, _, r.) This is important to make the slider still trigger updates.
Give it a try and let me know if that works for you.
Edit: What $Version of Mathematica are you using? I see some version dependence in the behavior of my code above.
Edit 2: I asked an expert on Dynamic and he found a better way, which I will describe in a separate answer.

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