9
$\begingroup$

I've uploaded an image showing a demonstration I'd like to create, but I definitely need help. There is a lot of talent on this list, so maybe someone can help me get started learning how to add graphics to a plot and animate them.

What I'd like to create is a Manipulate demonstration that will animate the plot of Exp[I θ] + b Exp[-I θ], where b is a real number between 0 and 1. As you can see in the attached figure, I want to draw two circles centered at the origin, one with radius 1, the second with radius b. I'd like the Manipulate to have a slider that will allow me to vary the value of b between 0 and 1.

Next, I want to draw the vectors Exp[I θ] and the vector b Exp[-I θ], as pictured in the sketch. Then I want to complete the parallelogram and draw the vector Exp[I θ] + b Exp[-I θ]. I also want a slider for θthat varies from 0 to 2 π, and when I move the slider, I want to see all of the vectors move, the parallelogram adjust, and the tip of the vector Exp[I θ] + b Exp[-I θ] trace out the dotted ellipse.

I'd really appreciate it if someone could show me how to do this.

Exp[I Theta] + b Exp[-I Theta]


This was overlooked in the initial answers:
How do we add the curve Exp[I θ] + b Exp[-I θ] to the figure?

$\endgroup$
3
  • $\begingroup$ Even though this is just a "code request" you get my +1 for a clearly written and illustrated question. Thank you. $\endgroup$
    – Mr.Wizard
    Dec 29, 2012 at 13:04
  • $\begingroup$ I think the answer is just Show. $\endgroup$
    – amr
    Dec 30, 2012 at 21:32
  • $\begingroup$ I have merged the "Part II" question into this one as the ellipse was there in your illustration from the beginning. (It is therefore not a moving-target-question which is discouraged.) $\endgroup$
    – Mr.Wizard
    Jan 1, 2013 at 17:34

5 Answers 5

11
$\begingroup$

Mathematica graphics

Manipulate[
 Module[{
   p1 = {Cos[theta ], Sin[theta]}, 
   p2 = {b Cos[-theta], b Sin[-theta ]},
   p3 = {(1 + b) Cos[theta ], (1 - b) Sin[theta ]}},
  Graphics[{
    Circle[{0, 0}],
    {Red, Circle[{0, 0}, b]},
    Arrow[{{0, 0}, p1}],
    {Red, Arrow[{{0, 0}, p2}]},
    Line[{{0, 0}, p1, p3, p2, {0, 0}}],
    {Dashed, Line[{{0, 0}, p3}]}}, 
   PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}}, Axes -> True]
  ],
 Grid[{
   {"b ", Control[{{b, .5, ""}, 0, 1, .1}], Dynamic[b]},
   {"theta ", Control[{{theta, Pi/4, ""}, 0, 2 Pi, 0.1}], 
    Row[{Dynamic[theta*180./Pi], Degree}]
    }}],
 ContinuousAction -> True
 ]
$\endgroup$
2
  • $\begingroup$ +1 very nicely done. You certainly helped him out! :) Does ContinuousAction default to false - I can't remember. $\endgroup$
    – cormullion
    Dec 29, 2012 at 10:39
  • $\begingroup$ That's a good plan, I think - better explicit than invisible, sometimes.. $\endgroup$
    – cormullion
    Dec 29, 2012 at 18:56
7
$\begingroup$

Blatantly copying Nasser's code, here is a slightly different style.

Mathematica graphics

I used both Text and Tooltip merely to demonstrate what is possible.

Manipulate[
  With[{
    p1 = {Cos[θ], Sin[θ]},
    p2 = {b Cos[-θ], b Sin[-θ]},
    p3 = {(1 + b) Cos[θ], (1 - b) Sin[θ]}
   },
   Graphics[{
     Circle[{0, 0}],
     {Red, Circle[{0, 0}, b], Arrow[{{0, 0}, p1}], Arrow[{{0, 0}, p2}]},
     Arrow[{{0, 0}, p3}],
     {Dashed, Line[{p1, p3, p2}], Circle[{0, 0}, Abs @ {b + 1, b - 1}]},
     {Red, Text[E^(I "θ"), 1.1 p1], Text["b"/E^(I "θ"), 1.1 p2]},
     Tooltip[Point[p3], E^(I "θ") + "b"/E^(I "θ")]
    },
    PlotRange -> {{-2, 2}, {-1.3, 1.3}},
    BaseStyle -> 20,
    ImageSize -> 500
   ]
  ],
  Grid[{
    {"b ", Control[{{b, 0.5, ""}, 0, 1, 0.1}], Dynamic[b]},
    {"θ", Control[{{θ, Pi / 4, ""}, 0, 2 Pi, 0.1}], Row[{Dynamic[θ 180` / Pi], Degree}]}
  }, BaseStyle -> 20]
]
$\endgroup$
3
  • $\begingroup$ I used both Text and Tooltip to demonstrate what is possible. $\endgroup$
    – Mr.Wizard
    Dec 29, 2012 at 14:09
  • $\begingroup$ How do we add the ellipse traced by the vector Exp[I theta] + b Exp[-I theta]? $\endgroup$
    – David
    Dec 30, 2012 at 3:53
  • $\begingroup$ @David Answer amended. $\endgroup$
    – Mr.Wizard
    Dec 30, 2012 at 14:16
6
$\begingroup$

I used this once:

ComplexParametricPlot[f_, stuff__] := ParametricPlot[{Re[#], Im[#]} &@f, stuff]

Then putting it in the previous Manipulate (from "part I"), and giving b a complex control, we get:

Manipulate[
 Show[Graphics[
   GraphicsComplex[
    complexparts /@ {E^(I π θ), b E^(-I π θ), 
      E^(I π θ) + b E^(-I π θ), 0},
    {Circle[4],
     {Red, Circle[4, Norm[b]]}, Arrow[{4, 1}], {Red, Arrow[{4, 2}]},
     Line[{1, 3, 2}],
     {Dashed, Line[{4, 3}]}}],
   PlotRange -> 2, Axes -> True],
  ComplexParametricPlot[
   E^(I π t) + b E^(-I π t), {t, 0, θ + 10^-6}],
  ImageSize -> 250
  ],
 {{b, 0.5}, 
  LocatorPane[
    Dynamic[{Re[b], Im[b]}, (b = If[Norm[#] > 1, Complex @@ Normalize[#], Complex @@ #]) &],
    Labeled[
     Graphics[{GrayLevel[0.97], Rectangle[{-1, -1}, {1, 1}], White, 
       Disk[]}, Axes -> True, ImageSize -> 130], Row[{"b = ", Dynamic[b]}]]] &,
  ControlPlacement -> Left},
 {{θ, 1/4.}, 0., 2,
  Row[{Manipulator[##], "  ", 
     InputField[#1, Number, FieldSize -> {{0, 6}, 1}, 
      Appearance -> "Frameless"], " π"}] &},
 {{complexparts, {Re[#], Im[#]} &}, None}]

Output of Manipulate

Addendum

Here's another version, inspired by Mr.Wizard, kguler, and the OP. Looks nearly the same, but it uses Circle and Rotate to trace the ellipse.

Manipulate[
 Graphics[GraphicsComplex[
   complexparts /@ {E^(I π θ), b E^(-I π θ), 
     E^(I π θ) + b E^(-I π θ), 0},
   {Circle[4],
    {Red, Circle[4, Norm[b]]}, Arrow[{4, 1}], {Red, Arrow[{4, 2}]},
    Line[{1, 3, 2}],
    {Dashed, Blue, Line[{4, 3}], 
     Rotate[
      Circle[4, Abs@{E^(I #) + b E^(-I #), E^(I #) - b E^(-I #)}, {-#, π θ - #}],
      #, {0, 0}] &[Arg[b]/2]}}],
  PlotRange -> 2, Axes -> True, ImageSize -> 250],
 {{b, 0.5 + 0.2 I}, 
  LocatorPane[
    Dynamic[{Re[b], Im[b]}, (b = If[Norm[#] > 1, Complex @@ Normalize[#], Complex @@ #]) &],
    Labeled[
     Graphics[{GrayLevel[0.97], Rectangle[{-1, -1}, {1, 1}], White, Disk[]},
      Axes -> True, ImageSize -> 130], 
     Row[{"b = ", Dynamic[b]}]]] &,
  ControlPlacement -> Left},
 {{θ, 0.25}, 0., 2.,
  Row[{Manipulator[##], "  ", 
     InputField[#1, Number, FieldSize -> {{0, 6}, 1}, 
      Appearance -> "Frameless"], " π"}] &},
 {{complexparts, {Re[#], Im[#]} &}, None}]
$\endgroup$
1
  • $\begingroup$ +1 for the complex locator pane for b $\endgroup$ Dec 30, 2012 at 11:49
4
$\begingroup$

My own little modification. Deals with complex numbers E^(I θ) as such, and maps the Complex graphics to Real coordinates in a, ahem, GraphicsComplex-- sorry, can't resist a bad pun. Couldn't figure out how to get the InputField that serves as a label to format properly. (Doesn't round numbers the way a regular label will. Feel free to fix it, if you can.)

Output of Manipulate

Manipulate[
 Graphics[GraphicsComplex[
   complexparts /@ {E^(I π θ), b E^(-I π θ), 
     E^(I π θ) + b E^(-I π θ), 0},
   {Circle[4],
    {Red, Circle[4, b]}, Arrow[{4, 1}], {Red, Arrow[{4, 2}]},
    Line[{1, 3, 2}],
    {Dashed, Line[{4, 3}]}}],
  PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}}, Axes -> True],
 {{b, .5}, 0, 1, Appearance -> "Labeled"},
 {{θ, 1/4.}, 0., 2,
  Row[{Manipulator[##], "  ", 
     InputField[#1, Number, FieldSize -> {{0, 6}, 1}, 
      Appearance -> "Frameless"], " π"}] &},
 {{complexparts, {Re[#], Im[#]} &}, None}]
$\endgroup$
1
  • $\begingroup$ Nice improvement. +1 $\endgroup$
    – Mr.Wizard
    Dec 30, 2012 at 14:05
2
$\begingroup$

Yep, the code ParametricPlot[{(1 + b) Cos[theta], (1 - b) Sin[theta]}, {theta, 0, 2 Pi}] was definitely my thought as well, but what was tough was where to put it. I finally got it to work as follows:

Manipulate[
 Module[{
   p1 = {Cos[theta], Sin[theta]},
   p2 = {b Cos[-theta], b Sin[-theta]},
   p3 = {(1 + b) Cos[theta], (1 - b) Sin[theta]}},
  Show[{
    ParametricPlot[{(1 + b) Cos[theta], (1 - b) Sin[theta]}, {theta, 
      0, 2 Pi}, PlotRange -> 1 + b],
    Graphics[{
      Circle[{0, 0}],
      {Red, Circle[{0, 0}, b]},
      Arrow[{{0, 0}, p1}],
      {Red, Arrow[{{0, 0}, p2}]},
      {Black, Dashed, Line[{{p1, p3}, {p2, p3}}]},
      {Blue, Arrow[{{0, 0}, p3}]}
      }]
    }]
  ],
 {{b, 0.5, "b"}, 0, 2},
 {{theta, 0, "theta"}, 0, 2 Pi}
 ]

I now have a working demonstration of the graph of $e^{i\theta}+be^{-i\theta}$ as $0\le\theta\le 2\pi$. There are three cases to consider: (1) $0<b<1$, (2) $b=1$, and (3) $b>1$, where $b$ is a real number. In either case, we can perform the following analysis:

$$ \begin{align*} e^{i\theta}+be^{-i\theta} &=(\cos\theta+i\sin\theta)+b(\cos(-\theta)+i\sin(-\theta))\\ &=(1+b)\cos\theta+i(1-b)\sin\theta \end{align*} $$

When we compare $z=x+iy$ with the last result, we get: $$ \begin{align*} x&=(1+b)\cos\theta\\ y&=(1-b)\sin\theta \end{align*} $$ Thus, $$ \begin{align*} \frac{x^2}{(1+b)^2}&=\cos^2\theta\\ \frac{y^2}{(1-b)^2}&=\sin^2\theta,\\ \end{align*} $$ so we can see that $$ \frac{x^2}{(1+b)^2}+\frac{x^2}{(1-b)^2}=1. $$ Thus, we have an ellipse with $x$-intercepts at $(-1-b,0)$ and $(1+b,0)$ and $y$-intercepts at $(0,-1+b)$ and $(0,1-b)$. Thus, PlotRange->1+b is a good move, as seen in the image below.

Ellipse

By visualizing the geometry, one can determine that the ellipse will be traced out in a counter-clockwise direction when $b<1$ and it will be traced out in a clockwise direction when $b>1$, and finally, it will trace out the line segment on the real axis between $-2$ and $2$ when $b=1$. But these cases can also be determined via the equations

$$ \begin{align*} x&=(1+b)\cos\theta\\ y&=(1-b)\sin\theta \end{align*} $$

For example, when $b=1$, these become:

$$ \begin{align*} x&=2\cos\theta\\ y&=0 \end{align*} $$

This defines the segment on the real axis between $-2$ and $2$.

There is one more step to be accomplished before I am done examining this question. I now need to select a $b$-value that is complex, that is, of the form $b=re^{i\phi}$. In this case, one can show that

$$ \begin{align*} e^{i\theta}+be^{-i\theta} &=e^{i\theta}+re^{i\phi}e^{-i\theta}\\ &=e^{i\phi}(e^{i(\theta-\phi)}+re^{-i(\theta-\phi)}), \end{align*} $$

which means that the entire image will simply be rotated through $\phi$ radians. So my final need will be to replace the $b$ slider with $r$ and $\phi$ sliders, where $r$ is a real number between 0 and 2, while $\phi$ is an angle between $-\pi/2$ and $\pi/2$. The $r$-value should affect the image in the same way as the $b$-value of the current image, while the $\phi$-value should rotate the image through $\phi$ radians.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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