1
$\begingroup$

I'm trying to manipulate the value of x, while also showing the graphic. My code is listed below:

R0 = {{Cos[2 Pi/3], -Sin[2 Pi/3]}, {Sin[2 Pi/3], Cos[2 Pi/3]}}; V1[x_] := {{Cos[x]}, {Sin[x]}}; R1 = R0.V1; R2 = R0.R1; X1 = V1[[1]][[1]]; Y1 = V1[[2]][[1]]; X2 = R1[[1]][[1]]; Y2 = R1[[2]][[1]]; X3 = R2[[1]][[1]]; Y3 = R2[[2]][[1]]; C1 = {Opacity[.2], Yellow, Disk[{0, 0}, 10, {0, 2 Pi/3}]}; C2 = {Opacity[.2], Orange, Disk[{0, 0}, 10, {2 Pi/3, 2*2 Pi/3}]}; C3 = {Opacity[.2], Blue, Disk[{0, 0}, 10, {2*2 Pi/3, 2 Pi}]}; T1 = Graphics[Translate[C1, {10*X1, 10*Y1}]]; T2 = Graphics[Translate[C2, {10*X2, 10*Y2}]]; T3 = Graphics[Translate[C3, {10*X3, 10*Y3}]]; Manipulate[Show[T1, T2, T3, PlotRange -> All, Axes -> True], {x, 0, 2 Pi}] 

In short, I intend to generate a graphic that shows how the pieces move as the angle for x_ changes. I can do this manually for any chosen value in place of x_ by choosing x_ and then deleting the extra manipulate function at the end.

$\endgroup$
4
  • $\begingroup$ What specifically? $\endgroup$ Commented Sep 14, 2018 at 4:31
  • $\begingroup$ Welcome to MMA stack exchange. A couple of things in your code that could help. Firstly keep in mind "V1" is different to "V1[x]" this means that when you take a part of V1 i.e. V1[[1]][[2]] it is not defined. You would need to do V1[x][[1]][[2]]. The next problem is in your definition of the Graphics. One solution is to put Manipulate[ Show[Evaluate[T1 /. x -> y], Evaluate[T2 /. x -> y], Evaluate[T3 /. x -> y], PlotRange -> All, Axes -> True], {y, 0, 2 Pi}] so that you force it to calculate a translated image before showing it. If this is unclear then I will post an answer $\endgroup$ Commented Sep 14, 2018 at 4:34
  • $\begingroup$ Thanks! So in such a case, will I have to modify every line that involves x_ aside from V1 ? $\endgroup$ Commented Sep 14, 2018 at 4:35
  • $\begingroup$ Although it is nice to accept my answer, try to wait 24h or so before accepting to give others a chance. The answers of @m_goldberg and kirma I think are much better and are probably cleaner code than what I gave which was more of an extended comment. $\endgroup$ Commented Sep 14, 2018 at 5:56

3 Answers 3

2
$\begingroup$

This is easier as an answer. Basically what you want to do is to use set delayed ":=" for those things you want to calculate later once you set the value of "X". You also need to be careful in your definition of V1 which is different to V1[x]. I hope this helps

R0 := {{Cos[2 Pi/3], -Sin[2 Pi/3]}, {Sin[2 Pi/3], Cos[2 Pi/3]}}; V1[x_] := {{Cos[x]}, {Sin[x]}}; R1 := R0.V1[x]; R2 := R0.R1; X1 := V1[x][[1]][[1]]; Y1 := V1[x][[2]][[1]]; X2 := R1[[1]][[1]]; Y2 := R1[[2]][[1]]; X3 := R2[[1]][[1]]; Y3 := R2[[2]][[1]]; C1 = {Opacity[.2], Yellow, Disk[{0, 0}, 10, {0, 2 Pi/3}]}; C2 = {Opacity[.2], Orange, Disk[{0, 0}, 10, {2 Pi/3, 2*2 Pi/3}]}; C3 = {Opacity[.2], Blue, Disk[{0, 0}, 10, {2*2 Pi/3, 2 Pi}]}; T1 = Graphics[Translate[C1, {10*X1, 10*Y1}]]; T2 = Graphics[Translate[C2, {10*X2, 10*Y2}]]; T3 = Graphics[Translate[C3, {10*X3, 10*Y3}]]; Manipulate[Show[Evaluate[T1 /. x -> y], Evaluate[T2 /. x -> y],Evaluate[T3 /. x -> y], PlotRange -> All, Axes -> True], {y, 0, 2 Pi}] 
$\endgroup$
3
  • $\begingroup$ Thanks, but I'm still getting the same error saying x_ is protected... $\endgroup$ Commented Sep 14, 2018 at 4:42
  • $\begingroup$ Try starting with a new kernel. $\endgroup$ Commented Sep 14, 2018 at 4:44
  • 1
    $\begingroup$ Thanks! that fixed it like magic! :) $\endgroup$ Commented Sep 14, 2018 at 4:46
2
$\begingroup$

The simplest fix to your code is to write

V1 = {{Cos[x]}, {Sin[x]}}; ... Manipulate[ Show[{T1, T2, T3} /. x :> u, PlotRange -> All, Axes -> True], {{u, 0, "x"}, 0, 2 Pi"}] 

demo

$\endgroup$
2
$\begingroup$

This is a rewrite of the code in a style more natural to Mathematica:

Manipulate[ Graphics[ {Opacity[.2], MapThread[Translate, {{{Yellow, Disk[{0, 0}, 10, {0, 2 Pi/3}]}, {Orange, Disk[{0, 0}, 10, {2 Pi/3, 2*2 Pi/3}]}, {Blue, Disk[{0, 0}, 10, {2*2 Pi/3, 2 Pi}]}}, Table[RotationTransform[i 2 Pi/3]@RotationTransform[x][{10, 0}], {i, 0, 2}]}]}, PlotRange -> All, Axes -> True], {x, 0, 2 Pi}] 

This could be further simplified to a MapThread over colors and sector angles:

Manipulate[ Graphics[ {Opacity[.2], MapThread[ {#1, Disk[RotationTransform[First@#2 + x][{10, 0}], 10, #2]} &, {{Yellow, Orange, Blue}, Partition[Range[0, 2 Pi, 2 Pi/3], 2, 1]}]}, PlotRange -> All, Axes -> True], {x, 0, 2 Pi}] 

It is understandable that these styles aren't necessarily immediately obvious if one hasn't used them before, though.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.