Skip to main content

dynamic - How are parameters evaluated for a Plot in Manipulate


I am trying to get my head around how Manipulate evaluates functions in a Plot. I have read the introduction to Manipulate, and introduction to Dynamic, but I still can't figure it.


For my specific example, I have a function bigA parameterised by m1 and m2 (this relates to question),


bigA[t_]:= (m1+m2) ((m1 m2 t)/(m1+m2)^3)^0.25

So when I try to plot it in Manipulate,


Manipulate[
Plot[bigA[t], {t, 1, 10}],
{{m1, 1.4}, 0.8, 3},
{{m2, 1.4}, 0.8, 3}]


Nothing appears. I presume this is because m1 and m2 aren't being evaluated. But I don't know what the order is supposed to be.



The thing is, this seems to work when I Evaluate and don't plot, i.e,


Manipulate[Evaluate@bigA[t],
{{m1, 1.4}, 0.8, 3},
{{m2, 1.4}, 0.8, 3}]

So couldn't I just stick a Plot command in there somewhere?



Answer




The problem is that inside the Manipulate, m1 and m2 are replaced with localized versions (as in Module) rather than assigned (as in Block). Since the m1 and m2 from bigA are outside the Manipulate, and bigA[t] is evaluated only after the replacement of m1 and m2 inside the Manipulate, they are not affected by the manipulation.


The best solution is to give m1 and m2 as extra arguments:


bigA[t_, m1_, m2_] := (m1+m2) ((m1 m2 t)/(m1+m2)^3)^0.25
Manipulate[Plot[bigA[t, m1, m2], {t, 1, 10}],
{{m1, 1.4}, 0.8, 3},
{{m2, 1.4}, 0.8, 3}]

If for some reason you cannot do that, you can also use replacement rules as follows:


bigA[t_] := (m1+m2) ((m1 m2 t)/(m1+m2)^3)^0.25
Manipulate[Plot[bigA[t]/.{m1->mm1,m2->mm2}, {t, 1, 10}],

{{mm1, 1.4}, 0.8, 3},
{{mm2, 1.4}, 0.8, 3}]

This works because ReplaceAll (/.) does the replacements only after the left hand side has been evaluated, and the mm1 and mm2 are now inside the Manipulate, so they can be properly localized.


About your edit:


By adding Evaluate@ at the beginning of the argument to Manipulate, you override Mathematica's order of evaluation. So with


Manipulate[Evaluate@bigA[t],
{{m1, 1.4}, 0.8, 3},
{{m2, 1.4}, 0.8, 3}]


Mathematica first evaluates bigA[t] to (m1+m2) ((m1 m2 t)/(m1+m2)^3)^0.25, and only then proceeds to evaluate the Manipulate, which therefore sees the m1 and m2.


Now this will not work with Plot, because the whole Plot statement will be executed, before Manipulate will have a chance to insert m1 and m2. So when Plot evaluates bigA[t], it will receive an expression containing m1 and m2 instead of a number, and thus produce an empty graph. This graph (which no longer contains any trace of m1 or m2) will then be passed to Manipulate. Of course replacing m1 and m2 at this stage doesn't work, because they already vanished.


So in essence, while without Evaluate, m1 and m2 are substituted too late, with Evaluate@Plot they are consumed too early.


Now you might have the idea to use Manipulate[Plot[Evaluate@bigA[t],...],...] instead, in order to evaluate bigA[t] (to get m1 and m2 visible) but not Plot (because that only works after m1 and m2 got a value). However that doesn't work either, because Evaluate only affects order of evaluation when it appears as immediate argument of the expression being evaluated. So while evaluating Manipulate, the Evaluate in the argument of Plot is not considered. It will be considered at the time Plot is evaluated, but at that time it's already too late.


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Adding a thick curve to a regionplot

Suppose we have the following simple RegionPlot: f[x_] := 1 - x^2 g[x_] := 1 - 0.5 x^2 RegionPlot[{y < f[x], f[x] < y < g[x], y > g[x]}, {x, 0, 2}, {y, 0, 2}] Now I'm trying to change the curve defined by $y=g[x]$ into a thick black curve, while leaving all other boundaries in the plot unchanged. I've tried adding the region $y=g[x]$ and playing with the plotstyle, which didn't work, and I've tried BoundaryStyle, which changed all the boundaries in the plot. Now I'm kinda out of ideas... Any help would be appreciated! Answer With f[x_] := 1 - x^2 g[x_] := 1 - 0.5 x^2 You can use Epilog to add the thick line: RegionPlot[{y < f[x], f[x] < y < g[x], y > g[x]}, {x, 0, 2}, {y, 0, 2}, PlotPoints -> 50, Epilog -> (Plot[g[x], {x, 0, 2}, PlotStyle -> {Black, Thick}][[1]]), PlotStyle -> {Directive[Yellow, Opacity[0.4]], Directive[Pink, Opacity[0.4]],