Skip to main content

graphics - Using Manipulate with a control variable for selective recalculation


I'm trying to adapt Nasser's method for using a control variable inside Manipulate to selectively choose what part of the code should be recalculated depending on the controls changed. I've also read his previous notes but the last version seems much more simple and something I can follow.


I need this functionality for a simulation I'm working on about the field lines of an electric dipole.


In the simulation it should be possible to move the two charges q1 and q2 generating the field and also to move a third point P. The point P only serves to see the field line passing through P.


Since the single field line originating from P should have different graphical parameters than the field lines of the "background field" I created two different Streamplots: "fieldlines" is for the general field and "fieldpoint" is for the single line through P. They are combined together with the "Show" command.


So my problem is similar to the one addressed in the first link. If I move the point P I don't want the Streamplot fieldlines of the electric field lines recalculated, but just the Streamplot fieldpoint (dealing with the single line through P). On the contrary both Streamplots should be recalculated when the two charges generating the field are moved or changed.


Up to now, following Nasser's model I've come up with the following "example" code (my intended simulation is much more complicated):



Manipulate[tick;
Show[f1, f2],
Grid[{
{"q1pos",
Slider2D[
Dynamic[q1pos, {q1pos = #; f1 = fieldlines[q1pos, q2pos];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}}], Dynamic[q1pos]},
{"q2pos",
Slider2D[

Dynamic[q2pos, {q2pos = #; f1 = fieldlines[q1pos, q2pos];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}}],
Dynamic[q2pos]}, {"pt",
Slider2D[Dynamic[pt, {pt = #; f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}}], Dynamic[pt]}
}],
{{tick, False}, None},
{{q1pos, {-2, 0}}, None},
{{q2pos, {2, 0}}, None},

{{pt, {-2.5, 2}}, None},
{{f1, fieldlines[{-2, 0}, {2, 0}]}, None},
{{f2, fieldpoint[{-2, 0}, {2, 0}, {-2.5, 2}]}, None},
TrackedSymbols :> {tick},
Initialization :> (
{
field[x_Real, y_Real] := Module[{},
(*heavy computation goes here*)
{(2 (x - q1pos[[1]]))/
EuclideanDistance[q1pos, {x, y}]^3 + (-2 (x - q2pos[[1]]))/

EuclideanDistance[q2pos, {x, y}]^3, (2 (y - q1pos[[2]]))/
EuclideanDistance[q1pos, {x, y}]^3 + (-2 (y - q2pos[[2]]))/
EuclideanDistance[q2pos, {x, y}]^3}];
fieldlines[q1pos_, q2pos_] := Module[{},
(*heavy computation goes here*)
StreamPlot[field[x, y], {x, -5, 5}, {y, -5, 5},
StreamPoints -> pnts,
StreamScale -> 1/20, ImageSize -> 600]];
fieldpoint[q1pos_, q2pos_, pt_] := Module[{},
(*heavy computation goes here*)

StreamPlot[field[x, y], {x, -5, 5}, {y, -5, 5},
StreamPoints -> {{{pt, {Thickness[0.005], RGBColor[1, 0, 0],
Arrowheads[0.02]}}}, Automatic, {ForwardBackward, 400}},
ImageSize -> 600]];
pnts = Tuples[{-3, -2, -1, 0, 1, 2, 3}, 2]
}
)
]

But there are problems with this code:




  1. The first run is very slow; the second run is ok (there must be some problem with the inizializaton).

  2. When I move the point P I can see that the Streamplot of the "background" field lines is not recalculated. That's fine and that's what I wanted. Anyway the single field line loses some of the specifications (arrows). The same thing happens when one of the charges q1 or q2 are moved.

  3. I'd like to use three Locator instead of three Slider2D for q1, q2 and P in my simulation, but I haven't found yet if that's possible and how to do that.


Any help?



Answer



You have few issues there. But nothing major. This runs much faster now. It also fixes the background field lines. Will add more comments later. For you third request, using Locators, needs more time. I'd actually suggest making a new question just for the locator question (your third question, i.e. changing Slider2D to Locator, since this is not related to this method itself and it is not a simple change as 1,2 questions you had).


This below addresses issue 1 and 2.


Some coding things fixed: need to make each function self contained. Do not reference Manipulate internal variables from the Initialization section functions. Pass all these via call parameters. Initialization section should not contain variables assignments. Only definitions (functions, and any other rules). Keep all variables inside Manipulate itself, or inside Modules inside Manipulate.



Added PerformanceGoal to Stream plots. few display changes.


One thing to note with this method, is that Manipulate variables needs to be explicitly initialized, this is the same as when using DynamicModule[{f=,b=},... where each symbol is initialized. This is needed, since Manipulate expression is evaluated first time, and it needs values for this variables. Other than this little house keeping, everything else is standard operating procedures.


enter image description here


Updated


This version has ContinuousAction -> option on each Dynamic. This way one can choose to make it false if needed. When it is true, it is much faster, but does not show the display as the mouse is being moved.


Manipulate[tick;
Show[f2, f1, ImageSize -> 300],

Text@Grid[{
{Grid[{

{"Q1"},
{Slider2D[Dynamic[q1pos, {q1pos = #;
f1 = fieldlines[q1pos, q2pos, pnts];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &, SynchronousUpdating -> False], {{-6, -6}, {6, 6}},
ContinuousAction -> True]},
{Dynamic[q1pos]}
}, Alignment -> Center]}
,
{Grid[{{"Q2"},

{Slider2D[Dynamic[q2pos, {q2pos = #;
f1 = fieldlines[q1pos, q2pos, pnts];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &, SynchronousUpdating -> False], {{-6, -6}, {6, 6}},
ContinuousAction -> True]},
{Dynamic[q2pos]}
}, Alignment -> Center]}
,
{Grid[{{"pt"},
{Slider2D[Dynamic[pt, {pt = #;

f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &, SynchronousUpdating -> False], {{-6, -6}, {6, 6}},
ContinuousAction -> True]},
{Dynamic[pt]}
}, Alignment -> Center]}
}, Spacings -> {.5, 1.5}, Alignment -> Center, Frame -> All
]
,
{{tick, False}, None},
{{q1pos, {-2, 0}}, None},

{{q2pos, {2, 0}}, None},
{{pt, {-2.5, 2}}, None},
{{f1, fieldlines[{-2, 0}, {2, 0}, Tuples[{-3, -1, 0, 1, 3}, 2]]}, None},
{{f2, fieldpoint[{-2, 0}, {2, 0}, {-2.5, 2}]}, None},
(*{{pnts,Tuples[{-3,-2,-1,0,1,2,3},2]},None},*)
{{pnts, Tuples[{-3, -1, 0, 1, 3}, 2]}, None},
ControlPlacement -> Left,
ContinuousAction -> False,
SynchronousUpdating -> True,
SynchronousInitialization -> False,

TrackedSymbols :> {tick}, Initialization :> (

field[x_, y_, q1pos_List, q2pos_List] := Module[{},
{
(2 (x - q1pos[[1]]))/EuclideanDistance[q1pos, {x, y}]^3 +
(-2 (x - q2pos[[1]]))/EuclideanDistance[q2pos, {x, y}]^3
,
(2 (y - q1pos[[2]]))/EuclideanDistance[q1pos, {x, y}]^3 +
(-2 (y - q2pos[[2]]))/EuclideanDistance[q2pos, {x, y}]^3}
];


fieldlines[q1pos_List, q2pos_List, pnts_List] := Module[{x, y},
StreamPlot[field[x, y, q1pos, q2pos], {x, -5, 5}, {y, -5, 5},
StreamPoints -> pnts, StreamScale -> Automatic , PerformanceGoal -> "Quality"]];

fieldpoint[q1pos_List, q2pos_List, pt_List] := Module[{x, y},
StreamPlot[field[x, y, q1pos, q2pos], {x, -5, 5}, {y, -5, 5},
StreamPoints -> {
{
{pt, {Thickness[0.005], RGBColor[1, 0, 0], Arrowheads[0.02]}}

}, Automatic, {ForwardBackward, 400}}, PerformanceGoal -> "Quality"]
]

)
]

Original code


This version defaults to ContinuousAction -> true which makes it much slower, but does show the display as the mouse is being moved.


Manipulate[tick;
Show[f2, f1, ImageSize -> 300],


Text@Grid[{
{Grid[{
{"Q1"},
{Slider2D[Dynamic[q1pos, {q1pos = #;
f1 = fieldlines[q1pos, q2pos, pnts];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}, {.1, .1}}]},
{Dynamic[q1pos]}
}, Alignment -> Center]}

,
{Grid[{{"Q2"},
{Slider2D[Dynamic[q2pos, {q2pos = #;
f1 = fieldlines[q1pos, q2pos, pnts];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}, {.1, .1}}]},
{Dynamic[q2pos]}
}, Alignment -> Center]}
,
{Grid[{{"pt"},

{Slider2D[Dynamic[pt, {pt = #;
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}, {.1, .1}}]},
{Dynamic[pt]}
}, Alignment -> Center]}
}, Spacings -> {.5, 1.5}, Alignment -> Center, Frame -> All
]
,
{{tick, False}, None},
{{q1pos, {-2, 0}}, None},

{{q2pos, {2, 0}}, None},
{{pt, {-2.5, 2}}, None},
{{f1, fieldlines[{-2, 0}, {2, 0}, Tuples[{-3, -2, -1, 0, 1, 2, 3}, 2]]}, None},
{{f2, fieldpoint[{-2, 0}, {2, 0}, {-2.5, 2}]}, None},
{{pnts, Tuples[{-3, -2, -1, 0, 1, 2, 3}, 2]}, None},
ControlPlacement -> Left,
ContinuousAction -> False,
SynchronousInitialization -> False,
TrackedSymbols :> {tick},
Initialization :> (


field[x_, y_, q1pos_List, q2pos_List] := Module[{},
{
(2 (x - q1pos[[1]]))/EuclideanDistance[q1pos, {x, y}]^3 +
(-2 (x - q2pos[[1]]))/EuclideanDistance[q2pos, {x, y}]^3
,
(2 (y - q1pos[[2]]))/EuclideanDistance[q1pos, {x, y}]^3 +
(-2 (y - q2pos[[2]]))/EuclideanDistance[q2pos, {x, y}]^3}
];


fieldlines[q1pos_List, q2pos_List, pnts_List] := Module[{x, y},
StreamPlot[field[x, y, q1pos, q2pos], {x, -5, 5}, {y, -5, 5},
StreamPoints -> pnts, StreamScale -> Automatic , PerformanceGoal -> "Quality"]];

fieldpoint[q1pos_List, q2pos_List, pt_List] := Module[{x, y},
StreamPlot[field[x, y, q1pos, q2pos], {x, -5, 5}, {y, -5, 5},
StreamPoints -> {
{
{pt, {Thickness[0.005], RGBColor[1, 0, 0], Arrowheads[0.02]}}
}, Automatic, {ForwardBackward, 400}}, PerformanceGoal -> "Quality"]

]

)
]

Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

list manipulation - Selecting multiple columns from a matrix?

Sample data: data = { {{2013, 1, 1}, 24.13, 167.67, 231.82}, {{2013, 1, 2}, 32.15, 170.92, 225.99}, {{2013, 1, 3}, 35.43, 172.68, 221.67}, {{2013, 1, 4}, 36.73, 173.05, 218.32}, {{2013, 1, 5}, 58.19, 165.96, 197.05}, {{2013, 1, 6}, 69.99, 163.50, 187.52}, {{2013, 1, 7}, 71.37, 154.21, 175.58}, {{2013, 1, 8}, 72.51, 149.66, 163.25}}; I want a DateListPlot with three graphs, so for a matrix formed by columns 1 and 2, one for columns 1 and 3, and 1 for columns 1 and 4. At the moment I'm using this code: data2 = Transpose[{data[[All, 1]], data[[All, 2]]}]; data3 = Transpose[{data[[All, 1]], data[[All, 3]]}]; data4 = Transpose[{data[[All, 1]], data[[All, 4]]}]; DateListPlot[{data2, data3, data4}, Joined -> True, Filling -> {3 -> {1}}] but I have a hunch that this can be done more efficiently. I don't like the Transpose s in particular. Any ideas? edit (for extra credit) What if I need to multiply the second column by 2, which in my solution is simp...

calculus and analysis - Integral of the Sinc product

Let us consider the following integral $$ B_n = \int_0^\infty \prod_{k=1,3,5,\dots}^n\frac{\sin (x/k)}{x/k}dx $$ By definition, Sinc[x] == Sin[x]/x , therefore B[n_?OddQ] := Integrate[Product[Sinc[x/k], {k, 1, n, 2}], {x, 0, Infinity}] B /@ Range[1, 13, 2] {Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2} OK, everything is fine. But... B[15] 467807924713440738696537864469 Pi/935615849440640907310521750000 What's going on? Answer As Eckhard wrote in comments B[n] is the n-th Borwein integral . (The letter B was not accidental :) ) This funny properties of Borwein integrals is related to the Fourier transform of Sinc function FourierTransform[Sinc[x], x, k] 1/2 Sqrt[Pi/2] (Sign[1 - k] + Sign[1 + k]) Plot[%, {k, -2, 2}, Filling -> 0] which is the box function. The result is $\pi/2$ while the sum $$ 1/3+1/5+\dots+1/n $$ B_n = \frac{\pi}{2} - \pi \bigg(\sum_{k=3,5,\ldots}^n\frac{1}{k} -1\biggr)^\frac{n-1}{2}\prod_{k=3,5,\dots}^n\frac{k}{k-1}. $$ For $n=15$ it is equal to $$ \frac{46780...