0
$\begingroup$

In the following code, the line defined in Epilog option of my 2nd parametric plot doesn't show up in the content pane of my Manipulate expression.

ClearAll[θ1, θ2];
m1 = 1;
m2 = 1;
lc1 = 1;
lc2 = 1;
l1 = 2;
l2 = 2;
Ix2 = 1;
Iy2 = 1;
Iz1 = 1;
Iz2 = 1;

m11[t_] := 
  m1*(lc1)^2 + m2*(l1 + lc2*Cos[θ2[t]])^2 + Ix2*(Sin[θ2[t]])^2 + 
    Iy2*(Cos[θ2[t]])^2 + Iz1;
m22[t_] := m2*(lc2)^2 + Iz2;
V11[t_] := 0;
V12[t_] := 
  (-2*m2*lc2*(l1 + lc2*Cos[θ2[t]])*Sin[θ2[t]] + 
     2 (Ix2 - Iy2)*Sin[θ2[t]]*Cos[θ2[t]])*θ1'[t];
V21[t_] := 
  (m2*lc2*(l1 + lc2*Cos[θ2[t]])*Sin[θ2[t]] - 
    (Ix2 - Iy2)*Sin[θ2[t]]*Cos[θ2[t]])*θ1'[t];
V22[t_] := 0;
M = {{m11[t], 0}, {0, m22[t]}};
V = {{V11[t], V12[t]}, {V21[t], V22[t]}};
G = {{0, 0}, {0, 0}};
initu = q[0] == {{0}, {.1}};
initv = q'[0] == {{0}, {0}};
q[t_] := {θ1[t], θ2[t]};
sol = 
  First @
    NDSolve[{
      Thread[M.q''[t] + V.q'[t] + G.q[t] == {Sin[2 t], Sin[2 t]}], 
      initu, initv}, q[t], {t, 0, 100}];

Plot[{θ1[t] /. sol, θ2[t] /. sol}, {t, 0, 100}, PlotTheme -> "Detailed"]

Manipulate[
  Show[
    ParametricPlot[{Evaluate[{l1*Cos[θ1[t]], l1*Sin[θ1[t]]} /. sol]}, {t, 0, a}, 
      AspectRatio -> 1, 
      PlotRange -> {{-5, 5}, {-5, 5}}, 
      Epilog -> 
        Line[{{0, 0}, {l1*Cos[θ1[t]], l1*Sin[θ1[t]]}}]],
    ParametricPlot[{
      Evaluate[{l1*Cos[θ1[t]] + l2*Cos[θ1[t] + θ2[t]], 
        l1*Sin[θ1[t]] + l2*Sin[θ1[t] + θ2[t]]} /. sol]}, 
      {t, 0, a},
      AspectRatio -> 1, 
      PlotRange -> {{-5, 5}, {-5, 5}}, 
      Epilog -> 
        Line[{{0, 0}, 
              {l1*Cos[θ1[t]] + l2*Cos[θ1[t] + θ2[t]], 
               l1*Sin[θ1[t]] + l2*Sin[θ1[t] + θ2[t]]}}]]], 
  {a, 0.001, 100}]
$\endgroup$
10
  • 3
    $\begingroup$ When you Show multiple plots, only the first plots Epilog gets used. $\endgroup$
    – george2079
    Jul 14, 2015 at 21:40
  • 1
    $\begingroup$ …so, put the Epilog setting in Show[] and not in any of the inner plots. $\endgroup$ Jul 14, 2015 at 21:48
  • $\begingroup$ This is listed unde "Possible Issues" in the Show documentation page: "Show uses the options from the first graphic:" $\endgroup$ Jul 14, 2015 at 23:19
  • $\begingroup$ with change epilog position it dont work too!!! it cant recognize teta1 and teta2 that i calculate from ndsolve. i also cant use evaluate!!! $\endgroup$
    – Farshid
    Jul 14, 2015 at 23:32
  • 1
    $\begingroup$ @Mr.Wizard I see your point about your answer. I don't remember now what I thought, but I probably I decided based on the question itself. My feeling is that your answer is languishing here, with its strengths hidden by the TL context & focus of this question. It seems impossible to improve this question without altering too greatly given the accepted answer. What if you or I ask a different question and you post your answer to that? E.g. How to combine plots and preserve the Epilog/Prolog of each? I think that would be more helpful to site visitors. Other solutions may be offered as well. $\endgroup$
    – Michael E2
    Jan 14, 2017 at 2:14

2 Answers 2

4
$\begingroup$

I couldn't get your code to show anything as posted. After quite a bit of debugging I came up with this:

m1 = 1;
m2 = 1;
lc1 = 1;
lc2 = 1;
l1 = 2;
l2 = 2;
Ix2 = 1;
Iy2 = 1;
Iz1 = 1;
Iz2 = 1;

m11[t_] := 
  m1*(lc1)^2 + m2*(l1 + lc2*Cos[θ2[t]])^2 + Ix2*(Sin[θ2[t]])^2 + 
    Iy2*(Cos[θ2[t]])^2 + Iz1;
m22[t_] := m2*(lc2)^2 + Iz2;
V11[t_] := 0;
V12[t_] := 
  (-2*m2*lc2*(l1 + lc2*Cos[θ2[t]])*Sin[θ2[t]] + 
     2 (Ix2 - Iy2)*Sin[θ2[t]]*Cos[θ2[t]])*θ1'[t];
V21[t_] := 
  (m2*lc2*(l1 + lc2*Cos[θ2[t]])*Sin[θ2[t]] - 
    (Ix2 - Iy2)*Sin[θ2[t]]*Cos[θ2[t]])*θ1'[t];
V22[t_] := 0;
M = {{m11[t], 0}, {0, m22[t]}};
V = {{V11[t], V12[t]}, {V21[t], V22[t]}};
G = {{0, 0}, {0, 0}};
initu = q[0] == {{0}, {.1}};
initv = q'[0] == {{0}, {0}};
q[t_] := {θ1[t], θ2[t]};

ClearAll[θ1, θ2];
sol = First@
   NDSolve[{M.q''[t] + V.q'[t] + G.q[t] == {Sin[2 t], Sin[2 t]}, 
     initu, initv}, q[t], {t, 0, 100}];

θ1 = First @* (Head @ sol[[1, 2]]);
θ2 = First @* (Head @ sol[[2, 2]]);

Plot[{θ1[t], θ2[t]}, {t, 0, 100}, PlotTheme -> "Detailed"]

plot

Manipulate[
  Show[
    ParametricPlot[{l1*Cos[θ1[t]], l1*Sin[θ1[t]]}, {t, 0, a},
      PlotStyle -> Blue], 
    ParametricPlot[
      {l1*Cos[θ1[t]] + l2*Cos[θ1[t] + θ2[t]], l1*Sin[θ1[t]] + l2*Sin[θ1[t] + θ2[t]]}, 
      {t,0, a},
      PlotStyle -> Red], 
    PlotRange -> {{-5, 5}, {-5, 5}},
    Epilog -> {
      Blue, Line[{{0, 0}, {l1*Cos[θ1[a]], l1*Sin[θ1[a]]}}],
      Red, Line[{{0, 0}, 
                 {l1*Cos[θ1[a]] + l2*Cos[θ1[a] + θ2[a]], 
                  l1*Sin[θ1[a]] + l2*Sin[θ1[a] + θ2[a]]}}]}],
  {a, 1, 100, 1, Appearance -> "Labeled"}]

demo

$\endgroup$
4
$\begingroup$

As noted in the comments only the first Epilog option will be used by Show. You could instead use a function that combines Prologs and Epilogs manually, if that is desired.

myShow[gr__] :=
 Show[gr,
  Join @@ Map[Options[#, {Prolog, Epilog}] &, {gr}] // Merge[List] // Normal
 ]

Example:

g1 = Plot[Style[2 Sin[x], Green], {x, -4, 4}, Epilog -> Circle[{0, 0}, 1.5]]

g2 = ParametricPlot[{Sin[u], Sin[2 u]}, {u, 0, 2 Pi}, 
  Epilog -> {Orange, Disk[{0, 0}, 1, #] & /@ {{0.4, 0.8}, {2.4, 2.8}}}]

enter image description here
enter image description here

myShow[g1, g2]

enter image description here

$\endgroup$

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