0
$\begingroup$

Since the original post of mine, which is below, is too long, too complicated and time-consuming for others, I tried to mimic the same structure with a very simple code. I want to calculate $x=alfa+beta$ by choosing a random seed. If output $x$ is less than 0.5, then I analyze $x$, otherwise I only want to dynamically change $beta$ (which is the case below), while keeping the seed value of $alfa$ unchanged. But this change in $beta$ should be done only if I click on the slider for $beta$. If I get $x1>=0.5$, then I analyze $x1$. The logic is similar to Do loop, with a difference that I am trying to adjust $alfa$ and $beta$ to get the desired output.

Any idea is appreciated.

 DynamicModule[{alfa, beta},
 Manipulate[
  SeedRandom[seed];
  alfa = RandomReal[];
  beta = RandomReal[{-1, 1}];
  x = alfa + beta;
  If[x < 0.5, x, x1 = alfa + beta],
  {{seed, 1, "seed"}, 1, 100, 1},
  {{alfa, 0.5, "alfa"}, 0, 1, 0.02},
  {Slider[Dynamic[beta], {-1, 1}], Dynamic[beta]},
  TrackedSymbols :> True, ContinuousAction -> False, 
  SynchronousUpdating -> True
  ]
 ]

I am editing the above code with Manipulate within Manipulate, Let us call this edited code as Edit 1:

Manipulate[
 SeedRandom[seed];
 Pause[2];
 alfa := RandomReal[1, 20];
 Manipulate[
  Plot[{Sin[a y], Sin[a1 y]}, {y, 0, 10}],
  Row[{Control[{a, alfa, Animator, AnimationRunning -> False}]}],
  Row[{Control[{a1, alfa*(1 + 0.5), Animator, 
      AnimationRunning -> False}]}],
  SaveDefinitions -> True
  ],
 {{seed, 1, "seed"}, 1, 100, 1}
 ]

Edit 1 produces the desired output, though I like to make a Slider for $0.5$ in the second Control command. As one may see, the first Manipulate randomizes $alfa$ and the second Manipulate adjusts $alfa$ to the desired level. In this example, the adjustment value is set to $0.5$ in the second Control command. If I can make this adjustment value as a Dynamic Slider (for updating purposes), that will answer my question.

$\endgroup$
  • 1
    $\begingroup$ You have a number of problems. The variables alfa and beta are set in the code to be random, yet you also specify the values of alfa and beta in the sliders (parameters of the Manipulate). You can do one or the other, but not both. Also, you can porbably accomplish your complete task without use of Dynamic -- the Manipulate is already a Dynamic object that can do what you seem to be asking. $\endgroup$ – bill s Sep 30 '18 at 14:39
  • $\begingroup$ @bill s: After If statement, the Manipulate command will be closed, but then I want to be able to adjust $alfa$ and $beta$ to the desired values. That is why I want to use $alfa$ and $beta$ in the Slider formats. However, I want to adjust the parameters one at a time, not like SeedRandom[] does. I hope my explanation makes sense. $\endgroup$ – Tugrul Temel Sep 30 '18 at 14:55
  • $\begingroup$ I'm afraid I can't understand what you are trying to do. The If statement is vacuous because it says if x<0.5 then x (=alfa + beta) else x1 (=alfa + beta). But that aside, you have to decide whether alfa and beta are set randomly or are set by a slider. You can't do both. $\endgroup$ – bill s Sep 30 '18 at 15:51
  • $\begingroup$ @bill s: To explain in more detail, I edited my question with a new example. I hope that this new example illustrates what I want to achieve. $\endgroup$ – Tugrul Temel Sep 30 '18 at 16:36
1
$\begingroup$

I found an answer to my question formulated as in Edit 1. Let me first give the code that answers to my question and then briefly explain what it does.

Clear[alfa, newalfa, a1, a2, x, s, chn];
Manipulate[
 SeedRandom[s];
 alfa = RandomReal[1, 20];
 newalfa = alfa*(1 + chn);
 Manipulate[
  Plot[{Sin[a1 x], Sin[a2 x]}, {x, 0, 10}],
  Row[{Control[{a1, alfa, Animator, AnimationRunning -> False}]}], (* model 1 *)
  Row[{Control[{a2, newalfa, Animator, AnimationRunning -> False}]}]
  ], (* model 2 *)
 {{s, 1, "s"}, 1, 100, 1},
 {{chn, 0, "change"}, -0.2, 0.2, 0.02}
 ]

This Manipulate inside Manipulate does this: The 1st Manipulate randomly (controlled by $s$) picks 20 numbers between 0 and 1 to create a variable $alfa$, which is then used to create another variable called $newalfa$ by introducing a change parameter $chn$. These variables are used in the 2nd Manipulate to compare two model plots, different from each other only with respect to the two parameters $a1$ and $a2$. The 2nd Manipulate helps me to visually adjust and compare the two models. The Sliders $s$ and $change$ set $alfa$ and $newalfa$, respectively, preparing the model parameters to be compared. In this formulation, model 1 can be thought as a benchmark model that is compared with the simulated model 2. This is the answer I was after.

I extended the above code to include CommunityGraphPlot with FindGraphCommunities to dynamically see the effects of changes in $alfa$ on the community structure. Here is the code (together with the output generated) to achieve this task. Below, everything is fine.

Clear[alfa, tao, newalfa, newtao, G, Gcom, mat, matG, aa, wG, wGcom];
Manipulate[
 SeedRandom[s];
 G = RandomGraph[{Round[n], Round[n*(n - 1)*d]}, 
   DirectedEdges -> True];
 Gcom = CommunityGraphPlot[G, FindGraphCommunities[G], 
   CommunityRegionStyle -> LightGray, Method -> "Modularity"]; 
 mat = Table[RandomReal[], {i, 1, n}, {j, 1, n}];
 matG = AdjacencyMatrix[G]*mat;
 aa = SparseArray[matG];
 wG = Graph[aa["NonzeroPositions"], EdgeWeight -> aa["NonzeroValues"],
    DirectedEdges -> True];
 wGcom = CommunityGraphPlot[wG, FindGraphCommunities[wG], 
   CommunityRegionStyle -> LightGray, Method -> "Modularity"];

 alfa = RandomReal[1, n];
 tao = RandomReal[1, n];
 newalfa = alfa*(1 + chnAlfa);
 newtao = tao*(1 + chnTao);
(* insert 1 *)

 Manipulate[
(* insert 2 *)
  plot = Plot[
    {Sin[a1 x], Sin[a2 x]}, {x, 0, 10}, AxesLabel -> {x, Sin[a x]}, 
    PlotLegends -> Placed[{"sin(a1 x)", "sin(a2 x)"}, Below]
    ];
  Grid[{   
    {Text["Benchmark vs Simulated Model"], Text["digraph (n,d): G"], 
     Text["communities in\nAdjacency G"], 
     Text["communities in\nWeighted G"]}, {plot, G, Gcom, wGcom}   
        }],
  Row[{Control[{a1, alfa*tao, Animator, AnimationRunning -> False}]}],
  Row[{Control[{a2, newalfa*newtao, Animator, 
      AnimationRunning -> False}]}],
(* insert 3 *)
  ],
 {{s, 1, "s"}, 1, 100, 1},
 {{n, 20, "n"}, 1, 50, 1},
 {{d, 0.05, "d"}, 0, 1, 0.02},
 {{chnAlfa, 0, "\[CapitalDelta]alfa"}, -0.2, 0.2, 0.02},
 {{chnTao, 0, "\[CapitalDelta]tao"}, -0.2, 0.2, 0.02}
 ]

enter image description here

When I edit the above code by inserting the following three inserts, the code does not work, although I apply the same logic as in $plot$. (* Insert 1 *) is:

AT = alfa*tao;
newAT = newalfa*newtao;
newAdT = Array[(Sqrt[newalfa]/newtao) &, {n,
   n}];  (* a list of n matrices, each with (n,n) dimension *) 

(* Insert 2 *) is:

bb = SparseArray[a3*matG]; 
wGbb =
Graph[bb["NonzeroPositions"], EdgeWeight -> bb["NonzeroValues"], 
  DirectedEdges -> True];
wGbbCom = 
  CommunityGraphPlot[wGbb, FindGraphCommunities[wGbb], 
   CommunityRegionStyle -> LightGray, Method -> "Modularity"];

(* Insert 3 *) is:

Row[{Control[{a3, newAdT, Animator, AnimationRunning -> False}]}] 

Can someone tell me where I make the mistake in adding these 3 inserts?

$\endgroup$
  • $\begingroup$ I had the above problem for sometime but only recently was I inspired by @FredSimons's interactivity course as well as his one of early comments about Manipulate in Manipulate. Since my answer is related to his earlier comments and does not use DynamicModule at all, I like to update the title of the current post to Manipulate within Manipulate. $\endgroup$ – Tugrul Temel Oct 1 '18 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.