I'm trying to write a script that will dynamically update when I change the tensor that I am working with and the interval tensor elements.

Whenever I update the tensor elements that I am working with in the Dynamic function with the manipulate function, the values are updated, and then it is automatically set to zero again and the plot is reset, as shown in the video here

I am only trying to update the plot so when I update the tensor elements, I get the updated plot

Main Goals

  • Be able to select specific tensors with PopUpMenu\
  • Be able to change values of tensor elements


  • Plot is automatically set to zero when updated

Here is the following code

Dynamic[Manipulate[Module[{c, \[Epsilon], cx, cy, cz, kin, kout, Epin, Esin, Epout, Esout, Rotx, Roty, Rotz, Rot, chiprime, PxPin, 
     PyPin, PzPin, PxSin, PySin, PzSin, pinpout, pinsout, sinpout, sinsout, Ipinpout, Ipinsout, Isinpout, Isinsout}, 
    c = 3*10^8; 
    \[Epsilon] = 8.85/10^12; 
    {cx, cy, cz} := {0, 0, 1}; 
    kin[\[Theta]_] := {-Sin[\[Theta]], 0, -Cos[\[Theta]]}; 
     kout[\[Theta]_] := {Sin[\[Theta]], 0, Cos[\[Theta]]}; 
     Epin[\[Theta]_] := {Cos[\[Theta]], 0, Sin[\[Theta]]}; 
     Esin[\[Theta]_] := {0, 1, 0}; 
     Epout[\[Theta]_] := {Cos[\[Theta]], 0, -Sin[\[Theta]]}; 
     Esout[\[Theta]_] = {0, 1, 0}; 
     Rot[\[Phi]_] := {{Cos[\[Phi]] + cx^2*(1 - Cos[\[Phi]]), cx*cy*(1 - Cos[\[Phi]]) - cz*Sin[\[Phi]], cx*cz*(1 - Cos[\[Phi]]) - cy*Sin[\[Phi]]}, 
       {cx*cy*(1 - Cos[\[Phi]]) + cz*Sin[\[Phi]], Cos[\[Phi]] + cy^2*(1 - Cos[\[Phi]]), cy*cz*(1 - Cos[\[Phi]]) - cx*Sin[\[Phi]]}, 
       {cx*cz*(1 - Cos[\[Phi]]) - cy*Sin[\[Phi]], cy*cz*(1 - Cos[\[Phi]]) + cx*Sin[\[Phi]], Cos[\[Phi]] + cz^2*(1 - Cos[\[Phi]])}}; 
     chiC1 := {{{xxx, xxy, xxz}, {xxy, xyy, xyz}, {xxz, xyz, xzz}}, {{yxx, yxy, yxz}, {yxy, yyy, yyz}, 
         {yxz, yyz, yzz}}, {{zxx, zxy, zxz}, {zxy, zyy, zyz}, {zxz, zyz, zzz}}}; 
     chiprime[\[Phi]_, chi_] := FullSimplify[Transpose[Rot[\[Phi]] . Transpose[Rot[\[Phi]] . (chi . Transpose[Rot[\[Phi]]])]]];
     PxPin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[1,i]][[j]]*Epin[\[Theta]][[i]]*Epin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PyPin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[2,i]][[j]]*Epin[\[Theta]][[i]]*Epin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PzPin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[3,i]][[j]]*Epin[\[Theta]][[i]]*Epin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PxSin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[1,i]][[j]]*Esin[\[Theta]][[i]]*Esin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PySin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[2,i]][[j]]*Esin[\[Theta]][[i]]*Esin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     PzSin[\[Theta]_, \[Phi]_, chi_] := Sum[chiprime[\[Phi], chi][[3,i]][[j]]*Esin[\[Theta]][[i]]*Esin[\[Theta]][[j]], {i, 1, 3}, {j, 1, 3}]; 
     pinpout[\[Theta]_, \[Phi]_, chi_] := FullSimplify[(PxPin[\[Theta], \[Phi], chi]*Cos[\[Theta]])^2 + (PzPin[\[Theta], \[Phi], chi]*Sin[\[Theta]])^2]; 
     Ipinpout[\[Theta]_, \[Phi]_, chi_] := (1/2)*c*\[Epsilon]*pinpout[\[Theta], \[Phi], chi]^2; PolarPlot[Ipinpout[\[Theta], \[Phi], chi], {\[Phi], 0, 2*Pi},
      PlotRange -> All, PlotLabel -> Subscript[p, in]*Subscript[p, out]]], 
      {chi, {chiC1}, ControlType -> PopupMenu}, 
    {{xxx, 0, "xxx"}, -1, 1, 0.1, Appearance -> "Labeled"}]]
  • 2
    $\begingroup$ why are you putting Manipulate inside Dynamic?? Manipulate is already DynamicModule itself. It is hard enough figuring the correct way to use Dynamics on its own, never mind one inside another :) I am just curious, where did you see an example using Manipulate inside Dynamic? Is it from the help pages somewhere? $\endgroup$
    – Nasser
    Jul 31 at 1:38
  • $\begingroup$ To be honest, I was messing around and trying to dynamically change the tensors and nesting Manipulate within Dynamic worked, which yielded this problem. $\endgroup$
    – Xenconic
    Jul 31 at 1:54
  • 1
    $\begingroup$ Your code is completely unreadable. Please edit the question to include human readable code according to these instructions: How to copy code from Mathematica so it looks good on this site. $\endgroup$
    – MarcoB
    Jul 31 at 2:03
  • $\begingroup$ most of the time, by making MWE to show the problem, you'll end up finding the problem yourself (because you removed all the clutter away). So, if you can make a very small example of the problem, it will not only help you, but if you can't still solve it, it will make it much easier for others to help as the code will be much simpler. $\endgroup$
    – Nasser
    Jul 31 at 2:20
  • 1
    $\begingroup$ Thank you for reformatting and reducing your code. I noticed that chiC1 is defined using MatrixForm then used in further calculations. That is almost never right: MatrixForm is a display function that gets in the way of further computation (see point 8 in that answer). Try removing the MatrixForm wrapper, for a start. $\endgroup$
    – MarcoB
    Jul 31 at 6:49


Your Answer

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

Browse other questions tagged or ask your own question.